Description
The script will automatically rebuild all SolidWorks drawing files (.SLDDRW) in a user-specified Windows directory, assuring every view and annotation is up to date, and will immediately save each as a PDF file.
System Requirements
- SolidWorks Version: SolidWorks 2014 or newer
- Operating System: Windows 10 or later
- Excel Version: Microsoft Excel 2010 or later (for Excel integration features)
Pre-requisites
- The macro code must have the absolute file path of the folder that contains the SolidWorks drawings before running the macro.
- SolidWorks needs to be installed and open on the machine in order to run the macro.
Results
- For each original SolidWorks drawing processed, a PDF file is created.
- PDF files are saved in the same directory as their SolidWorks drawing source files.
- The original files (.SLDDRW) will be opened, rebuilt, and closed, with no changes.
Steps to Set Up the Macro
- Register the Macro File: Start with SolidWorks open and go to Tools>Macro>New.
- Name and Register: Give the macro a meaningful name (e.g., BatchPDFExport.swp) and save the file. This step will open your VBA editor automatically.
- Paste the code: Download the VBA code we have provided below and place it in your editor.
- Define Target Folder (Important step): In the VBA editor, in the Sub Main() sub-routine, replace the file path in the code line:
RebuildAndSaveAllDrawingsAsPDF “C:\SOLIDWORKS”, “.SLDDRW”, True
Replace “C:\SOLIDWORKS” with the target folder where your drawing files are located. This SOLIDWORKS macro to batch rebuild and export drawings to PDF helps streamline this process by automating rebuild and export tasks for all drawings within the specified folder.
- Run Macro: Close your VBA editor and save the work when prompted. You can now run the macro with Tools>Macro>Run or an assigned shortcut.
VBA Macro Code
Visual Basic
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
' Disclaimer: ' The code provided should be used at your own risk. ' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code. ' For more information, visit [Blue Byte Systems Inc.](https://bluebyte.biz). Option Explicit ' Define the main variables for SolidWorks application and model Dim swApp As SldWorks.SldWorks Dim swModel As ModelDoc2 Dim swFilename As String Dim swRet As Boolean Dim swErrors As Long Dim swWarnings As Long Dim swResponse As String ' Main subroutine Sub Main() ' Initialize SolidWorks application Set swApp = Application.SldWorks ' Specify the folder location containing the SolidWorks drawings RebuildAndSaveAllDrawingsAsPDF "C:\SOLIDWORKS", ".SLDDRW", True End Sub ' Subroutine to rebuild drawing and save as PDF Sub RebuildAndSaveAllDrawingsAsPDF(swFolder As String, swExt As String, swSilent As Boolean) Dim swDocTypeLong As Long ' Ensure the file extension is in uppercase swExt = UCase$(swExt) swDocTypeLong = Switch(swExt = ".SLDDRW", swDocDRAWING, True, -1) ' If the file type is not supported, exit the subroutine If swDocTypeLong = -1 Then Exit Sub End If ' Change directory to the folder ChDir (swFolder) ' Get the first file in the folder swResponse = Dir(swFolder) Do Until swResponse = "" swFilename = swFolder & swResponse ' Check if the file extension matches If Right(UCase$(swResponse), 7) = swExt Then ' Open the SolidWorks document Set swModel = swApp.OpenDoc6(swFilename, swDocTypeLong, swOpenDocOptions_Silent, "", swErrors, swWarnings) ' Rebuild the drawing if it's a drawing file If swDocTypeLong <> swDocDRAWING Then swModel.ShowNamedView2 "*Isometric", -1 End If ' Define file path variables Dim swFilePath As String Dim swPathSize As Long Dim swPathNoExtension As String Dim swNewFilePath As String swFilePath = swModel.GetPathName swPathSize = Strings.Len(swFilePath) swPathNoExtension = Strings.Left(swFilePath, swPathSize - 6) swNewFilePath = swPathNoExtension & "PDF" ' Save the document as PDF swRet = swModel.SaveAs3(swNewFilePath, 0, 0) ' Close the document swApp.CloseDoc swModel.GetTitle End If ' Get the next file in the folder swResponse = Dir Loop End Sub |
Macro
You can download the macro from here.
Need Customization & Expert Support?
If so, make sure to reach out! Our specialized team at Blue Byte Systems Inc. develops custom macros and add-ins to meet these advanced workflow needs.
