Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何在不打开word的情况下打印word文档_Vb.net_Printing_Ms Word - Fatal编程技术网

Vb.net 如何在不打开word的情况下打印word文档

Vb.net 如何在不打开word的情况下打印word文档,vb.net,printing,ms-word,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

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


谢谢

这里有一个完整的功能程序:
荣誉归于约瑟帕利诺

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

这或多或少就是我从中得到的代码,无论出于什么原因,它都不起作用。谢谢你。