打印文件外部文件

时间:2013-10-15 18:13:45

标签: excel-vba vba excel

文件的打印件是从A列的单元格值到单个网络打印机(NE03 :),现在我想将从B列并行打印的文件打印到另一台连接的打印机(NE05:)

Excel文件格式:

Column 'A'-------------Column 'B'
C:\test\gear.pdf-------C:\invoice\chan.pdf
C:\test\court.pdf------C:\tyo\dated.pdf
...

有些人会帮我修改代码,这样我就可以连续打印2台打印机,从A列到打印机HP LaserJet Professional M1213nf MFP,从B列打印到打印机HP LaserJet P1106。

    Sub PrintPDFFiles() 
    zProg = "C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe" 
    zLastRow = [a65536].End(xlUp).Row 
    temp = "a1:a" & zLastRow 
    zPrinter = "HP LaserJet Professional M1213nf MFP " 
    For Each cell In Range(temp) 
    zFile = cell.Value 
    If zFile Like "*.pdf" Then 
    Shell """" & zProg & """/n /t """ & zFile & """" 
    End If 
    Next 
    End Sub

1 个答案:

答案 0 :(得分:1)

我相信您使用的是Excel 2003.您绝不应该对65536之类的值进行硬编码。您可以在xl2007 +中获得不良结果,因为它们有1048576行。

这是你在尝试什么? (的 UNTESTED

Option Explicit
'~~> Printer names
Const Prin1 As String = "HP LaserJet Professional M1213nf MFP"
Const Prin2 As String = "HP LaserJet P1106"
Const AcroPath As String = "C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe"

Private Declare Function SetDefaultPrinter Lib "winspool.drv" _
Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i  As Long
    Dim prt As String

    '~~> Get current default printer
    prt = Application.ActivePrinter

    '~~> Set this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Get last row of col A
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Set default printer
        SetDefaultPrinter Prin1

        '~~> Print from Col A first
        For i = 1 To lRow
            Shell """" & AcroPath & """/n /t """ & .Range("A" & i).Value & """"
            DoEvents
        Next i

        '~~> Get last row of col B
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        '~~> Set default printer
        SetDefaultPrinter Prin2

        '~~> Print from Col B Next
        For i = 1 To lRow
            Shell """" & AcroPath & """/n /t """ & .Range("B" & i).Value & """"
            DoEvents
        Next i
    End With

    SetDefaultPrinter prt
End Sub