从Excel VBA自动选择打印机托盘以打印Word文档

时间:2015-01-20 17:48:18

标签: vba excel-vba excel

我正在尝试从Excel打印Word文档 我想选择打印机托盘在默认打印机中打印word文档。 纸盘1(信头)中的前2页和纸盘2中的第3页(A4纸)使用excel VBA。

以下代码可以在默认打印机和托盘中打印。 帮我选择托盘

'Print in letter head:- Choose Tray one

'Print Other pages in A4:- Choose Tray Two

打印机详细信息:

*网络打印机 * Hp激光打印机5200 dtn

Dim directory As String, fileName As String, ans As String, i As Integer
            Dim objWord As Object
            Dim intpage As Integer, intcopies As Integer, intnrml As Integer

            Set objWord = CreateObject("Word.Application")
            objWord.Visible = True

            ' path to the folder
            directory = "C:\Users\bharath.a.raj.DIR\Desktop\Print\"
            fileName = Dir(directory & "*.doc*") ' Open Multiple Word Docs Both .doc and .docx
            Do While fileName <> ""
            objWord.Documents.Open (directory & fileName)

            On Error Resume Next
           '
            intcopies = CInt(InputBox("How many copies?"))
            intpage = 1 'Letter head page no.
            intnrml = 3 'A4 page no.
            On Error GoTo 0

            If intpage * intcopies <> 0 Then
                For i = 1 To intcopies ' Loop to print next page of uer Choice Note: simplex is not working in my Office, Default Duplex
                    'Network Printer :- HP Laser Printer 5200dtn
                    'Print in letter head:- Choose Tray one
                    objWord.PrintOut Range:=4, Pages:=CStr(intpage) '1st page
                    intpage = intpage + 1
                    objWord.PrintOut Range:=4, Pages:=CStr(intpage) '2nd page
                    intpage = 1
                    'Print Other pages in A4:- Choose Tray Two
                    objWord.PrintOut Range:=4, Pages:=CStr(intnrml) '3rd page
                    intnrml = intnrml + 1
                    objWord.PrintOut Range:=4, Pages:=CStr(intnrml) '4th page
                    intnrml = intnrml + 1
                    objWord.PrintOut Range:=4, Pages:=CStr(intnrml) '5th page
                    intnrml = 3

                Next
            Else
                MsgBox "sorry, wrong page or copies, try again"
            End If
               ' Next
            objWord.Documents.Close
                ' set file to next in Dir
            fileName = Dir()
            Loop

1 个答案:

答案 0 :(得分:0)

我们使用一个名为Tray Selector的非常棒的实用程序,您可以从http://us.trayselector.com下载并免费试用。您可以设置一个单击按钮以打印到任何打印机和托盘组合,甚至可以使用VBA宏调用这些按钮。它在我们的律师事务所非常有用。