我正在使用HansUp的代码:
Private Sub Command7_Click()
Dim f As Object
Dim strFile As String
Dim strFolder As String
Dim varItem As Variant
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
If f.Show Then
For Each varItem In f.SelectedItems
strFile = Dir(varItem)
strFolder = Left(varItem, Len(varItem) - Len(strFile))
MsgBox "Folder: " & strFolder & vbCrLf & _
"File: " & strFile
Next
End If
Set f = Nothing
End Sub
但是,我希望将我选择的绝对路径放入文本框而不是弹出窗口。
我正在学习。
答案 0 :(得分:1)
我不明白为什么你得到错误438,所以只是编写并测试了这个版本。
我的命令按钮名为 cmdBrowse ,我存储所选文件路径的文本框名为 MyTextBox 。
Private Sub cmdBrowse_Click()
Const msoFileDialogFilePicker As Long = 3
Dim f As Object
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.AllowMultiSelect = False
If f.Show Then
MsgBox f.SelectedItems(1)
'Me!MyTextBox.Value = f.SelectedItems(1)
End If
Set f = Nothing
End Sub
验证MsgBox
正确显示文件路径后,请禁用该行并启用下一行:
'MsgBox f.SelectedItems(1)
Me!MyTextBox.Value = f.SelectedItems(1)