Setting the printer from the Drawing print manager gives a generic error

Setting the printer from the Drawing print manager gives a generic error

dwlawrence
Contributor Contributor
135 Views
1 Reply
Message 1 of 2

Setting the printer from the Drawing print manager gives a generic error

dwlawrence
Contributor
Contributor

In the following code, attempting to set the printer from the DrawingPrintManager gives an error and then continues onto using the default system printer. This error has come up as a "Class Not Registered error, but is typically an Unspecified Error. 

 

If you hard code in the string to, i.e., "Microsoft Print to PDF" etc., it works without any problems. It's just when setting the printer to the user selection after being certain it's cast to a String data type, that it catches the errors. I left the MessageBox in that shows what the user's selected item is from the drop down box.

 

<Start Code>

Imports System.Text
Imports System.Windows.Forms
AddReference "System.Drawing.dll"
Sub Main()
'Define the active document and check to make sure it's a DrawingDocument type and, if not, send the user a Msg and bail out
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MessageBox.Show("Please Open a Drawing File to Print", "NOT A DRAWING DOCUMENT")
Exit Sub
End If
Call SetUpDialogForm(oDoc)
End Sub
 
Sub SetUpDialogForm(oDoc As DrawingDocument)
 
Dim oPrintMgr As Inventor.DrawingPrintManager = oDoc.PrintManager
Dim printerList As New StringBuilder()
printerList.AppendLine("Installed Printer List: ").AppendLine()
'Get Installed printers
For Each printer As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
printerList.AppendLine(printer)
Next
Dim strResults As String = printerList.ToString
'MessageBox.Show(strResults, "Printers Available")
 
' Convert StringBuilder to a string array
Dim printers() As String = printerList.ToString().Split(System.Environment.NewLine)
 
' Create the pull-down form 
Dim pullDownForm As New Form()
pullDownForm.Text = "Select Printer From List"
pullDownForm.Size = New Drawing.Size(400, 150)
 
' Center the form on the screen
pullDownForm.StartPosition = FormStartPosition.CenterScreen
 
' Create and configure the ComboBox
Dim comboBox As New ComboBox()
comboBox.Items.AddRange(printers)
comboBox.DropDownStyle = ComboBoxStyle.DropDownList
comboBox.Size = New Drawing.Size(300, 30)
 
' Center the ComboBox horizontally
comboBox.Location = New Drawing.Point((pullDownForm.ClientSize.Width - comboBox.Width) / 2, 30)
 
' Create and configure the OK button
Dim okButton As New Button()
okButton.Text = "OK"
okButton.Size = New Drawing.Size(75, 30)
 
' Position the OK button below the ComboBox by 10 pixels
okButton.Location = New Drawing.Point((pullDownForm.ClientSize.Width - okButton.Width) / 2 - 40, comboBox.Bottom + 10)
 
' Create and configure the Cancel button
Dim cancelButton As New Button()
cancelButton.Text = "Cancel"
cancelButton.Size = New Drawing.Size(75, 30)
 
' Position the Cancel button to the right of the OK button
cancelButton.Location = New Drawing.Point(okButton.Right + 10, okButton.Top)
 
' Define what happens when the OK button is clicked
AddHandler okButton.Click, Sub(sender, E)
                               ' Check if a choice has been selected
                               If comboBox.SelectedIndex = -1 Then
                                   ' If no selection, close the form affter letting the user know
   MessageBox.Show("No Printer Selected. Exiting", "EXITING. TRY AGAIN", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                                   pullDownForm.Close()
                               Else
                                   Dim selectedChoice As String = comboBox.SelectedItem.ToString()
'Set the printer options and use Arch D size
With oPrintMgr 
Try
'Print out selected choice for debugging
MessageBox.Show("Selected Printer: " + selectedChoice, "Selected Printer")
'Attempt to set the printer
.Printer = selectedChoice.ToString()
Catch ex As System.Exception
MessageBox.Show("Error: " + ex.Message, "ERROR OCCURED SETTING PRINTER")'**Error: 'Class not registered' may mean the COM Object isn't properly registered
End Try
.AllColorsAsBlack = True
.Orientation = kLandscapeOrientation
.PaperSize = kPaperSizeCustom
.PaperHeight = 24
.PaperWidth = 36
.SubmitPrint()
End With
 
                                   ' Close the form after selection
                                   pullDownForm.Close()
                               End If
                           End Sub
 
' Define what happens when the Cancel button is clicked
AddHandler cancelButton.Click, Sub(sender, E)
                                  ' Close the form when Cancel is clicked
                                  pullDownForm.Close()
                               End Sub
 
' Handle the form's closing event to detect when the user hits the X
AddHandler pullDownForm.FormClosing, Sub(sender, e)
                                       If e.CloseReason = CloseReason.UserClosing Then
                                           ' Set the DialogResult to Cancel when the user clicks the "X"
                                           pullDownForm.DialogResult = DialogResult.Cancel
                                       End If
                                   End Sub
 
' Add controls to the form
pullDownForm.Controls.Add(comboBox)
pullDownForm.Controls.Add(okButton)
pullDownForm.Controls.Add(cancelButton)
 
' Show the form to the user
pullDownForm.ShowDialog()
End Sub
<End Code>
0 Likes
Accepted solutions (1)
136 Views
1 Reply
Reply (1)
Message 2 of 2

dwlawrence
Contributor
Contributor
Accepted solution

Thanks to @gkniksch_aep, who found it was StringBuilder messing with the string. So making it a List(Of String) did the trick for anyone that faces this one.