VBA Excel打印Word Doc-不打印

时间:2019-05-13 19:09:50

标签: excel vba ms-word word-vba

我的代码可以在自己的便携式笔记本电脑上正常工作,该代码将打开Word文档并使用默认打印机进行打印。

代码是:

Private Sub CommandButton1_Click()    
    ' Step 1
    Dim objWord As New Word.Application
    ' Hidden window!
    objWord.Visible = False
    ' Save the original printer, otherwise you will reset the system default!
    Dim previousPrinter As String


    ' Step 2
    Dim objDoc
    Set objDoc = objWord.Documents.Open("test.docx")

    ' Step 3 -- in this case, print out the document without any prompts
    objDoc.PrintOut
    ' Restore the original printer
    objWord.ActivePrinter = previousPrinter

    ' Step 4
    objDoc.Close

    ' Step 5
    objWord.Quit
    MsgBox (objWord.ActivePrinter) 
End Sub

但是,当我在办公室尝试此操作时,代码没有任何错误,但是没有任何内容发送到打印机。有什么可以引起这个的吗?这是一台xerox打印机(在网络上),并且使用跟随打印

1 个答案:

答案 0 :(得分:1)

如果您要强制它在另一台打印机上打印,则需要这样的东西:

previousPrinter = objWord.ActivePrinter

For i = 0 To 15
   curNePrint = VBA.Format(i, "00")
   On Error Resume Next
      objWord.ActivePrinter = "\\ServerName\printername z111b on ne" & curNePrint & ":"
Next i

objWord.PrintOut Copies:=1
ObjWord.ActivePrinter = previousPrinter
On Error Goto 0