none
Print word document without open the program ? RRS feed

  • Question

  • Hello !

    in vb.net 2010 , i need to print a word document without open Ms word , or at least the word window to be hidden all the time , I want that user don't know that Word is open ?

    Thank you.

    Wednesday, September 26, 2012 1:06 AM

Answers

  • This is the code for priinting a word document using vb.net. Try it and let me know.

    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
    Wednesday, September 26, 2012 2:50 AM
  • Hi dcode,

    Welcome to the MSDN forum.

    Of course you can, just set the visible property of word.application to False. Here is a sample:

    Dim oWord = New Word.Application With {.Visible = False}
    'OR
    Dim oWord = New Word.Application
    oWord.Visible = False

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 27, 2012 3:59 AM
    Moderator

All replies

  • This is the code for priinting a word document using vb.net. Try it and let me know.

    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
    Wednesday, September 26, 2012 2:50 AM
  • you can use the following code of lines:

            Dim objWord As Word.Application
            Dim objDoc As Word.Document
            objWord = CreateObject("Word.Application")
            objDoc = objWord.Documents.Open("enter your word document file path")
            objDoc.PrintOut()
            objDoc.Close()
            objDoc = Nothing
            objWord.Quit()

    the word document will be printed on your default printer..

    Thanks


    Hirendra Sisodiya from authorcode.com


    Wednesday, September 26, 2012 6:14 AM
  • thank you !

    But the winword window is open and visible for some moments and is closed.I want that user to not understand that Winword program is open.

    Wednesday, September 26, 2012 11:53 AM
  • I don't believe there is an alternative for Word unless you want to pay for a third-party solution.

    http://www.aspose.com/categories/.net-components/aspose.words-for-.net/default.aspx

    Otherwise, you're pretty much stuck with the automation methods posted by others.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, September 26, 2012 1:30 PM
  • thank you !

    But at least , is possible to open Word , but with invisible window ?

    Wednesday, September 26, 2012 4:12 PM
  • Hi dcode,

    Welcome to the MSDN forum.

    Of course you can, just set the visible property of word.application to False. Here is a sample:

    Dim oWord = New Word.Application With {.Visible = False}
    'OR
    Dim oWord = New Word.Application
    oWord.Visible = False

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 27, 2012 3:59 AM
    Moderator