vbaoutlookoutlook-2016

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


The following worked on my old installation of Outlook 2016 so it must be either a small typo or there is something wrong with the references.

The code is in 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

I get:

Error message

for this line:

myFile = Application.GetOpenFilename(MultiSelect:=True)

Here are my selected references:

enter image description here


Solution

  • I was able to get your test working (In Outlook 365) by doing the following:

    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
    

    Be Aware

    This creates a new instance of the Excel application for the duration your code is running.