1

I am currently using the following code to print a word document

                Dim oWordApp As Word.Application
                Dim oTargetDoc As Word.Document
                oWordApp = New Word.Application

                Select Case Priority
                    Case 1
                        oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority1, DoNotSetAsSysDefault:=1)
                    Case 2
                        oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority2, DoNotSetAsSysDefault:=1)
                    Case 3
                        oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority3, DoNotSetAsSysDefault:=1)
                    Case 4
                        oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority4, DoNotSetAsSysDefault:=1)
                    Case 5
                        oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority5, DoNotSetAsSysDefault:=1)
                End Select

                oTargetDoc = oWordApp.Documents.Open(DocumentName & ".doc")
                oWordApp.PrintOut()
                oWordApp.Documents.Close()
                oWordApp.Quit()

However I have found it to be buggy with our shared printers, this bug only happens when printing using word. It works fine when doing print automation with PDFs(Adobe Reader) etc.

What I am looking for is some code in vb.net which will allow me print this documents and I have to be about to specify the printer it uses.

Thanks!

1 Answer 1

2

here's a complete functional program:
credits goes to josepaulino

Public Class Form1   
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f As New OpenFileDialog
            Dim p As New PrintDialog
            Dim app As Word.Application
            Dim doc As Word.Document

            'Open file and print dialogs to get desired document and printer
            If f.ShowDialog = Windows.Forms.DialogResult.OK Then
                If p.ShowDialog = Windows.Forms.DialogResult.OK Then
                    'Create instance of Word Application
                    app = New Word.Application

                    'Set Printer
                    app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, DoNotSetAsSysDefault:=1)

                    'Set filename to object type
                    Dim filename As Object = f.FileName
                    Dim m As Object = System.Reflection.Missing.Value

                    'Open document
                    doc = app.Documents.Open(filename, m, m, m, m, m, m, m, m, m, m, m)

                    'Print document
                    app.PrintOut()

                    'Close document
                    app.Documents.Close()

                    'Quit word application
                    app.Quit()

                    'Release 
                    app = Nothing
                End If
            End If
        End Sub
        Private Sub PrintWordDocument(ByVal strFilePath As String)



            ' Run Microsoft Word as COM-server
            On Error Resume Next
            Dim App As Word.Application
            Dim Doc As Object
            Dim p As New PrintDialog

            'Set Default printer
            Dim w = CreateObject("WScript.Network")
            w.SetDefaultPrinter(p.PrinterSettings.PrinterName)

            If p.ShowDialog = Windows.Forms.DialogResult.OK Then
                App = New Word.Application
                'App = CreateObject("Word.Application")

                ' Open document from file
                Doc = app.Documents.Open(strFilePath, , 1)


                If Doc = Not Nothing Then

                    ' Print all pages of the document
                    'App.ActivePrinter = p.PrinterSettings.PrinterName
                    Call app.PrintOut(False)

                    ' Close the document
                    Call Doc.Close()
                    Doc = Nothing

                End If
            End If

            ' Close Microsoft Word
            If App IsNot Nothing Then
                Call App.Quit()
            End If
            App = Nothing

        End Sub
    End Class
1
  • That is more or less where I got the code from which for whatever reason, doesn't work. Thanks though.
    – Rambomst
    Commented May 7, 2012 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.