Forum Discussion

EmiAle79's avatar
EmiAle79
Copper Contributor
May 22, 2023
Solved

Print with VBA PC vs. MAC

I wrote the code below to be able to print all tabs to PDF except 1,4,5 and 6.

In my PC it works perfectly, but when I sent it to a MAC it creates an error.

what should I change in the code so it will work in MAC computer?

PS: we both have excel 365

 

Sub SavetoPDF()

 

Application.ScreenUpdating = False

 

Dim ws As Worksheet
Dim excludedSheets As Variant
Dim tempWorkbook As Workbook

Set tempWorkbook = Workbooks.Add ' Create a temporary workbook

excludedSheets = Array("Summary", "Trucks", "Crews", "Circuits")

' Copy all sheets except excluded ones to the temporary workbook
For Each ws In ThisWorkbook.Sheets
If IsError(Application.Match(ws.Name, excludedSheets, 0)) Then
ws.Copy After:=tempWorkbook.Sheets(tempWorkbook.Sheets.Count)
End If
Next ws

' Set the page setup for the selected sheets in the temporary workbook
For Each ws In tempWorkbook.Sheets
With ws.PageSetup
.PaperSize = xlPaperLetter
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Next ws

' Print to PDF from the temporary workbook
tempWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\tempo.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True

' Close and delete the temporary workbook
tempWorkbook.Close SaveChanges:=False
Set tempWorkbook = Nothing

Application.ScreenUpdating = True
End Sub

 

 

  • EmiAle79 

    The code you provided should at first glance work on both PC and Mac as it uses standard VBA syntax and does not contain any platform specific commands or functions.

    However, there is one part of the code that could potentially cause an error on a Mac due to differences in file path syntax.

    In the line where you specify the PDF filename, you are currently using a Windows file path format:

    Filename:="C:\tempo.pdf"

    On a Mac, the file path should use the forward slash (/) as the path separator and the appropriate Mac file system syntax. You can modify the file path to work on both PC and Mac by using the Application.PathSeparator property, which automatically adapts to the current platform.

    Here is an updated version of that line:

    Filename:=Application.DefaultFilePath & Application.PathSeparator & "tempo.pdf"

    This will use the default file path on the user's machine and append the filename "tempo.pdf" to it, using the appropriate path separator for the platform.

    With this modification, the code theoretically should work on both PC and Mac without any issues.

    Theoretically, because I do not have a Mac and can try it out.

3 Replies

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    EmiAle79 

    The code you provided should at first glance work on both PC and Mac as it uses standard VBA syntax and does not contain any platform specific commands or functions.

    However, there is one part of the code that could potentially cause an error on a Mac due to differences in file path syntax.

    In the line where you specify the PDF filename, you are currently using a Windows file path format:

    Filename:="C:\tempo.pdf"

    On a Mac, the file path should use the forward slash (/) as the path separator and the appropriate Mac file system syntax. You can modify the file path to work on both PC and Mac by using the Application.PathSeparator property, which automatically adapts to the current platform.

    Here is an updated version of that line:

    Filename:=Application.DefaultFilePath & Application.PathSeparator & "tempo.pdf"

    This will use the default file path on the user's machine and append the filename "tempo.pdf" to it, using the appropriate path separator for the platform.

    With this modification, the code theoretically should work on both PC and Mac without any issues.

    Theoretically, because I do not have a Mac and can try it out.

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor
        I am glad that I was able to help you with your project.
        I wish you continued success with Excel!

Resources