This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This VBA macro allows to export all sheets (or selected sheets) from the active SOLIDWORKS drawing into the separate PDF files. If no sheets selected - all sheets will be exported.
PDF files are saved to the same folder as original drawing and named after the sheet.
INCLUDE_DRAWING_NAME option allows to also include the name of the drawing to the output PDF if set to True, otherwise only sheet name is used.
Const INCLUDE_DRAWING_NAME AsBoolean = True'include the name of the drawing
Const INCLUDE_DRAWING_NAME AsBoolean = TrueDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
try_:
OnErrorGoTo catch_
Dim swDraw As SldWorks.DrawingDoc
Set swDraw = swApp.ActiveDoc
Dim swModel As SldWorks.ModelDoc2
Set swModel = swDraw
If swModel.GetPathName() = ""Then
Err.Raise vbError, "", "Please save drawing"EndIfDim vSheetNames AsVariantDim i AsIntegerDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swModel.SelectionManager
Dim selSheetNames() AsStringFor i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelSHEETS ThenIf (Not selSheetNames) = -1 ThenReDim selSheetNames(0)
ElseReDimPreserve selSheetNames(UBound(selSheetNames) + 1)
EndIfDim swSheet As SldWorks.Sheet
Set swSheet = swSelMgr.GetSelectedObject6(i, -1)
selSheetNames(UBound(selSheetNames)) = swSheet.GetName()
EndIfNextIf (Not selSheetNames) = -1 Then
vSheetNames = swDraw.GetSheetNames
Else
vSheetNames = selSheetNames
EndIfFor i = 0 To UBound(vSheetNames)
Dim sheetName AsString
sheetName = vSheetNames(i)
Dim swExpPdfData As SldWorks.ExportPdfData
Set swExpPdfData = swApp.GetExportFileData(swExportDataFileType_e.swExportPdfData)
Dim errs AsLongDim warns AsLongDim expSheets(0) AsString
expSheets(0) = sheetName
swExpPdfData.ExportAs3D = False
swExpPdfData.ViewPdfAfterSaving = False
swExpPdfData.SetSheets swExportDataSheetsToExport_e.swExportData_ExportSpecifiedSheets, expSheets
Dim drawName AsString
drawName = swModel.GetPathName()
drawName = Mid(drawName, InStrRev(drawName, "\") + 1, Len(drawName) - InStrRev(drawName, "\") - Len(".slddrw"))
Dim outFile AsString
outFile = swModel.GetPathName()
outFile = Left(outFile, InStrRev(outFile, "\"))
outFile = outFile & IIf(INCLUDE_DRAWING_NAME, drawName & "_", "") & sheetName & ".pdf"IfFalse = swModel.Extension.SaveAs(outFile, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, swExpPdfData, errs, warns) Then
Err.Raise vbError, "", "Failed to export PDF to " & outFile
EndIfNextGoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally_:
EndSub
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more