如何在不打开单词的情况下打印word文档

时间:2012-05-03 23:28:09

标签: vb.net printing ms-word

我目前正在使用以下代码打印word文档

                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()

但是我发现它与我们的共享打印机有关,这个错误只发生在使用word打印时。使用PDF(Adobe Reader)等进行打印自动化时,它可以正常工作。

我正在寻找的是vb.net中的一些代码,它允许我打印这些文档,我必须要指定它使用的打印机。

谢谢!

1 个答案:

答案 0 :(得分:2)

这是一个完整的功能程序:
学分转到约瑟帕利诺

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