1

我目前正在使用以下代码打印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 中的一些代码,它允许我打印这些文档,并且我必须指定它使用的打印机。

谢谢!

4

1 回答 1

2

这是一个完整的功能程序:
感谢 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
于 2012-05-04T09:38:13.780 回答