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 に答える