What puzzles me is that this Application.Run error doesn't appear to be known about?! (my assumption from a quick google search)
I am interested in knowing WHY this conflict occurs.
To assist testing this, I have created a sample module to demonstrate this conflict. See below:
Option Explicit
Private Msg As String, Title As String, Ans As Integer, Config As Integer
Public Sub Test_Call_Ver()
Config = vbInformation
Msg = "The test sub will be run by using CALL."
Msg = Msg & vbNewLine & "During the sub, an Application.GetOpenFileName will be attempted."
Msg = Msg & vbNewLine & vbNewLine & "PREDICTION: The sub will run without any conflicts"
Ans = MsgBox(Msg, Config, Title)
Call ExampleOfRunConflict(11)
End Sub
Public Sub Test_Run_Ver()
Config = vbInformation
Msg = "The test sub will be run by using APPLICATION.RUN."
Msg = Msg & vbNewLine & "During the sub, an Application.GetOpenFileName will be attempted."
Msg = Msg & vbNewLine & vbNewLine & "PREDICTION: The sub will not display the file picker box"
Msg = Msg & "and it will appear to run the sub _TWICE_! (Note the error message box repeated)"
Msg = Msg & vbNewLine & vbNewLine & "Why does APPPLICATION.RUN conflict when CALL does not?!"
Ans = MsgBox(Msg, Config, Title)
Application.Run "ExampleOfRunConflict(11)"
End Sub
Private Sub ExampleOfRunConflict(ByRef bytArg1 As Byte)
Dim strSelectedFilePath As String
Debug.Print vbNewLine & Now() & ": About to call GetSingleFile_FullPath"
strSelectedFilePath = GetSingleFile_FullPath("Excel Workbook" & ", *.XLS*", "Select an Excel workbook", False)
Debug.Print vbNewLine & Now() & " wb = " & strSelectedFilePath
Debug.Print vbNewLine & Now() & " WB Len = " & Len(strSelectedFilePath)
If Not Len(strSelectedFilePath) > 1 Then
MsgBox "No file selected"
Else
MsgBox "Success! The rest of the code will run now"
End If
End Sub
Private Function GetSingleFile_FullPath(Optional ByRef FileFilter As String, _
Optional ByRef Title As String, _
Optional ByRef ReturnFileNameOnly As Boolean) As String
Dim FilePath As Variant
If Application.ScreenUpdating = False Then Application.ScreenUpdating = True
If Len(FileFilter) > 0 Then 'file filter argument passed
Debug.Print vbNewLine & Now() & " About to GetOpenFileName"
Debug.Print "FileFilter = " & FileFilter
Debug.Print "Title = " & Title
Debug.Print "ReturnFileNameOnly = " & ReturnFileNameOnly
FilePath = Application.GetOpenFilename(FileFilter:=FileFilter, Title:=Title, MultiSelect:=False)
Else 'no filter/allow all files
FilePath = Application.GetOpenFilename(Title:=Title, MultiSelect:=False)
End If
Debug.Print "FilePath " & FilePath
If FilePath = False Then Exit Function 'user cancelled
GetSingleFile_FullPath = FilePath
FilePath = vbNull
If ReturnFileNameOnly = True Then
GetSingleFile_FullPath = Mid(String:=ReturnFileNameOnly, Start:=InStrRev(GetSingleFile_FullPath, Application.PathSeparator) + 1)
End If
End Function
Bookmarks