SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Programming and macros
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 377
x 136

SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by CarrieIves »

We have a Save as PDF macro we run from SolidWorks.

If I were to manually save as PDF, I can adjust the check box "View PDF after saving". Is there an API call for SolidWorks that can toggle this?
image.png
image.png (8.27 KiB) Viewed 2408 times
Also, the description property from SolidWorks shows here. I don't know that the information gets saved into the PDF. Can we include that information in the PDF?
User avatar
AlexB
Posts: 501
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 269
x 445

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by AlexB »

Take a look at this interface. It allows you to set the "View after save" option for PDFs.

https://help.solidworks.com/2021/englis ... fData.html
User avatar
SPerman
Posts: 2056
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2227
x 1878
Contact:

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by SPerman »

My "Save as PDF" macro uses the "SaveAs3" method. This appears to be a rather generic "Save As" where the output is based on the file extension.

There is, however, a different method that deals specifically with PDF's and allows you to set the above mentioned switch.

https://help.solidworks.com/2023/englis ... 77a130#Pg0

It looks like AlexB beat me to it.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 377
x 136

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by CarrieIves »

So, I have been trying to rework my regular save as PDF macro (not what is used as a PDM task) to follow the example. I want to always save all sheets. The number of sheets varies by drawing.

So, my macro did use:

Code: Select all

swModel.SaveAs (fullfilename)
fullfilename is derived from properties in the model and placed in a specific folder

It looks like I should be doing something like this:

Code: Select all

boolstatus = swModelDocExt.SaveAs(fullfilename, 0, 0, swExportPDFData, lErrors, lWarnings)
What I'm not getting right is the swExportPDFData. Which I think is supposed to be the sheets we want to export.

From the help here:
https://help.solidworks.com/2023/englis ... ort_e.html
image.png
So, I tried putting in 1 to get all sheets exported but got an error "Type mis-match"

The examples I have seen look like they are putting the sheets into an array, but the examples I am looking at are not clear.

Thanks,
Carrie
User avatar
gupta9665
Posts: 413
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 435
x 452

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by gupta9665 »

Add the below line above boolstatus = swModelDocExt.SaveAs(Full File Name Here with extension, 0, 0, swExportPdfData, lErrors, lWarnings)

Dim swExportData As SldWorks.ExportPdfData
Set swExportData = swApp.GetExportFileData(swExportPdfData)
swExportData.ViewPdfAfterSaving = False
CarrieIves wrote: Mon Dec 04, 2023 12:29 pm Also, the description property from SolidWorks shows here. I don't know that the information gets saved into the PDF. Can we include that information in the PDF?
In the PDF file name or in PDF document properties?
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
SPerman
Posts: 2056
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2227
x 1878
Contact:

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by SPerman »

Have you tried entering swExportData_ExpoprtAllSheets instead of 1?
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 377
x 136

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by CarrieIves »

@SPerman - I tried your suggestion of entering swExportData_ExpoprtAllSheets instead of 1. I got runtime error '424' "Object required"

@gupta9665 - I updated my macro to what I think you suggested and got error '438' "Object doesn't support this property or method" in the Set swExportData line. Here's the snippet from that part of the macro.

Code: Select all

If savefile Then
    Dim swExportData As SldWorks.ExportPdfData
    Set swExportData = swApp.GetExportFileData(swExportPDFData)
    swExportData.ViewPdfAfterSaving = False
    boolstatus = swModelDocExt.SaveAs(fullfilename, 0, 0, swExportData_ExpoprtAllSheets, lErrors, lWarnings)
    SHAddToRecentDocs Path, fullfilename
End If
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 377
x 136

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by CarrieIves »

@gupta9665 It would be nice to save the SolidWorks properties into the PDF, but I'm not sure how to see the PDF properties.
User avatar
AlexB
Posts: 501
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 269
x 445

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by AlexB »

@CarrieIves here's a minimal example of what to do to export your sheets. If you are specifying sheet names then un-comment the lines that have vSheets in it.

Code: Select all

Option Explicit

Dim swApp As SldWorks.SldWorks
Sub main()
    Dim swModel As ModelDoc2
    Dim swDraw As DrawingDoc
    Dim swModelDocExt As ModelDocExtension
    Dim boolStatus As Boolean
    Dim lErrors As Long
    Dim lWarnings As Long
    'Dim vSheets As Variant
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swModelDocExt = swModel.Extension
    
    Dim swExportPDFData As ExportPdfData
    Set swExportPDFData = swApp.GetExportFileData(swExportDataFileType_e.swExportPDFData) 'Argument is integer 1
    
    'vSheets = swDraw.GetSheetNames
    swExportPDFData.ViewPdfAfterSaving = False
    'boolStatus = swExportPDFData.SetSheets(swExportDataSheetsToExport_e.swExportData_ExportSpecifiedSheets, vSheets)
    'Debug.Print boolStatus
    
    boolStatus = swModelDocExt.SaveAs(Environ("USERPROFILE") & "\Desktop\test.pdf", 0, 0, swExportPDFData, lErrors, lWarnings)
    Debug.Print boolStatus & "  " & lErrors & "  " & lWarnings
    
End Sub

Edit: To answer your question about variables on your PDF file. Since you're on PDM Standard, your best bet of setting these yourself is to get creative with Dispatch since you don't have access to the PDM API. This assumes that Dispatch is available with PDM Standard but I can't recall right now if that's the case.
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 377
x 136

Re: SolidWorks Save as PDF macro - description and toggle of "View PDF after Saving"

Unread post by CarrieIves »

@AlexB Thank you for the example. I don't know what I had wrong in my macro, but was able to finally step through and get it to line up with what you provided (with my filename and location adjustments)

What I am thinking of doing, is having all our users uncheck the "View PDF after saving" check box. For the PDFs that we make using the macro, I can have it set to view the PDF.

Though, I am pushing back some to my VAR.
Post Reply