Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return if file exists VBA Mac Office 2011

I'm trying to test if a file exists with VBA on Mac Office 2011.

My macro is working with Office 2004 but doesn't work with 2011.

I'm using the Dir function. If the function returns nothing, this means the file doesn't exist. But with Office 2011, the function returns an error code 76 when the file doesn't exist.

like image 995
ohmer Avatar asked Jan 19 '23 19:01

ohmer


1 Answers

You can just create your own function that handles the error. For instance, something like this:

Function FileExists(ByVal AFileName As String) As Boolean
    On Error GoTo Catch

    FileSystem.FileLen AFileName

    FileExists = True

    GoTo Finally

    Catch:
        FileExists = False
    Finally:
End Function

Sub Test()
  If FileExists("Macintosh HD:Library:User Pictures:Flowers:Flower.tif") Then
    MsgBox "File exists"
  Else
    MsgBox "File doesn't exists"
  End If
End Sub
like image 164
Alexander de Man Avatar answered Jan 25 '23 20:01

Alexander de Man