【发布时间】:2016-02-25 11:49:36
【问题描述】:
我正在尝试使用 MS Access VBA 中的文件对话框获取完整路径和文件名。
我想要做的是通过调用此函数在按钮单击时打开文件对话框。此函数应返回从文件对话框中选择的完整路径和文件名。
我评论了循环部分,因为我只想选择单个文件。
这个函数在我选择一个文件后返回错误Error: 0
到目前为止,这是我的代码。
任何人都可以找出问题所在?
谢谢
Public Function SelectTheFile() As String
On Error GoTo SelectTheFile_ErrorHandler
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.Title = "Please select one file"
If .Show = True Then
'For Each varFile In .SelectedItems
'SelectTheFile = varFile
'Debug.Print SelectTheFile
'Next
SelectTheFile = .SelectedItems(1)
Debug.Print SelectTheFile
Else
Debug.Print "Cancel"
End If
End With
SelectTheFile_ErrorHandler:
Set fd = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
End Function
【问题讨论】:
-
您只是在
SelectTheFile_ErrorHandler:之前缺少Exit Function,因此代码一直运行到最后。而且我认为你不需要Set fd = Nothing