运行-time error '438': Object doesn't support this object or 属性 with GetOpenFilename

Run-time error '438': Object doesn’t support this object or property with GetOpenFilename

以下内容适用于我旧安装的 Outlook 2016,因此它一定是一个小错字,或者引用有问题。

代码在 Outlook 2016 中:

Sub Sample()
    Dim myFile As Variant
    Dim i As Integer

    'Open File to search
    myFile = Application.GetOpenFilename(MultiSelect:=True)

    If IsArray(myFile) Then  '<~~ If user selects multiple file
        For i = LBound(myFile) To UBound(myFile)
            MsgBox myFile(i)
        Next i
    Else '<~~ If user selects single file
        MsgBox myFile
    End If
End Sub

我得到:

对于这一行:

myFile = Application.GetOpenFilename(MultiSelect:=True)

以下是我选择的参考资料:

通过执行以下操作,我能够让您的测试工作(在 Outlook 365 中):

  • 在 VBE
  • 中启用 Excel Object Reference Library 16.0
  • 创建一个变量并将其设置为Excel.Application
  • 使用此变量限定 GetOpenFilename 方法。
Sub Sample()
    Dim myFile As Variant
    Dim i As Integer
    Dim myApp As Excel.Application
    Set myApp = New Excel.Application

    'Open File to search
    myFile = myApp.GetOpenFilename(MultiSelect:=True)

    If IsArray(myFile) Then  '<~~ If user selects multiple file
        For i = LBound(myFile) To UBound(myFile)
            MsgBox myFile(i)
        Next i
    Else '<~~ If user selects single file
        MsgBox myFile
    End If
End Sub

注意

这会在您的代码 运行 期间创建 Excel 应用程序的新实例。