简体   繁体   中英

Excel VBA code to work on Mac, Create PDF Function

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM