【问题标题】:Excel VBA code to work on Mac, Create PDF FunctionExcel VBA 代码可在 Mac 上工作,创建 PDF 函数
【发布时间】:2015-02-14 22:03:18
【问题描述】:

我编写了以下函数。但是,我无法让它在 Office Mac 上运行。我不确定找到 EXP_PDF.DLL mac 等效项的过程

Function Create_PDF(Myvar As Object, FixedFilePathName As String, _                    
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String    

Dim FileFormatstr As String    
Dim FName As Variant

'Test If the Microsoft Add-in is installed    
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
        If FixedFilePathName = "" Then            
           'Open the GetSaveAsFilename dialog to enter a file name for the pdf            
           FileFormatstr = "PDF Files (*.pdf), *.pdf"            
           FName = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _                                                                                       Title:="Create PDF")
            'If you cancel this dialog Exit the function            
            If FName = False Then Exit Function        
         Else            
            FName = FixedFilePathName        
        End If
        'If OverwriteIfFileExist = False we test if the PDF        
         'already exist in the folder and Exit the function if that is True        
        If OverwriteIfFileExist = False Then            
            If Dir(FName) <> "" Then Exit Function        
        End If

       'Now the file name is correct we Publish to PDF        
       On Error Resume Next        
       Myvar.ExportAsFixedFormat _                
                Type:=xlTypePDF, _                
                FileName:=FName, _                
                Quality:=xlQualityStandard, _                
                IncludeDocProperties:=True, _                
                IgnorePrintAreas:=False, _                
                OpenAfterPublish:=OpenPDFAfterPublish        
        On Error GoTo 0

        'If Publish is Ok the function will return the file name        
         If Dir(FName) <> "" Then Create_PDF = FName    

End If
End Function

【问题讨论】:

    标签: excel macos vba pdf


    【解决方案1】:

    无需检查该特定 DLL 是否存在,因为在 MacOS 下,PDF 导出支持是原生的。如果您删除插件检查并删除 FileFilter 字符串,您的代码就可以正常工作:

    Function Create_PDF(Myvar As Object, FixedFilePathName As String, _
    OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
    
    Dim FileFormatstr As String
    Dim FName As Variant
    
        If FixedFilePathName = "" Then
           'Open the GetSaveAsFilename dialog to enter a file name for the pdf
           FName = Application.GetSaveAsFilename("", Title:="Create PDF")
            'If you cancel this dialog Exit the function
            If FName = False Then Exit Function
         Else
            FName = FixedFilePathName
        End If
        'If OverwriteIfFileExist = False we test if the PDF
         'already exist in the folder and Exit the function if that is True
        If OverwriteIfFileExist = False Then
            If Dir(FName) <> "" Then Exit Function
        End If
    
       'Now the file name is correct we Publish to PDF
       On Error Resume Next
       Myvar.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=FName, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=OpenPDFAfterPublish
        On Error GoTo 0
    
        'If Publish is Ok the function will return the file name
         If Dir(FName) <> "" Then Create_PDF = FName
    
    End Function
    

    GetSaveAsFilename 在 MacOS 上是 crippled,并且不允许按文件类型过滤文件。如果您需要将用户限制为某种文件类型,您可以使用 AppleScript 并执行以下操作:

    Function Create_PDF_Mac(Myvar As Object, FixedFilePathName As String, _
    OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
    
    Dim FileFormatstr As String
    Dim FName As Variant
    
        If FixedFilePathName = "" Then
           'Open the GetSaveAsFilename dialog to enter a file name for the pdf
           'FName = Application.GetSaveAsFilename("", ".PDF", Title:="Create PDF")
    
            On Error Resume Next
            ThePath = MacScript("return (path to documents folder) as String")
    
            TheScript = _
            "set applescript's text item delimiters to "","" " & vbNewLine & _
            "set theFile to (choose file name with prompt ""Save As File"" " & _
                "default name ""untitled.pdf"" default location alias """ & _
                ThePath & """ ) as string" & vbNewLine & _
            "if theFile does not end with "".pdf"" then set theFile to theFile & "".pdf"" " & vbNewLine & _
            "set applescript's text item delimiters to """" " & vbNewLine & _
            "return theFile"
    
               FName = MacScript(TheScript)
            On Error GoTo 0
    
            'If you cancel this dialog Exit the function
            If FName = False Then Exit Function
         Else
            FName = FixedFilePathName
        End If
        'If OverwriteIfFileExist = False we test if the PDF
         'already exist in the folder and Exit the function if that is True
        If OverwriteIfFileExist = False Then
            If Dir(FName) <> "" Then Exit Function
        End If
    
       'Now the file name is correct we Publish to PDF
       On Error Resume Next
       Myvar.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=FName, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=OpenPDFAfterPublish
        On Error GoTo 0
    
        'If Publish is Ok the function will return the file name
         If Dir(FName) <> "" Then Create_PDF = FName
    
    End Function
    

    The you can use an OS selector switch 为每个操作系统运行相应的功能:

    #If Mac Then
        savedFileName = Create_PDF_Mac(...)
    #Else
        savedFileName = Create_PDF_PC(...)
    #End If
    

    鉴于 MacOS 中默认 VB 函数的限制,这也是Microsof't suggested method

    【讨论】:

    • 谢谢。它几乎可以工作。发送我去执行函数。它带来的只是保存窗口。它不默认为 PDF,即使我选择 PDF 并保存它,也会出现编译运行计时器错误。我创建了使用该功能的按钮,但它失败了。 MAcro 编辑器引用错误的地方在最后一行。 If Dir(FName) "" Then Create_PDF = FName
    • 我基本上希望能够执行该功能,默认情况下,我在工作表中定义的选定区域应自动保存为 pdf。
    • 您使用的是哪个版本的 Excel for Mac?您是否尝试在调用 Create_PDF 时手动设置文件名?类似:CommandButton1.Caption = Create_PDF(Worksheets(1), "Internal:Users:Mahmood:Desktop:jambalaya.pdf", True, True)。最后一行还有错误吗?请注意,目录是用“:”而不是“/”分隔的。顺便说一句,最后一行只返回文件地址,因此您应该能够将整行替换为 Create_PDF = "" 以进行测试,并且它不应影响代码的主要功能。
    • GetSaveAsFilename 不支持在 MacOS 上按扩展名过滤文件。如果您需要包含用户,您可以使用 AppleScript 来完成(请参阅更新的代码)。上面的两个脚本在我的电脑(Yosemite + Office 2011)上运行良好。如果遇到错误,可能是您传递给函数的参数在其中起作用。否则代码似乎运行良好。
    【解决方案2】:

    以下是有关如何在较新版本的 Mac Excel 中执行此操作的指南:https://www.rondebruin.nl/mac/mac034.htm

    重要的是您不能将文件保存到您选择的位置。

    它必须保存到当前用户的主目录下的文件夹Library/Group Containers/UBF8T346G9.Office,所以在大多数情况下是/Users/[current user]/Library/Group Containers/UBF8T346G9.Office。如果文件夹不存在,则必须创建它。 (参见上面链接页面上的代码示例)

    向罗恩致敬!

    请在此处投票支持 MS 解决此问题:https://excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2018-07-12
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多