gpt4 book ai didi

excel - 是否有 Application.GetOpenFilename 的 Mac/PC 变体?

转载 作者:行者123 更新时间:2023-12-04 19:47:18 30 4
gpt4 key购买 nike

是否有通用的操作系统变体?我希望这段代码可以在 Mac 和 PC 上运行,但这似乎只能在 PC 上运行。

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

最佳答案

我发现我可以在 PC 和 Mac 上毫无问题地使用 Application.GetSaveAsFileName

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

但是我还发现 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

下面是 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

希望对您有所帮助!

关于excel - 是否有 Application.GetOpenFilename 的 Mac/PC 变体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263016/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com