Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如果文件存在,返回VBA Mac Office 2011_Macos_Vba_File Io_Ms Office - Fatal编程技术网

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

Macos 如果文件存在,返回VBA Mac Office 2011,macos,vba,file-io,ms-office,Macos,Vba,File Io,Ms Office,我正在测试Mac Office 2011上是否存在带有VBA的文件 我的宏与Office 2004配合使用,但与2011不配合使用 我正在使用Dir函数。如果函数不返回任何内容,则表示该文件不存在。但在Office 2011中,当文件不存在时,函数返回错误代码76。您可以创建自己的函数来处理错误。例如,类似这样的事情: Function FileExists(ByVal AFileName As String) As Boolean On Error GoTo Catch Fi

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

我的宏与Office 2004配合使用,但与2011不配合使用


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

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

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

答案1中的解决方案应该有效。但是,当您遇到MacExcel2011中出现的文件名截断问题时,即使应用了SP1,也会失败

基本问题在Microsoft上描述

而且文件系统对象与使用VBA native Dir函数有相同的问题


换言之,它返回与Dir返回的相同的截断文件名,因此您无法使用Excel 2011 SP1后面的VBA来实际确定具有长名称的文件是否存在,而无需借助AppleScripting

更好的答案在msft网站上:

例如:

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 
子测试文件()
'第一个参数,1=文件,2=文件夹。
'注意:此宏使用FileOrFolderExistsSonmac函数。
如果FileOrFolderExistsSONMAC(1,“Macintosh HD:Users::Documents:YourFileName.xlsx”)=True,则
MsgBox“文件已存在。”
其他的
MsgBox“文件不存在。”
如果结束
端接头
子测试文件夹()
'第一个参数,1=文件,2=文件夹。
'注意:此宏使用FileOrFolderExistsSonmac函数。
如果FileOrFolderExistsSonmac(2,“Macintosh HD:Users::Documents”)=True,则
MsgBox“文件夹已存在。”
其他的
MsgBox“文件夹不存在。”
如果结束
端接头
函数FileOrFolderExistsOnMac(FileOrFolder为Long,FileOrFolderstr为String)为布尔值
“罗恩·德·布鲁因
'2012年7月30日
'函数测试Mac上是否存在文件或文件夹。
'使用AppleScript避免长文件名的问题。
Dim ScriptToCheckFileFolder作为字符串
ScriptToCheckFileFolder=“告诉应用程序”&Chr(34)&Finder&Chr(34)&Chr(13)
如果FileOrFolder=1,则
ScriptToCheckFileFolder=ScriptToCheckFileFolder&“存在文件”&_
Chr(34)和FileOrFolderstr&Chr(34)和Chr(13)
其他的
ScriptToCheckFileFolder=ScriptToCheckFileFolder&“存在文件夹”&_
Chr(34)和FileOrFolderstr&Chr(34)和Chr(13)
如果结束
ScriptToCheckFileFolder=ScriptToCheckFileFolder&“end tell”&Chr(13)
FileOrFolderExistsOnMac=MacScript(ScriptToCheckFileFolder)
端函数
SP2仍然存在问题(14.2.3)