Hello everyone,
I'm trying execute a macro in VBA to print Model sheet, but it doesn't create PDF file in specified file path. When I change the printer to "Microsoft Print to PDF" the dialog window with printing settings appears. Does this function even work with built-in printers/plotters?
Here's my code:
Dim dsApp As DraftSight.Application
Dim dsDoc As DraftSight.Document
Dim dsSelectionManager As DraftSight.selectionManager
Dim dsPolyline As DraftSight.PolyLine
Dim dsPrintMgr As DraftSight.PrintManager
Dim dsSheet As DraftSight.Sheet
Dim sheetsArr As Variant
Dim sheetArray(0) As String
Dim printerName As String
Dim paperLength As Double
Dim paperWidth As Double
Dim top As Double
Dim bottom As Double
Dim left As Double
Dim right As Double
Dim sheetObj As Variant
Sub ConnectToDraftSight()
Debug.Print (" #### MODULE 2 - START ####")
Set dsApp = GetObject(, "DraftSight.Application")
If dsApp Is Nothing Then
Set dsApp = CreateObject("DraftSight.Application")
End If
dsApp.Visible = True
Set dsDoc = dsApp.GetActiveDocument
Set dsPrintMgr = dsApp.GetPrintManager
'Get available printers
dsVarPrinters = dsPrintMgr.GetAvailablePrinters
Debug.Print ("Available printers:")
Debug.Print (" PDF")
Debug.Print (" JPG")
Debug.Print (" PNG")
Debug.Print (" SVG")
For i = 0 To UBound(dsVarPrinters)
Debug.Print (i & ". " & dsVarPrinters(i))
Next i
Debug.Print (" #### USTAWIANIE DRUKARKI ####")
'Set the printer name
printerName = "PDF"
dsPrintMgr.Printer = printerName
If printerName = "" Then
MsgBox ("Failed to set IPrintManager.Printer property " & printerName & " value.")
End If
'Get available paper sizes for printer
dsVarPaperSizes = dsPrintMgr.AvailablePaperSizes
If IsArray(dsVarPaperSizes) And UBound(dsVarPaperSizes) = 0 Then
MsgBox ("List of available paper sizes is empty for " & dsPrintMgr.Printer & " printer.")
Else
Debug.Print ("Available paper sizes for " & printerName & ":")
For i = 0 To UBound(dsVarPaperSizes)
Debug.Print (" " & dsVarPaperSizes(i))
Next i
End If
'Set paper size
dsPrintMgr.PaperSize = "ISO_A4_(297.00_x_210.00_MM)"
'Get paper size
dsPrintMgr.GetPaperSize paperLength, paperWidth
Debug.Print "Paper length: " & paperLength
Debug.Print "Paper width: " & paperWidth
'Get print margins
dsPrintMgr.GetPrintMargins top, bottom, left, right
Debug.Print ("Top margin: " & top)
Debug.Print ("Bottom margin: " & bottom)
Debug.Print ("Left margin: " & left)
Debug.Print ("Right margin: " & right)
'Set print orientation
dsPrintMgr.Orientation = dsPrintOrientation_e.dsPrintOrientation_Landscape
'Set the sheets to print
sheetArray(0) = "Sheet1"
dsPrintMgr.SetSheets (sheetArray)
'Verify the sheets to print
sheetObj = dsPrintMgr.GetSheets
'If sheetObj Is Nothing Then
' MsgBox ("Failed to set sheets to print")
' End If
'Center the printout
dsPrintMgr.PrintOnCenter = True
'Set print quality as defined in dsStandardPrintQuality_e
dsPrintMgr.Quality = dsStandardPrintQuality_e.dsStandardPrintQuality_Normal
'Scale the line weights
dsPrintMgr.ScaleLineWeight = True
'Scale the printout to fit the paper
dsPrintMgr.ScaleToFit = True
dsPrintMgr.GetAvailablePrintStyleTables dsVarPrintStyles
Debug.Print "Available print style tables:"
For i = 0 To UBound(dsVarPrintStyles)
Debug.Print " " & dsVarPrintStyles(i)
Next i
'Set the print style table
dsPrintMgr.StyleTable = "default.ctb"
'Set the printing range
dsPrintMgr.SetPrintRange dsPrintRange_e.dsPrintRange_AllGeometry, "", False, 0#, 0#, 0#, 0#
'Use the assigned print style
dsPrintMgr.UseAssignedPrintStyle = True
dsPrintMgr.PrintOut 1, "c: pdf.pdf"
MsgBox "%%%%% KONIEC %%%%"
End Sub
