【问题标题】:Is there a Mac/PC variant of Application.GetOpenFilename?是否有 Application.GetOpenFilename 的 Mac/PC 变体?
【发布时间】:2021-12-06 02:31:40
【问题描述】:

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

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

【问题讨论】:

    标签: excel vba macos


    【解决方案1】:

    我发现我可以在 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
    

    我希望这会有所帮助!

    【讨论】:

    • 2021 年,在新版本的 Excel / 新 macbook 上,我仍然必须这样做,但这是唯一有效的解决方案!要成为 VBA 新手,只需创建一个新模块并将上述函数复制到其中即可。然后用 IF Then 代码行修改你的其他代码,但没有 #
    【解决方案2】:

    它也适用于 MAC (Excel 2011)。查看屏幕截图

    跟进 在聊天中讨论后,我怀疑错误不是Application.GetSaveAsFilename,而是其他问题。在这种情况下是Application.GetOpenFilename

    现在Application.GetOpenFilename 在 Mac 中肯定会出现问题。我建议您查看此主题,它也可以解决您的问题。

    Error when closing an opened workbook in VBA Userform

    【讨论】:

    • 您收到了什么错误信息? Excel 2011 是否更新了最新的服务包(尽管我怀疑这会是原因)?
    • 更新了,在NewFile = Application.GetSaveAsFilename(InitialFileName:="Open Order Log - " &amp; Format(Date, "dd-mm-yyyy") &amp; ".xlsx", fileFilter:=NewFileType)这一行报错Run-time error '1004': Method: 'GetSaveAsFilename' of object '_Application' failed
    • Ok 什么时候出现错误?那条线是executed的那一刻?或者当您按cancel 或当您按save 时?你能告诉我们完整的子代码吗?
    • 或者更好的是,您还可以上传您的文件吗?我可以在 MAC 中测试它
    • kaboomlabs.com/excel/OORv14.xlsm 这只会持续几分钟......因为这是我来这里以来所做的很多工作。话虽如此,请单击“作业列表”选项卡,然后单击“更新 PO 跟踪”。
    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2019-09-27
      • 2019-06-15
      相关资源
      最近更新 更多