返回文件是否存在 VBA Mac Office 2011

发布于 2024-11-06 03:03:01 字数 170 浏览 6 评论 0原文

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

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

我正在使用 Dir 函数。如果该函数没有返回任何内容,则意味着该文件不存在。但在 Office 2011 中,当文件不存在时,该函数会返回错误代码 76。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后咖啡店 2024-11-13 03:03:01

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

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

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
难理解 2024-11-13 03:03:01

答案 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 返回的相同的截断文件名,因此您实际上无法使用 Excel 2011 后面的 VBA 确定是否存在具有长名称的文件(该名称实际出现在 Finder 列表中的用户) SP1,无需借助 AppleScripting!

The solution in answer 1 should work. But will fail any time you encounter the truncated file name issue that plauges Mac Excel 2011 - even with SP1 applied.

The basic problem is described on 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

and the FileSystem object has the same disease as using the VBA native Dir function.

In other words it returns the same truncated file name that Dir returns, consequently you can't actually determine if a file with a long name, the name that actually appears to the user in a listing in the Finder, exists using VBA behind Excel 2011 SP1, without resorting to AppleScripting!

╭⌒浅淡时光〆 2024-11-13 03:03:01

更好的答案在 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 

Better answer is on msft website: http://msdn.microsoft.com/en-us/library/office/jj614412(v=office.14).aspx

For example:

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文