Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a Word server to print Word files in VB .NET
DescriptionThis example shows how to use a Word server to print Word files in VB .NET.
KeywordsOffice, Word, print, PrintOut, VB.NET
CategoriesOffice, VB.NET
 
Open a Word server object. Use it to open and print the file.

Note that this project contains a reference to the Word object library. You may need to use a different reference for your version of Word.

 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated