Excel VBA code to work on Mac, Create PDF Function

前端 未结 2 669
清歌不尽
清歌不尽 2021-01-24 12:04

I have coded the following function. However, I cannot get it to work on office Mac. I am not sure of the procedure to find the EXP_PDF.DLL mac equivalent

Funct         


        
相关标签:
2条回答
  • 2021-01-24 12:10

    Here is a guide on how you can do it in newer versions of Mac Excel: https://www.rondebruin.nl/mac/mac034.htm

    What's important is that you can't save your file to a location that is selected by you.

    It has to be saved to the folder Library/Group Containers/UBF8T346G9.Office under the current user's home dir, so /Users/[current user]/Library/Group Containers/UBF8T346G9.Office in most of the cases. If the folder is not there, you have to create it. (See the code example on the page linked above)

    Kudos to Ron!

    Please vote for this to be fixed by MS here: https://excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method

    0 讨论(0)
  • 2021-01-24 12:21

    There is no need to check for the existence of that specific DLL, because under MacOS, PDF export support is native. Your code simply works if you remove the Add-in check and remove the FileFilter string:

    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
    

    But GetSaveAsFilename is crippled on MacOS and does not allow filtering files by filetype. If you need to restrict users to a certain filetype, you can resort to AppleScript and do the following:

    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 to run the appropriate function for each OS:

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

    Given the limitations of default VB functions in MacOS, this is Microsof't suggested method as well.

    0 讨论(0)
提交回复
热议问题