0

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

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Andy M
  • 167
  • 1
  • 3
  • 17

1 Answers1

1

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

  • Enable the Excel Object Reference Library 16.0 in the VBE
  • Create a variable and set it as Excel.Application
  • Use this variable to qualify the GetOpenFilename method.
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.

Samuel Everson
  • 2,097
  • 2
  • 9
  • 24
  • 1
    2 days i've been looking at this code and it just didn't make sense why it was not working. it is now working as i wanted it and for that i thank you. – Andy M Jun 10 '21 at 15:27
  • 1
    @madmiddle as far as I can tell `GetOpenFileName` is a method for the **Excel Object Library**. Looking at the documentation for the methods/properties you want to use can generally steer you in the right direction :-) – Samuel Everson Jun 13 '21 at 07:46