vba 是否有 Application.GetOpenFilename 的 Mac/PC 变体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12263016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a Mac/PC variant of Application.GetOpenFilename?
提问by Matt Ridge
Is there a universal OS variant of this? I am looking to have this code work on a Mac and PC, but this seems only to work on a PC.
是否有通用的操作系统变体?我希望这段代码可以在 Mac 和 PC 上运行,但这似乎只能在 PC 上运行。
strFile = Application.GetOpenFilename("Text Files (.csv),.csv", , "Please selec text file...")
采纳答案by Siddharth Rout
It works in MAC (Excel 2011) As well. See screen shot
它也适用于 MAC(Excel 2011)。查看屏幕截图
FOLLOWUPUpon discussion in chat as I suspected the error was not with Application.GetSaveAsFilename
but something else. In this case it was Application.GetOpenFilename
跟进在聊天中讨论后,我怀疑错误不是与Application.GetSaveAsFilename
其他原因有关。在这种情况下是Application.GetOpenFilename
Now Application.GetOpenFilename
definitely gives a problem in Mac. I would recommend seeing this thread which also addresses your problem.
现在Application.GetOpenFilename
肯定会在 Mac 中出现问题。我建议您查看此线程,该线程也可以解决您的问题。
回答by jrwagz
I find that I am able to use Application.GetSaveAsFileName
on both PC and Mac without issue.
我发现我可以Application.GetSaveAsFileName
在 PC 和 Mac 上正常使用。
FName = Application.GetSaveAsFilename(fileFilter:=filterString, InitialFileName:=myInitialFileName)
However I also found that Application.GetOpenFilename
does not work on the Mac, so I did some googling and came up with this function as a workaround on the Mac:
但是我也发现它Application.GetOpenFilename
在 Mac 上不起作用,所以我做了一些谷歌搜索并想出了这个功能作为 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
Here is the implementation of Select_File_Or_Files_Mac
:
下面是实现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
I hope this helps!
我希望这有帮助!
回答by JeeShen Lee
Check out the solution on MSDN - Programmatically Selecting Files in Excel for Windows and Excel for the Mac
查看 MSDN 上的解决方案 - Programmatically Selecting Files in Excel for Windows 和 Excel for the Mac