Select printer for word document to print with process.start ()

  • Thread starter Thread starter big moose
  • Start date Start date
B

big moose

I have a windows forms application that includes a datagridview with links to
word documents (.doc, .docx), .pdf files and Windows Journal documents
(.jnt). I would like to print these out to a printer that the user selects.
My code will work fine with the "print" verb to the default printer on a
users's pc but will not work with the "printto" verb after trying to select
the printer with the printdialog form. Any suggestions for an amateur would
be most appreciated. Thank you in advance.

Private Sub SelectedReportsToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SelectedReportsToolStripMenuItem.Click


Dim RowCount, i As Integer
RowCount =
Me.DataGridView.Rows.GetRowCount(DataGridViewElementStates.Displayed) ' Get
row count of all rows
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
End If
For i = 0 To RowCount - 1 'loop through all rows in DataGridView
Dim Row As DataGridViewRow
Row = Me.DataGridView.Rows(i)
If Row.Selected = True Then
Dim FileNamePath As String 'Get file name path
FileNamePath = Row.Cells(7).Value 'get path to file
PrintDocument1.DocumentName = FileNamePath
Dim File As String = PrintDocument1.DocumentName
Dim Process As New Process
Process.StartInfo.UseShellExecute = True
Process.StartInfo.FileName = File 'give filename to process
Process.StartInfo.Verb = "PrintTo"
Process.StartInfo.CreateNoWindow = True
Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
Process.Start()
Process.CloseMainWindow()
Process.Close()
Process = Nothing
End If
Next i

End Sub
 
Back
Top