Get and Set Print Options Example (VBA)

This example shows how to get and set print options.

'--------------------------------------------------------------
' Preconditions:
' 1. Create a VBA macro in a software product in which VBA is
'    embedded.
' 2. Copy and paste this example into the Visual Basic IDE.
' 3. Add a reference to the DraftSight type library,
'    install_dir\bin\dsAutomation.dll.
' 4. Start DraftSight and open a document.
' 6. Run the macro.
'
' Postconditions: Message boxes pop up if setting any printer
' option fails for the specified printer. Read the text in each
' message box before clicking OK to close it.
'----------------------------------------------------------------
Sub main()
    Dim dsApp As DraftSight.Application
    Dim dsDoc As DraftSight.Document
    
    'Connect to DraftSight
    Set dsApp = GetObject(, "DraftSight.Application")
    'Abort any command currently running in DraftSight 
    'to avoid nested commands
    dsApp.AbortRunningCommand
    'Get active document
    Set dsDoc = dsApp.GetActiveDocument()
    If Not dsDoc Is Nothing Then
    'Get and set printing options
    GetSetPrintOptions dsApp
    End If
    
End Sub
Sub GetSetPrintOptions(dsApp As DraftSight.Application)
    Dim dsPrintMgr As DraftSight.PrintManager
    Dim printerName As String
    Dim dsVarPaperSizes As Variant
    Dim paperLength As Double
    Dim paperWidth As Double
    Dim rightLength As Double
    Dim rightWidth As Double
    Dim paperSize As String
    Dim precision As Double
    Dim xOffset As Double
    Dim yOffset As Double
    Dim printOrientation As DraftSight.dsPrintOrientation_e
    Dim centerPrint As Boolean
    Dim printQuality As Long
    Dim scaleLineWeight As Boolean
    Dim scaleToFit As Boolean
    Dim styleTable As String
    Dim userScale As Boolean
    Dim viewDisplayStyle As DraftSight.dsViewDisplayStyle_e
    
    'Get PrintManager
    Set dsPrintMgr = dsApp.GetPrintManager
    
    If Not dsPrintMgr Is Nothing Then
        
        'Get printer name
        printerName = "JPG"
        dsPrintMgr.Printer = printerName
        If printerName = "" Then
            MsgBox "Failed to set IPrintManager.Printer property " & printerName & " value."
        End If
        
        'Get available paper sizes
        dsVarPaperSizes = dsPrintMgr.AvailablePaperSizes
        
        If IsArray(dsVarPaperSizes) And UBound(dsVarPaperSizes) = 0 Then
            MsgBox "List of available paper sizes is empty for " & dsPrintMgr.Printer & " printer."
        End If
        
        'Set paper size to "UserDefinedRaster (87.00 x 134.00Pixels)"
        paperSize = "UserDefinedRaster (87.00 x 134.00Pixels)"
        dsPrintMgr.paperSize = paperSize
        
        If paperSize <> dsPrintMgr.paperSize Then
            MsgBox "Failed to set IPrintManager.PaperSize property to " & paperSize & " value."
        End If
        
        'Get paper size
        rightLength = 134
        rightWidth = 87
        dsPrintMgr.GetPaperSize paperLength, paperWidth
        
        'Verify paper length value
        precision = 0.000000001
        If Abs(rightLength - paperLength) > precision Then
            MsgBox "IPrintManager.GetPaperSize method returns wrong paper length for '" & paperSize & "' paper size."
        End If
        
        'Verify paper width value
        If Abs(rightWidth - paperWidth) > precision Then
            MsgBox "IPrintManager.GetPaperSize method returns wrong paper width for '" & paperSize & "' paper size."
        End If
        
        'Set paper size to "VGA_(640.00_x_480.00_Pixels)"
        paperSize = "VGA_(640.00_x_480.00_Pixels)"
        dsPrintMgr.paperSize = paperSize
        
        If paperSize <> dsPrintMgr.paperSize Then
            MsgBox "Failed to set IPrintManager.PaperSize property to " & paperSize & " value."
        End If
        
        'Get paper size
        rightLength = 480
        rightWidth = 640
        dsPrintMgr.GetPaperSize paperLength, paperWidth
        
        'Verify paper length value
        If Abs(rightLength - paperLength) > precision Then
            MsgBox "IPrintManager.GetPaperSize method returns wrong paper length for '" & paperSize & "' paper size."
        End If
        
        'Verify paper width value
        If Abs(rightWidth - paperWidth) > precision Then
            MsgBox "IPrintManager.GetPaperSize method returns wrong paper width for '" & paperSize & "' paper size."
        End If
        
        'Get print margins
        Dim top As Double
        Dim bottom As Double
        Dim left As Double
        Dim right As Double
        dsPrintMgr.GetPrintMargins top, bottom, left, right
        
        'Set print offset
        xOffset = 5#
        yOffset = 10#
        dsPrintMgr.SetPrintOffset xOffset, yOffset
        
        'Get print margins
        Dim resultXOffset As Double
        Dim resultYOffset As Double
        
        dsPrintMgr.GetPrintOffset resultXOffset, resultYOffset
        
        'Verify print offset values
        If Abs(resultXOffset - xOffset) > precision Then
            MsgBox "IPrintManager.GetPrintOffset method returns wrong XOffset value. It should be " & xOffset & ", but it is " & resultXOffset & "."
        End If
        
        If Abs(resultYOffset - yOffset) > precision Then
            MsgBox "IPrintManager.GetPrintOffset method returns wrong YOffset value. It should be " & yOffset & ", but it is " & resultYOffset & "."
        End If
        
        'Set print orientation
        printOrientation = dsPrintOrientation_Landscape
        dsPrintMgr.Orientation = printOrientation
        
        'Verify if print orientation was set
        If printOrientation <> dsPrintMgr.Orientation Then
            MsgBox "Failed to set IPrintManager.Orientation property. It should be " & printOrientation & ", but it is " & dsPrintMgr.Orientation & "."
        End If
        
        'Set PrintOnCenter property
        centerPrint = False
        dsPrintMgr.PrintOnCenter = centerPrint
        
        If centerPrint <> dsPrintMgr.PrintOnCenter Then
            MsgBox "Failed to set IPrintManager.PrintOnCenter property to " & centerPrint
        End If
        
        'Set print quality
        printQuality = 300
        dsPrintMgr.Quality = printQuality
      
        'Set ScaleLineWeight property
        scaleLineWeight = False
        dsPrintMgr.scaleLineWeight = scaleLineWeight
        
        If scaleLineWeight <> dsPrintMgr.scaleLineWeight Then
            MsgBox "Failed to set IPrintManager.ScaleLineWeight property."
        End If
        
        'Set ScaleToFit property
        scaleToFit = False
        dsPrintMgr.scaleToFit = scaleToFit
        
        If scaleToFit <> dsPrintMgr.scaleToFit Then
            MsgBox "Failed to set IPrintManager.ScaleToFit property to " & scaleToFit
        End If
         
        'Set StyleTable property
        styleTable = "default.ctb"
        dsPrintMgr.styleTable = styleTable
        
        If styleTable <> dsPrintMgr.styleTable Then
            MsgBox "Failed to set IPrintManager.StyleTable property to " & styleTable
        End If
        
        'Set UserScale property
        userScale = False
        dsPrintMgr.userScale = userScale
         
        'Set ViewDisplayStyle property
        viewDisplayStyle = dsViewDisplayStyle_Rendered
        dsPrintMgr.viewDisplayStyle = viewDisplayStyle
        
        If viewDisplayStyle <> dsPrintMgr.viewDisplayStyle Then
            MsgBox "Failed to set IPrintManager.ViewDisplayStyle property to " & viewDisplayStyle
        End If
        
    Else
        MsgBox "IDocument.GetPrintManager returns Nothing."
    End If
End Sub