Excel SaveAs功能在Microsoft PC上工作,但在MAC上不工作

Excel SaveAs功能在Microsoft PC上工作,但在MAC上不工作,excel,macos,vba,Excel,Macos,Vba,我有VBA代码,可以控制用户以.xls、.xlsm或.pdf以外的任何其他格式保存文件。这是为了防止在保存过程中剥离宏 我插入了一行代码来检查操作系统是否是OSx(…类似于“Mac”),它在其他宏中工作,但在这个宏中不工作。进程失败,突出显示“msoFileDialogSaveAs”,显示“找不到文件对象或库” 这是我的密码: Option Explicit Option Compare Text Private Sub Workbook_BeforeSave(ByVa

我有VBA代码,可以控制用户以.xls、.xlsm或.pdf以外的任何其他格式保存文件。这是为了防止在保存过程中剥离宏

我插入了一行代码来检查操作系统是否是OSx(…类似于“Mac”),它在其他宏中工作,但在这个宏中不工作。进程失败,突出显示“msoFileDialogSaveAs”,显示“找不到文件对象或库”

这是我的密码:

    Option Explicit
    Option Compare Text

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
        Cancel As Boolean)

      Dim fso As Object 'FileSystemObject
      Dim PdfSave As Boolean
      Dim SheetName As String
      If Not Application.OperatingSystem Like "*Mac*" Then
      SheetName = ActiveSheet.Name
      'Save-As action?
      If SaveAsUI Then
        Set fso = CreateObject("Scripting.FileSystemObject")
        'Abort excel's dialog
        Cancel = True
        'Create our own
        With Application.FileDialog(msoFileDialogSaveAs)
          'Select the XLSM filter by default
          .FilterIndex = 2
    Again:
          'Ok clicked?
          If .Show = -1 Then
            'Which extension should we save?
            Select Case fso.GetExtensionName(.SelectedItems(1))
              Case "xlsm"
                'Okay
              Case "xls"
                'Okay
              Case "pdf"
                PdfSave = True
                'Okay
              Case Else
                MsgBox "Invalid file type selected!" _
                  & vbCr & vbCr & "Only the following file formats are   permitted:" _
                  & vbCr & "   1. Excel Macro-Enabled Workbook (*.xlsm)" _
                  & vbCr & "   2. Excel 97-2003 Workbook (*.xls)" _
                  & vbCr & "   3. PDF (*.pdf)" _
                  & vbCr & vbCr & "Please try again." _
                  & vbCr & vbCr & "NOTE: 'Excel 97-2003 Workbook (*.xls)' format should be used for" _
                  & vbCr & "backwards compatability only!", vbOKOnly + vbCritical
                GoTo Again
            End Select
            'Prevent that we call ourself
            Application.EnableEvents = False
            'Save the file
            If PdfSave = True Then
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,   Filename:=ActiveWorkbook.Path & "\" & SheetName & ".pdf",  Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            Else
                ThisWorkbook.SaveAs .SelectedItems(1)
            End If
            Application.EnableEvents = True
          End If
        End With
      End If
      End If
    End Sub
任何人都可以提出修改建议,使此代码在PC和MAC上都适用于Office,或者使用不同的代码实现相同的功能

谢谢


迈克

在Mac和PC环境下工作,你正处于地图的边缘,我必须做很多工作,这是肯定的!我的建议是坚持下去,你走对了路

首先,我有一个类似的操作系统检查:-

BlnIsAPC = IIf(Left(Trim(UCase(Application.OperatingSystem)), 1) = "M", False, True)
这仅仅是试图用最经得起未来考验的方法来获得正确的操作系统

其次,很好,您延迟绑定到
Scripting.FileSystemObject
,因为它不在Mac Office中(它是Windows而不是Office的一部分)

第三,
FileDialog
,因此错误“找不到文件对象或库”。还有一种选择,你最终需要参考一下。它是一个内置函数,名为

您需要了解如何在AppleScript中执行该操作,然后创建该脚本并在VBA中通过MacScript运行它。下面是我工作的一个简单示例,其中的代码要么在PC上使用
Application.FileDialog(msoFileDialogOpen)
,要么在Mac上使用
MacScript
,具体来说,这只是显示Mac端

Public Function GetFilePath(ByVal StrTitle As String, ByVal StrButtonName As String, ByVal BlnMultiSelect As Boolean, ByVal StrFilters As String) As String
'               StrTitle        = The title to go on the dialog box
'               StrButtonName   = What to show on the OK button
'               BlnMultiSelect  = Can the user select more than one file
'               StrFilters      = What can be selected pipe and colon delimited i.e. [name]:[suffix]|[name]:[suffix]

If Procs.Global_IsAPC Then
    GetFilePath = GetFilePath_PC(StrTitle, StrButtonName, BlnMultiSelect, StrFilters)
Else
    GetFilePath = GetFilePath_Mac(StrTitle, StrButtonName, BlnMultiSelect, StrFilters)
End If

End Function

Private Function GetFilePath_PC(ByVal StrTitle As String, ByVal StrButtonName As String, ByVal BlnMultiSelect As Boolean, StrFilters As String) As String
...
End Function

Private Function GetFilePath_Mac(ByVal StrTitle As String, ByVal StrButtonName As String, ByVal BlnMultiSelect As Boolean, StrFilters As String) As String
Dim AryTemp2()      As String
Dim LngCounter      As Long
Dim StrContainer    As String
Dim StrPath         As String

StrContainer = "tell application " & """" & "Finder" & """" & Chr(13)
StrContainer = StrContainer & "choose file with prompt " & """" & StrTitle & """"

If StrFilters <> "" Then
    StrContainer = StrContainer & " of type {"
    'Code was here that prepared the filters into AryTemp2 
    For LngCounter = 0 To UBound(AryTemp2, 1)
        If Right(StrContainer, 1) <> "{" Then StrContainer = StrContainer & ", "
        StrContainer = StrContainer & """" & AryTemp2(LngCounter2) & """"
    Next
    StrContainer = StrContainer & "} " 
End If

StrContainer = StrContainer & "without invisibles" & IIf(BlnMultiSelect, "", " and multiple selections") & " allowed" & Chr(13)
StrContainer = StrContainer & "end tell"
StrPath = MacScript(StrContainer)

If Left(StrPath, 6) = "alias " Then StrPath = Right(StrPath, Len(StrPath) - 6)

GetFilePath_Mac = StrPath

End Function

最后,VBA并不是在所有版本的Office for Mac上都可用,它们之间的工作方式存在细微的差异,不幸的是,只有通过体验才能发现这些差异。就像我说的“你正驶离地图的边缘”进入未知水域。

查看此处的评论:。似乎你在FileDialog上运气不佳。嗨,Gary,谢谢你的评论和帮助。我决定退后一步,继续使用古老的“吻”这个词——保持简单愚蠢。在这一点上,我意识到OSx的VBA不起作用;I don’我不喜欢ActiveX,我调回了Excel中所有我喜欢的东西(如切换按钮等),并保留了基本的Excel函数和表单。通过这种方式,我现在在两个平台上都有了一个完整的电子表格版本。。。离开地图。。。我已经过了边缘,即将被遗忘…@GaryEvans我有一个类似的问题,它适用于Office Mac 2011,但您提到的链接提到,此功能现在已被弃用。他们建议使用:
applescriptask
。你有什么建议对这两个版本都适用吗?还有,当用户没有选择文件并处理它时,如何避免错误?@david Leal,恐怕我已经摆脱了跨平台的麻烦海洋!如果建议使用
applescriptask
,如果您对正在运行的版本有信心,请使用它,否则您可能需要一种方法来决定使用哪个命令才能向后兼容。当用户未选择任何内容时,请使用错误句柄(
On error goto error handle
error handle:
)或检查返回值是否为空并停止处理。
tell application "Finder"
choose file with prompt "Select the required Config stub" of type {"Config_Stub"} without invisibles and multiple selections allowed
end tell