【问题标题】:Run-time error '438': Object doesn’t support this object or property with GetOpenFilename运行时错误“438”:对象不支持此对象或带有 GetOpenFilename 的属性
【发布时间】:2021-08-27 10:26:54
【问题描述】:

以下内容适用于我旧安装的 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)

这是我选择的参考资料:

【问题讨论】:

    标签: vba outlook outlook-2016


    【解决方案1】:

    通过执行以下操作,我能够让您的测试正常工作(在 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 应用程序的新实例。

    【讨论】:

    • 2 天以来,我一直在查看这段代码,但它为什么不起作用是没有意义的。它现在可以按我的意愿工作,为此我感谢您。
    • @madmiddle 据我所知GetOpenFileNameExcel 对象库 的一种方法。查看您要使用的方法/属性的文档通常可以引导您朝着正确的方向前进 :-)
    猜你喜欢
    • 1970-01-01
    • 2014-03-21
    • 2023-03-30
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多