是否有Application.GetOpenFilename的Mac / PC变种?

有没有一个通用的操作系统变种? 我正在寻找这个代码在Mac和PC上工作,但这似乎只能在PC上工作。

strFile = Application.GetOpenFilename("Text Files (.csv),.csv", , "Please selec text file...") 

它工作在MAC(Excel 2011)以及。 见屏幕截图

在这里输入图像描述

后续在聊天讨论,因为我怀疑错误不是与Application.GetSaveAsFilename但别的东西。 在这种情况下,它是Application.GetOpenFilename

现在Application.GetOpenFilename在Mac中肯定会出现问题。 我会build议看到这个线程,也解决您的问题。

在VBA Userform中closures打开的工作簿时出错

我发现我可以在PC和Mac上使用Application.GetSaveAsFileName而没有问题。

 FName = Application.GetSaveAsFilename(fileFilter:=filterString, InitialFileName:=myInitialFileName) 

但是我也发现Application.GetOpenFilename在Mac上不起作用,所以我做了一些Googlesearch,并在Mac上提出这个函数作为解决方法:

 #If Mac Then tempfnameList = Select_File_Or_Files_Mac() #Else tempfnameList = Application.GetOpenFilename(fileFilter:=filterString, Title:="Select File(s) to Open", MultiSelect:=True) #End If 

这里是Select_File_Or_Files_Mac的实现:

 Function Select_File_Or_Files_Mac() As String() Dim MyPath As String Dim MyScript As String Dim MyFiles As String Dim MySplit As Variant Dim N As Long Dim FName As String Dim mybook As Workbook On Error Resume Next MyPath = MacScript("return (path to documents folder) as String") 'Or use MyPath = "Macintosh HD:Users:Ron:Desktop:TestFolder:" ' In the following statement, change true to false in the line "multiple ' selections allowed true" if you do not want to be able to select more ' than one file. Additionally, if you want to filter for multiple files, change ' {""com.microsoft.Excel.xls""} to ' {""com.microsoft.excel.xls"",""public.comma-separated-values-text""} ' if you want to filter on xls and csv files, for example. MyScript = _ "set applescript's text item delimiters to "","" " & vbNewLine & _ "set theFiles to (choose file of type " & _ " {""public.comma-separated-values-text""} " & _ "with prompt ""Please select a file or files"" default location alias """ & _ MyPath & """ multiple selections allowed true) as string" & vbNewLine & _ "set applescript's text item delimiters to """" " & vbNewLine & _ "return theFiles" MyFiles = MacScript(MyScript) Dim returnList() As String On Error GoTo 0 If MyFiles <> "" Then With Application .ScreenUpdating = False .EnableEvents = False End With 'MsgBox MyFiles MySplit = Split(MyFiles, ",") ReDim returnList(LBound(MySplit) To UBound(MySplit)) For N = LBound(MySplit) To UBound(MySplit) returnList(N) = MySplit(N) Next N With Application .ScreenUpdating = True .EnableEvents = True End With Select_File_Or_Files_Mac = returnList Else ReDim returnList(0 To 0) returnList(0) = "False" Select_File_Or_Files_Mac = returnList End If End Function 

我希望这有帮助!

在MSDN上查看解决scheme – 以编程方式在Mac上为Excel和Excelselect文件