如果文件存在,则返回VBA Mac Office 2011

ohm*_*mer 4 macos file-io vba ms-office

我正在尝试测试Mac Office 2011上是否存在VBA文件.

我的宏正在使用Office 2004但不适用于2011.

我正在使用Dir功能.如果函数不返回任何内容,则表示该文件不存在.但是对于Office 2011,该函数在文件不存在时返回错误代码76.

小智 5

您可以创建自己的函数来处理错误.例如,像这样:

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
Run Code Online (Sandbox Code Playgroud)