How to make Excel VBA Export as PDF on MAC computer

只愿长相守 提交于 2021-01-28 11:09:09

问题


I have a macro code for my Excel document. I want to export the active sheet as a PDF file to the specific folder. Same code working on my Windows PC, but it doesn't work on my MAC pc.When I click the Save button, it's printing the pdf instead save it. I want to save it. If anybody can help me I will be very glad.

Sub Save_NEWPORT_ESTIMATE()

If Range("G1") = "INVOICE" Then

LigneIS = Application.CountA(Sheets("Invoice summary").Range("A:A"))
Sheets("Invoice summary").Range("A" & LigneIS + 1) = Now
Sheets("Invoice summary").Range("B" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("H8")
Sheets("Invoice summary").Range("C" & LigneIS + 1) = "NANTUCKET"
Sheets("Invoice summary").Range("D" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("A12")
Sheets("Invoice summary").Range("E" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("M49")

Else
    LigneIS = Application.CountA(Sheets("Invoice summary").Range("A:A"))
    Sheets("Estimate summary").Range("A" & LigneIS + 1) = Now
    Sheets("Estimate summary").Range("B" & LigneIS + 1) = 
    Sheets("NANTUCKET ESTIMATE").Range("H8")
    Sheets("Estimate summary").Range("C" & LigneIS + 1) = "NANTUCKET"
    Sheets("Estimate summary").Range("D" & LigneIS + 1) = 
    Sheets("NANTUCKET ESTIMATE").Range("A12")
    Sheets("Estimate summary").Range("E" & LigneIS + 1) = 
    Sheets("NANTUCKET ESTIMATE").Range("M49")

End If
    D1 = Format(Date, "ddmmyy")
    Customer = Left(Range("A12"), 6)
    Job = Range("G12")
    Tipe = Range("G1")
    Model = Range("G18")

If Tipe = "INVOICE" Then
Tipe2 = "1 SALES INVOICES"
Else
Tipe2 = "1 ESTIMATES"
End If

Lien = "/Users/macbookpro/Desktop/INVOICE/" & Tipe2

ChDir "/Users/macbookpro/Desktop/INVOICE/" & Tipe2

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    Lien & "\" & D1 & " " & Model & "_" & Customer & "_" & Job & "_" & Tipe & ".pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

End Sub

Sub Save_NANTUCKET_ESTIMATE()

If Range("G1") = "INVOICE" Then
LigneIS = Application.CountA(Sheets("Invoice summary").Range("A:A"))
Sheets("Invoice summary").Range("A" & LigneIS + 1) = Now
Sheets("Invoice summary").Range("B" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("H8")
Sheets("Invoice summary").Range("C" & LigneIS + 1) = "NANTUCKET"
Sheets("Invoice summary").Range("D" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("A12")
Sheets("Invoice summary").Range("E" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("M49")
Else
LigneIS = Application.CountA(Sheets("Invoice summary").Range("A:A"))
Sheets("Estimate summary").Range("A" & LigneIS + 1) = Now
Sheets("Estimate summary").Range("B" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("H8")
Sheets("Estimate summary").Range("C" & LigneIS + 1) = "NANTUCKET"
Sheets("Estimate summary").Range("D" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("A12")
Sheets("Estimate summary").Range("E" & LigneIS + 1) = Sheets("NANTUCKET ESTIMATE").Range("M49")
End If

D1 = Format(Date, "ddmmyy")
Customer = Left(Range("A12"), 6)
Job = Range("G12")
Tipe = Range("G1")
Model = Range("G18")
If Tipe = "INVOICE" Then
Tipe2 = "1 SALES INVOICES"
Else
Tipe2 = "1 ESTIMATES"
End If

Lien = "/Users/macbookpro/Desktop/INVOICE/" & Tipe2

ChDir "/Users/macbookpro/Desktop/INVOICE/" & Tipe2

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    Lien & "\" & D1 & " " & Model & "_" & Customer & "_" & Job & "_" & Tipe & ".pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End Sub

回答1:


I am not sure if this request is still valid, but I found a solution on

http://www.rondebruin.nl/mac/mac005.htm

It appears that the export to PDF parameters react differently on mac than on windows PC and you need a workaround that saves the PDF into the users Library folder, specificially into the Office Library folder. The key function is this

    Function CreateFolderinMacOffice2016(NameFolder As String) As String
    'Function to create folder if it not exists in the Microsoft Office Folder
    'Ron de Bruin : 1-Feb-2019
    Dim OfficeFolder As String
    Dim PathToFolder As String
    Dim TestStr As String

    OfficeFolder = MacScript("return POSIX path of (path to desktop folder) as string")
    OfficeFolder = Replace(OfficeFolder, "/Desktop", "") & _
        "Library/Group Containers/UBF8T346G9.Office/"

    PathToFolder = OfficeFolder & NameFolder

    On Error Resume Next
    TestStr = Dir(PathToFolder & "*", vbDirectory)
    On Error GoTo 0
    If TestStr = vbNullString Then
        MkDir PathToFolder
        'You can use this msgbox line for testing if you want
        'MsgBox "You find the new folder in this location :" & PathToFolder
    End If
    CreateFolderinMacOffice2016 = PathToFolder
End Function

Creating a folder using this function and directly using the returning value as file path will make it work.

All credits to Ron de Bruin, I just copied this to help out. Visit his page for further examples.



来源:https://stackoverflow.com/questions/56552181/how-to-make-excel-vba-export-as-pdf-on-mac-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!