Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Word Doc without openning it

Status
Not open for further replies.

basbrian

Programmer
Feb 18, 2002
49
AU
From a Visual Studio 2008 form I create and save a word 2003 word document for an employee selected from a dropdown list. I am trying to add a button to my form that will allow the user to view (printpreview) the document and print it if required. I am trying to work out how to printpreview the document without openning it. When I open it and then printpreview and close printpreview the document is still open which then allows the user to change it and/or they have to remember to close it.(trying to make this form idiot proof lol). here is my code at the moment.
Code:
        oWord = CreateObject("Word.Application")

        Try
            oDoc = oWord.Documents.Open("q:\delegations\Delegation for " & CboName.Text & ".doc")
            oWord.Visible = True
            oDoc.PrintPreview()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            oDoc.Close(False)
            oDoc = Nothing
            oWord.Application.Quit(Nothing)
            oWord = Nothing
            Exit Sub
        End Try
 
You can't. To see what is in a file you have to open that file. Also note that a Catch only works if there is an actual error so things like closing the document have to be done after the End Try or within a Finally. Also if you are using a try and you need an Exit sub in your Catch then you really need to restructure the Sub/Function in another way. While some times needed an Exit is still the worst way to leave a part of the code.

Code:
        oWord = CreateObject("Word.Application")

        Try
            oDoc = oWord.Documents.Open("q:\delegations\Delegation for " & CboName.Text & ".doc")
            oWord.Visible = True
            oDoc.PrintPreview()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            'Finally occurs if you get an error or not so
            'since it could fail while openg the document
            'you need to make sure it isn't nothing first
            'or you get another error.
            If oDoc IsNot Nothing Then
                oDoc.Close(False)
                oDoc = Nothing
            End If

            'Always best to check first.
            If oWord IsNot Nothing Then
                oWord.Application.Quit(Nothing)
                oWord = Nothing
            End If
        End Try

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Also to close the WINWORD.EXE process call:
Code:
Marshal.FinalReleaseComObject(oDoc)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top