US$0.00
0

SolidWorks Macro to Batch Rebuild and Export Drawings to PDF

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.

SolidWorks Macro to Batch Rebuild and Export Drawings to PDF and An image divided into two side-by-side sections labeled "Before" and "After," showing a SOLIDWORKS software interface. The "Before" section displays a sketch on the "Front Plane" with a simple rectangular outline and a single dimension line labeled "L." The feature tree on the left lists "Sketch1" under "Front Plane." The "After" section shows the same sketch transformed into a filled square with diagonal lines, now including dimension lines labeled "59.43" for the length and "60.0" for the width, and the same feature tree updated with "Sketch2." Both sections include toolbars and a 3D view with axes indicators.

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

  1. Register the Macro File: Start with SolidWorks open and go to Tools>Macro>New.
  2. 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.
  3. Paste the code: Download the VBA code we have provided below and place it in your editor.
  4. 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.

  1. 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

' 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.

Author

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM. With over a decade of experience, he has authored several courses and open-source frameworks related to the SOLIDWORKS API. His leadership ensures that Blue Byte Systems prioritizes customer satisfaction and delivers high-quality software and CAD design solutions.
0
    0
    Your Cart
    Your cart is emptyReturn to Shop
    ×