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 print active SOLIDWORKS document. It is possible to specify the settings for printing: printer name, printer range, orientation, paper size and scale
Settings
To configure settings change the values of constants at the top of the macro as described below
Const PRINTER_NAME AsString = "Microsoft Print To PDF"'full name of the printerConst PRINT_RANGE AsString = "1-3,5"'range to print. Specify * to print all pages or a rangeConst PRINT_ORIENTATION AsInteger = swPageSetupOrientation_e.swPageSetupOrient_Landscape 'orientation landscape or portraitConst PRINTER_PAPER_SIZE AsString = "A3"'Paper size to print toConst PRINT_SCALE AsString = "*"'Scale of print. Use * to scale to fit or a value of scale % (from 1 to 1000)
PrivateDeclare PtrSafe Function DeviceCapabilities Lib"winspool.drv"Alias"DeviceCapabilitiesA" (ByVal lpDeviceName AsString, ByVal lpPort AsString, ByVal iIndex AsLong, ByRef lpOutput AsAny, ByRef lpDevMode AsAny) AsLongDim swApp As SldWorks.SldWorks
Const PRINTER_NAME AsString = "Microsoft Print To PDF"Const PRINT_RANGE AsString = "1-3,5"Const PRINT_ORIENTATION AsInteger = swPageSetupOrientation_e.swPageSetupOrient_Landscape
Const PRINTER_PAPER_SIZE AsString = "A3"Const PRINT_SCALE AsString = "*"Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
If swModel IsNothingThen
Err.Raise vbError, "", "Please open the document"EndIfDim swPageSetup As SldWorks.PageSetup
Set swPageSetup = swModel.PageSetup
Dim origPrinter AsStringDim origPrinterPaperSize AsIntegerDim origScaleToFit AsBooleanDim origScale AsDoubleDim origOrientation AsIntegerDim origUsePageSetup AsInteger
origPrinter = swModel.Printer
origPrinterPaperSize = swPageSetup.PrinterPaperSize
origScaleToFit = swPageSetup.ScaleToFit
origScale = swPageSetup.Scale2
origOrientation = swPageSetup.Orientation
origUsePageSetup = swModel.Extension.UsePageSetup
swModel.Printer = PRINTER_NAME
swPageSetup.PrinterPaperSize = GetPaper(PRINTER_NAME, PRINTER_PAPER_SIZE)
If PRINT_SCALE = "*"Then
swPageSetup.ScaleToFit = TrueElse
swPageSetup.ScaleToFit = False
swPageSetup.Scale2 = CDbl(PRINT_SCALE)
EndIf
swPageSetup.Orientation = PRINT_ORIENTATION
swModel.Extension.UsePageSetup = swPageSetupInUse_e.swPageSetupInUse_Document
Dim swPrintSpec As SldWorks.PrintSpecification
Set swPrintSpec = swModel.Extension.GetPrintSpecification
swPrintSpec.printRange = GetPrintRange(PRINT_RANGE)
swModel.Extension.PrintOut4 PRINTER_NAME, "", swPrintSpec
swModel.Printer = origPrinter
swPageSetup.PrinterPaperSize = origPrinterPaperSize
swPageSetup.ScaleToFit = origScaleToFit
swPageSetup.Scale2 = origScale
swPageSetup.Orientation = origOrientation
swModel.Extension.UsePageSetup = origUsePageSetup
EndSubFunction GetPrintRange(range AsString) AsVariantDim printRange() AsLongIf range = "*"ThenReDim printRange(1)
printRange(0) = -1
printRange(1) = -1
ElseDim vPageRanges AsVariant
vPageRanges = Split(range, ",")
ReDim printRange((UBound(vPageRanges) + 1) * 2 - 1)
Dim i AsIntegerFor i = 0 To UBound(vPageRanges)
Dim vStartEndPages AsVariant
vStartEndPages = Split(Trim(CStr(vPageRanges(i))), "-")
Dim startPage AsLongDim endPage AsLong
startPage = CLng(vStartEndPages(0))
If UBound(vStartEndPages) = 0 Then
endPage = startPage
ElseIf UBound(vStartEndPages) = 1 Then
endPage = CLng(vStartEndPages(1))
Else
Err.Raise vbError, "", "Invalid page range: " & CStr(vPageRanges(i))
EndIf
printRange(i * 2) = startPage
printRange(i * 2 + 1) = endPage
NextEndIf
GetPrintRange = printRange
EndFunctionFunction GetPaper(printerName AsString, paperName AsString) AsIntegerConst DC_PAPERNAMES AsInteger = &H10
Const DC_PAPERS AsInteger = &H2
Dim papersCount AsInteger
papersCount = DeviceCapabilities(printerName, "", DC_PAPERS, ByVal vbNullString, 0)
If papersCount > 0 ThenDim papersCodes() AsIntegerReDim papersCodes(papersCount - 1)
DeviceCapabilities printerName, "", DC_PAPERS, papersCodes(0), 0
Dim papersNames AsString
papersNames = String$(64 * papersCount, 0)
DeviceCapabilities printerName, "", DC_PAPERNAMES, ByVal papersNames, 0
Dim i AsIntegerFor i = 0 To papersCount
If LCase(ParsePaperName(papersNames, 64 * i + 1)) = LCase(paperName) Then
GetPaper = papersCodes(i)
EndIfNextElse
Err.Raise vbError, "", "No sizes available for the specified printer"EndIfEndFunctionFunction ParsePaperName(papersNames AsString, offset AsInteger) AsStringDim paperName AsString
paperName = Mid(papersNames, offset, 64)
Dim nullCharIndex AsInteger
nullCharIndex = InStr(paperName, vbNullChar)
If nullCharIndex <> 0 Then
paperName = Left$(paperName, nullCharIndex - 1)
EndIf
ParsePaperName = paperName
EndFunction
Notifications
Join session by SOLIDWORKS and PDM API expert Artem Taturevych at 3DEXPERIENCE World 2026 on Wednesday, Feb 4 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more