Description
This macro rebuilds all SolidWorks drawing files (.SLDDRW) in a specified directory and saves them as PDF files in the same location. It automates the process of batch exporting drawings to PDFs, making it ideal for users who need to handle large quantities of files efficiently. By using a SOLIDWORKS macro to rebuild and save all drawings in a directory as PDF, teams can ensure every updated drawing is exported consistently without manual effort.
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-Conditions
- The folder containing the SolidWorks drawings must be specified in the code.
- SolidWorks must be installed and running on the machine.
Post-Conditions
- PDF files will be saved in the same location as the SolidWorks drawings.
- The original drawing files will remain unchanged.
VBA Macro Code
Visual Basic
|
1 |
' Disclaimer:<br>' The code provided should be used at your own risk. <br>' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code. <br>' For more information, visit [Blue Byte Systems Inc.](https://bluebyte.biz).<br><br>Option Explicit<br><br>' Define the main variables for SolidWorks application and model<br>Dim swApp As SldWorks.SldWorks<br>Dim swModel As ModelDoc2<br>Dim swFilename As String<br>Dim swRet As Boolean<br>Dim swErrors As Long<br>Dim swWarnings As Long<br>Dim swResponse As String<br><br>' Main subroutine<br>Sub Main()<br><br> ' Initialize SolidWorks application<br> Set swApp = Application.SldWorks<br><br> ' Specify the folder location containing the SolidWorks drawings<br> RebuildAndSaveAllDrawingsAsPDF "C:\SOLIDWORKS", ".SLDDRW", True<br><br>End Sub<br><br>' Subroutine to rebuild drawing and save as PDF<br>Sub RebuildAndSaveAllDrawingsAsPDF(swFolder As String, swExt As String, swSilent As Boolean)<br><br> Dim swDocTypeLong As Long<br><br> ' Ensure the file extension is in uppercase<br> swExt = UCase$(swExt)<br> swDocTypeLong = Switch(swExt = ".SLDDRW", swDocDRAWING, True, -1)<br><br> ' If the file type is not supported, exit the subroutine<br> If swDocTypeLong = -1 Then<br> Exit Sub<br> End If<br><br> ' Change directory to the folder<br> ChDir (swFolder)<br><br> ' Get the first file in the folder<br> swResponse = Dir(swFolder)<br> Do Until swResponse = ""<br><br> swFilename = swFolder & swResponse<br><br> ' Check if the file extension matches<br> If Right(UCase$(swResponse), 7) = swExt Then<br><br> ' Open the SolidWorks document<br> Set swModel = swApp.OpenDoc6(swFilename, swDocTypeLong, swOpenDocOptions_Silent, "", swErrors, swWarnings)<br><br> ' Rebuild the drawing if it's a drawing file<br> If swDocTypeLong <> swDocDRAWING Then<br> swModel.ShowNamedView2 "*Isometric", -1<br> End If<br><br> ' Define file path variables<br> Dim swFilePath As String<br> Dim swPathSize As Long<br> Dim swPathNoExtension As String<br> Dim swNewFilePath As String<br><br> swFilePath = swModel.GetPathName<br> swPathSize = Strings.Len(swFilePath)<br> swPathNoExtension = Strings.Left(swFilePath, swPathSize - 6)<br> swNewFilePath = swPathNoExtension & "PDF"<br><br> ' Save the document as PDF<br> swRet = swModel.SaveAs3(swNewFilePath, 0, 0)<br><br> ' Close the document<br> swApp.CloseDoc swModel.GetTitle<br><br> End If<br><br> ' Get the next file in the folder<br> swResponse = Dir<br> Loop<br><br>End Sub |
Macro
You can download the macro from here.
Customization
Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. Contact us.
