Private Sub btnPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPrint.Click
Dim word_server As Word.Application
' Open the Word server.
Me.Cursor = Cursors.WaitCursor
Application.DoEvents()
Try
word_server = New Word.Application
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show("Error opening Word.Basic" & vbCrLf _
& ex.Message, _
"Error", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Exit Sub
End Try
' Open the file.
Try
word_server.Documents.Open( _
FileName:=CType(txtFilename.Text, Object), _
ReadOnly:=True, _
AddToRecentFiles:=False, _
Format:=Word.WdOpenFormat.wdOpenFormatAuto)
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show("Error opening file " & _
txtFilename.Text & vbCrLf & ex.Message)
word_server.Quit(False)
Exit Sub
End Try
' Print the document.
Try
word_server.ActiveDocument.PrintOut( _
Range:=Word.WdPrintOutRange.wdPrintAllDocument, _
_
Item:=Word.WdPrintOutItem.wdPrintDocumentContent, _
_
Copies:=1, _
Background:=False)
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show("Error opening file " & _
txtFilename.Text & vbCrLf & ex.Message)
word_server.ActiveDocument.Close(SaveChanges:=False)
word_server.Quit(False)
Exit Sub
End Try
' Close the document.
word_server.ActiveDocument.Close(SaveChanges:=False)
' Close the Word server.
word_server.Quit(False)
word_server = Nothing
MessageBox.Show("OK", "Done", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub
|