在两台不同的打印机上打印两张不同的纸

时间:2016-11-26 11:06:47

标签: excel vba printing

我正在尝试在两台不同的打印机(PrinterX& PrinterY)上打印不同的纸张(X& Y)。

Dim PrinterX as string
PrinterX=activeworkbook.Worksheets("Printers").Range("B1").value
Activerprinter = PrinterX
ActiveWorkbook.Worksheets("X").Printout

Dim PrinterY as string
PrinterY = Activeworkbook.Worksheets("Printers").Range("B2").value
Activerprinter = PrinterY
ActiveWorkbook.Worksheets("Y").Printout

==>我将范围(“B1”)中的打印机名称称为“Ne01上的HP LaserJet Professional P1102(Copy4)”&范围(“B2”)为“Ne02上的HP LaserJet Professional P1102(Copy1)”。

它在PrinterX上打印。我试图在PrinterX上打印X,在PrinterY上打印Y.

application.activeprinter会出错。

1 个答案:

答案 0 :(得分:0)

application.activeprinter 

必须是:

Application.ActivePrinter 

此外,您可能需要按如下方式重构代码:

Sub main()
    Dim iCell As Long


    With Worksheets("Printers").Range("B1:B2") '<--| reference range with printers
        For iCell = 1 To .Count '<--|loop through it
            Application.ActivePrinter = .Cells(iCell, 1).Value '<--| set activeprinter to current cell value
            Worksheets(Array("X", "Y")(iCell - 1)).PrintOut '<--| define array with corresponding wanted sheets names and printout current one
       Next iCell
    End With
End Sub

或者您可以直接在ActivePrinter方法中指定PrintOut()

Sub main()
    Dim iCell As Long

    With Worksheets("Printers").Range("B1:B2") '<--| reference range with printers
        For iCell = 1 To .Count '<--|loop through it
            Worksheets(Array("X", "Y")(iCell - 1)).PrintOut ActivePrinter:=.Cells(iCell, 1).Value '<--| define array with corresponding wanted sheets names and printout current setting ActivePrinter to current cell value
       Next iCell
    End With
End Sub