有Application.GetOpenFilename的Mac / PC的变种?(Is there

2019-09-28 10:52发布

是否有这样一个普遍的OS变种? 我期待有一个Mac和PC上运行此代码的工作,但这似乎只在PC上运行。

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

Answer 1:

它可以在MAC(EXCEL 2011)为好。 见截图

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

现在Application.GetOpenFilename肯定给在Mac的一个问题。 我会推荐看到这个线程也解决了你的问题。

在VBA用户窗体关闭已打开的工作簿时出错



Answer 2:

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

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

我希望这有帮助!



Answer 3:

请查看MSDN上的解决方案- 编程选择文件在Excel的Windows和Excel为Mac



文章来源: Is there a Mac/PC variant of Application.GetOpenFilename?