【问题标题】:Return if file exists VBA Mac Office 2011如果文件存在则返回 VBA Mac Office 2011
【发布时间】:2011-05-10 18:57:24
【问题描述】:

我正在尝试使用 Mac Office 2011 上的 VBA 测试文件是否存在。

我的宏适用于 Office 2004,但不适用于 2011。

我正在使用 Dir 函数。如果函数什么都不返回,这意味着文件不存在。但是在 Office 2011 中,当文件不存在时,该函数会返回错误代码 76。

【问题讨论】:

    标签: macos vba file-io ms-office


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

      答案 1 中的解决方案应该有效。但是,任何时候遇到困扰 Mac Excel 2011 的截断文件名问题时都会失败 - 即使应用了 SP1。

      Microsoft 上描述了基本问题 http://answers.microsoft.com/en-us/mac/forum/macoffice2011-macexcel/help-xl-2011s-dir-function-truncates-filename/e72fbf5d-749c-4a55-a77c-e2def6db24d9?msgId=644b9f20-251b-46fe-8df3-f5a28a1c37f6

      并且FileSystem对象与使用VBA原生Dir函数有同样的病。

      换句话说,它返回的文件名与 Dir 返回的相同,因此您实际上无法确定使用 VBA 是否存在具有长名称的文件,即在 Finder 列表中实际显示给用户的名称在 Excel 2011 SP1 之后,无需借助 AppleScripting!

      【讨论】:

      • 仍然是 SP2 (14.2.3) 的问题
      【解决方案3】:

      更好的答案在 msft 网站上:http://msdn.microsoft.com/en-us/library/office/jj614412(v=office.14).aspx

      例如:

      Sub TestFile()
      'First argument, 1 = file and 2 = folder.
      'Note: This macro uses the FileOrFolderExistsOnMac function.
          If FileOrFolderExistsOnMac(1, "Macintosh HD:Users:<user name>:Documents:YourFileName.xlsx") = True Then
              MsgBox "File exists."
          Else
              MsgBox "File does not exist."
          End If
      End Sub
      
      Sub TestFolder()
      'First argument, 1 = file and 2 = folder.
      'Note: This macro uses the FileOrFolderExistsOnMac function.
          If FileOrFolderExistsOnMac(2, "Macintosh HD:Users:<user name>:Documents") = True Then
              MsgBox "Folder exists."
          Else
              MsgBox "Folder does not exist."
          End If
      End Sub
      
      Function FileOrFolderExistsOnMac(FileOrFolder As Long, FileOrFolderstr As String) As Boolean
      'By Ron de Bruin
      '30-July-2012
      'Function to test whether a file or folder exist on a Mac.
      'Uses AppleScript to avoid the problem with long file names.
          Dim ScriptToCheckFileFolder As String
          ScriptToCheckFileFolder = "tell application " & Chr(34) & "Finder" & Chr(34) & Chr(13)
          If FileOrFolder = 1 Then
              ScriptToCheckFileFolder = ScriptToCheckFileFolder & "exists file " & _
              Chr(34) & FileOrFolderstr & Chr(34) & Chr(13)
          Else
              ScriptToCheckFileFolder = ScriptToCheckFileFolder & "exists folder " & _
              Chr(34) & FileOrFolderstr & Chr(34) & Chr(13)
          End If
          ScriptToCheckFileFolder = ScriptToCheckFileFolder & "end tell" & Chr(13)
          FileOrFolderExistsOnMac = MacScript(ScriptToCheckFileFolder)
      End Function 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-02
        • 2011-05-05
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多