Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

3D Printing Service (https://www.javelin-tech.

com/3d/3d-printing-service/)
Website (https://www.javelin-tech.com/) Careers (https://www.javelin-tech.com/site/careers/)
Store (https://store.javelin-tech.com/) Contact Us (https://www.javelin-tech.com/3d/contact-us/)
 (https://solution.javelin-tech.com/facebook)  (https://solution.javelin-tech.com/twitter) 
(https://solution.javelin-tech.com/linkedin)  (https://solution.javelin-tech.com/youtube) 
(https://solution.javelin-tech.com/instagram)

(https://www.javelin-tech.com/blog/)

SOLIDWORKS VBA
Macro Code to Save
Drawing as a PDF
Article by Samony Riyaz (https://www.javelin-
tech.com/blog/author/samony-riyaz/) updated September 4,
2019
Usually when we want to speed up process tasks in
SOLIDWORKS, we tend to search for Macros that can do the
task for us so we can decrease our process time. A lot of
these Macros are published online by various users, but be
mindful as the code published is SPECIFIC to their machine
and environment and might not be the same for you.

Let’s take a look at one particular Macro — Save SOLIDWORKS Drawing


to PDF

 Modify the macro to integrate it within your process work ow


 Create a Macro button and a hotkey for the Macro

Sample Code:
Sub main()

Dim swApp As SldWorks.SldWorks


Dim swModel As SldWorks.ModelDoc2
Dim swExportPDFData As SldWorks.ExportPdfData
Dim strFilename As String
Dim status As Boolean
Dim errors As Long, warnings As Long

Set swApp = Application.SldWorks


Set swModel = swApp.ActiveDoc

'Save
status = swModel.Save3(swSaveAsOptions_e.swSaveAsOptions_Si

'Export to PDF if it is a drawing


If (swModel.GetType = swDocDRAWING) Then

strFilename = swModel.GetPathName
strFilename = Left(strFilename, Len(strFilename) - 6) & "pd
Set swExportPDFData = swApp.GetExportFileData(1)
swModel.Extension.SaveAs strFilename, 0, 0, swExportPDFData

End If

End Sub

The above code was altered so that along with saving the Drawing le,
it also exports a PDF of that drawing using the system default settings.

Integrate the Code:


Launch SOLIDWORKS and navigate to Tools > Macro > New
 This brings up the Folder browser dialog prompting you to save
the le.
— If this macro is being shared with others within your network,
make sure to save it in a Shared network location.

IF your coworkers are using a DIFFERENT version of


SOLIDWORKS, or a di erent installation directory – It
may cause reference errors
 Workaround: Send them the code, so they can create
the macro on their own local machine

 Once you have saved the macro, a Visual Basic for Applications
Dialog box will open with the default code below:

Dim swApp As Object


Sub main()

Set swApp = Application.SldWorks


End Sub

Replace the default code with the “Sample Code” shown above and
Save.

Congratulations! You have just created a usable macro from the


source code.

Running the Macro with a Quick Access Button:


Now to use the Save SOLIDWORKS Drawing to PDF macro e ciently
you will need to customize a Macro button/hotkey within
SOLIDWORKS:

1. Create a custom macro button


 Launch SOLIDWORKS and Create a New Drawing (New >
Drawing (use any available template))
 Right click on the Task bar > Customize > Commands Tab >
Select the Macro Category
 Drag the icon for macro button to a suitable location.
 Beside Macro Textbox, Click the browse button > select the
macro. If there is more than one method select the
appropriate method from the list. Click OK.
 Your new macro button is now usable.

2. Create a Keyboard shortcut


 Launch SOLIDWORKS and Create a New Drawing (New >
Drawing (use any available template))
 Right click on the Task bar > Customize > Keyboard Tab >
Scroll to the very bottom until you see the row for the Macro
button
 Press any combination you want, in this case we will do (Ctrl +
P) for Printing to PDF
 Click Yes to Override, Click OK
 Now Control P will save a PDF if a drawing le is open.


Posts related to 'SOLIDWORKS VBA Macro Code to Save
Drawing as a PDF'

(https://www.javelin- (https://www.javelin-
tech.com/blog/2020/11/solidworks-2021- tech.com/blog/2020/11/solidw
adds-vda-balloons/) macro-to-toggle-auto-rotate-vi
to-sketch-plane-on-sketch-crea
sketch-edit/)

SOLIDWORKS 2021 Adds VDA SOLIDWORKS Macro to to


Balloons “Auto-rotate view normal
sketch plane on sketch cr
and sketch edit”

Find Related Content by TAG:


Customize SOLIDWORKS (https://www.javelin-
tech.com/blog/tag/customize-solidworks/)

SOLIDWORKS Drawing (https://www.javelin-


tech.com/blog/tag/solidworks-drawing/)

SOLIDWORKS Keyboard Shortcuts (https://www.javelin-


tech.com/blog/tag/solidworks-keyboard-shortcuts/)

SOLIDWORKS Macro (https://www.javelin-


tech.com/blog/tag/solidworks-macro/)
← How to Align Breaks in a SOLIDWORKS Projected View with a Parent
View (https://www.javelin-tech.com/blog/2019/09/align-breaks-solidworks-
projected-view/)

How to manually launch a task in SOLIDWORKS PDM →


(https://www.javelin-tech.com/blog/2019/09/how-to-manually-launch-a-
task-in-solidworks-pdm/)

Want to gain SOLIDWORKS


Certi cation?
Take a SOLIDWORKS training course from Javelin and
get Certi ed in 2020

FIND A TRAINING COURSE (https://www.javelin-


tech.com/3d/people/training/)

Home (Https://Www.Javelin-Tech.Com/Blog/) › SOLIDWORKS Tech Tip


(Https://Www.Javelin-Tech.Com/Blog/Category/Tech-Tips/) ›
SOLIDWORKS VBA Macro Code To Save Drawing As A PDF

WHO WE ARE AND WHAT WE DO

We are Canadian experts in 3D and have enabled thousands of companies


to aim high with solutions for 3D Design (//www.javelin-
tech.com/3d/technology/), Data Management & Work ow (//www.javelin-
tech.com/3d/process/), Manufacturing & 3D Printing (//www.javelin-
tech.com/3d/manufacture/).

No matter the size of your business, we have the skills and experience to
propel your organization to new heights.

SUBSCRIBE TO OUR NEWSLETTER

Enter your Email Address SUBSCRIBE

 
NOTE: By subscribing you are granting permission to receive news and promotion
emails from us, unsubscribe at any time. View our privacy policy (//www.javelin-
tech.com/3d/privacy-policy)

© 1997 - 2020. All Rights Reserved by Javelin Technologies Inc.

Privacy Policy (https://www.javelin-tech.com/3d/privacy-policy) | Accessibility Policy


(https://www.javelin-tech.com/3d/accessibility-policy) | Terms of Use (https://www.javelin-
tech.com/3d/terms-of-use)
Call 877-219-6757 / 905-815-1906      (mailto:sales@javelin-tech.com)  
(https://solution.javelin-tech.com/facebook)  (https://solution.javelin-tech.com/twitter) 
(https://solution.javelin-tech.com/google+)  (https://solution.javelin-tech.com/linkedin) 
(https://solution.javelin-tech.com/youtube)  (https://solution.javelin-tech.com/blog) 
(https://solution.javelin-tech.com/instagram)

O ce Locations:  Ontario (//www.javelin-tech.com/3d/locations/oakville-ontario/) · Manitoba


(//www.javelin-tech.com/3d/locations/winnipeg-manitoba/) · Alberta (//www.javelin-
tech.com/3d/locations/calgary-alberta/) · British Columbia (//www.javelin-
tech.com/3d/locations/vancouver-british-columbia/)

You might also like