我在表单上有一个按钮,下面的代码位于表单中。它曾经执行没有问题。突然,当单击按钮时,我现在收到此运行时错误:
运行时错误91:在过程cmdImportEDD_Click的第0行中未设置对象变量或带块变量
我已经尝试注释掉代码中的各个行以查找问题。我最终确定FileDialog部分似乎是一个问题。但是,在上次向其添加If.. then
部分之后,该代码再次起作用,但是今天错误又回来了。
为清楚起见,该错误在执行VBA代码之前出现(因此第0行),并且Compile也不会产生任何错误!
这里我没有得到什么?
Private Sub cmdImportEDD_Click()
On Error GoTo cmdImportEDD_Click_Error
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
fDialog.AllowMultiSelect = False
fDialog.InitialFileName = GetDownloadFolder
fDialog.Show
If fDialog.SelectedItems.Count < 1 Then
Exit Sub
End If
Debug.Print fDialog.SelectedItems(1)
'Replace the selected file with the current one
Dim sOldFile As String
sOldFile = strTARGET_EDD_SALESFILE
Dim oFSO As FileSystemObject
Set oFSO = New FileSystemObject
oFSO.DeleteFile sOldFile
oFSO.MoveFile Source:=fDialog.SelectedItems(1), Destination:=sOldFile
'Perform the update
DoCmd.SetWarnings False
DoCmd.OpenQuery "qry_app_EDD", acViewNormal, acAdd
DoCmd.SetWarnings True
MsgBox "The data has been successfully imported!", vbOKOnly Or vbInformation, "Import Data: EDD"
On Error GoTo 0
Exit Sub
cmdImportEDD_Click_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdImportEDD_Click, line " & Erl & "."
End Sub
GetDownloadFolder的代码:
Function GetDownloadFolder() As String
Dim objShell
Dim objFolder
Dim objFolderItem
Dim temp
Const DESKTOP = &H10&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(DESKTOP)
Set objFolderItem = objFolder.Self
temp = objFolderItem.Path
temp = Left(temp, Len(temp) - 7) & "Downloads" '<--- I believe this is the download folder
GetDownloadFolder = temp
End Function
已取消OnError,现在在Set objFolderItem = objFolder.Self
行上收到调试错误
我设法通过用更简单的GetDownloadFolder
替换Environ("USERPROFILE") & "\Downloads"
函数中稍微复杂一些的Shell调用来使代码正常工作。
这已解决了运行时错误。