您帖子中的 Microsoft 页面显示了用于msoFileDialogFilePicker
对话框的属性,但您的代码使用的是wdDialogFileOpen
. MS 页面上的示例代码工作正常,但尝试使用该属性wdDialogFileOpen
也会产生运行时错误。
所以这有效:
Sub ThisWorks()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
With fd
.InitialFileName = "C:\folder\printer_ink_test.docx"
'If the user presses the action button...
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MsgBox "Selected item's path: " & vrtSelectedItem
Next vrtSelectedItem
'If the user presses Cancel...
Else
End If
End With
Set fd = Nothing
End Sub
但这失败了:
Sub ThisFails()
Dim fd As Dialog
Set fd = Application.Dialogs(wdDialogFileOpen)
Dim vrtSelectedItem As Variant
With fd
' This line causes a run-time error
.InitialFileName = "C:\folder\printer_ink_test.docx"
'If the user presses the action button...
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MsgBox "Selected item's path: " & vrtSelectedItem
Next vrtSelectedItem
'If the user presses Cancel...
Else
End If
End With
Set fd = Nothing
End Sub