VBA Solidworks don't get print

53 Views Asked by At

I'm trying to create a VBA code to print my drawings in SolidWorks. I've designed a UserForm with buttons, where each button corresponds to a sheet size (A4 Portrait and Landscape, A3, A2 and A1).

Please note that the drawing is already open and active. I want to use a shortcut to run my VBA code, and when I do, my UserForm should appear so I can choose my desired sheet size.

Below is my code; it doesn't show any errors, but it's not working. The code is for printing only in A4 sheet size; it works when I use it for other sheet sizes.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPageSetup As SldWorks.PageSetup
Dim NameFile As String
Dim Printer As String 

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set swPageSetup = swModel.PageSetup

NameFile = swModel.GetTitle

Printer = "Brother HL-5470DW series Printer" 

With swPageSetup
    .PrinterPaperSize = 9
    .Orientation = 1
    .HighQuality = True
    .Scale2 = False
    .DrawingColor = 1
End With

swModelDocExt.PrintOut4 Printer, NameFile, swPageSetup
1

There are 1 best solutions below

0
Mario Malic On

Here is a macro that does it. In your code, third argument to PrintOut4 is wrong. It needs to be the PrintSpecification object

Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPageSetup As SldWorks.PageSetup
Dim swPrintSpec As SldWorks.PrintSpecification

Sub main()
Dim printerStr As String

Set swApp = Application.SldWorks
Set swModelDoc = swApp.ActiveDoc
Set swModelDocExt = swModelDoc.Extension
Set swPrintSpec = swModelDocExt.GetPrintSpecification

swPrintSpec.PrintToFile = False

printerStr = swPrintSpec.PrinterQueue ' Gets the default printer name, otherwise specify it yourself
nameFile = swModelDoc.GetTitle
swModelDocExt.PrintOut4 printerStr, nameFile, swPrintSpec

End Sub