从 Excel VBA 打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7670855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:12:27  来源:igfitidea点击:

Printing from Excel VBA

vbaexcel-vbaexcel

提问by abcde123483

Trying to print the same excel sheet a number of times (e.g 100) with a cell incremented each time (e.g cell 4F).

尝试多次(例如 100)打印相同的 Excel 工作表,每次都会增加一个单元格(例如单元格 4F)。

I tried using

我尝试使用

Workbook_BeforePrint

工作簿_BeforePrint

to increment the cell, but it requires interaction with the "select printer" dialog for each printed sheet.

以增加单元格,但它需要与每个打印表的“选择打印机”对话框交互。

Would it be possible to make something like:

是否有可能制作类似的东西:

a = getIntegerUserInput()
for i in 1..a
   increment 4F with one
   print the sheet suppressing the "select printer" dialog
end for

Cheers

干杯

回答by Seyren

Have you selected a default printer?

您是否选择了默认打印机?

I used this:

我用过这个:

Sub printinc()
For i = 0 To 3
Range("A1").Value = Range("A1").Value + 1
Sheets("Sheet1").PrintOut
Next
End Sub

It printed 4 copies incrementing the value in cell A1 each time without prompting me for settings or printer selection.

它每次打印 4 个副本,增加单元格 A1 中的值,而不提示我进行设置或打印机选择。

回答by JMax

To print a sheet, you can use this kind of code (assuming you know on which printer you want to print) using PrintOut:

要打印一张纸,您可以使用以下代码(假设您知道要在哪台打印机上打印)PrintOut

Sub PrintFile()
    Dim curPrinter As String
    curPrinter = Application.ActivePrinter
    Application.ActivePrinter = "Myprinter"
    ActiveWindow.SelectedSheets.PrintOut 
    Application.ActivePrinter = curPrinter
End Sub

Hence, you can create a loop to increase a cell and print your worksheet with the increment.

因此,您可以创建一个循环来增加单元格并使用增量打印您的工作表。

By the way, you could do it using Before_printand if you don't want to display the print dialog, you can set Cancelto False while calling the procedure Private Sub Workbook_BeforePrint( Cancel As Boolean)(ref on MSDN)

顺便说一句,你可以使用它来做Before_print,如果你不想显示打印对话框,你可以Cancel在调用过程时设置为 False Private Sub Workbook_BeforePrint( Cancel As Boolean)(参考MSDN

You can also read this SO thread to prevent displaying the printing dialog: How do you prevent printing dialog when using Excel PrintOut method.

您还可以阅读此 SO 线程以防止显示打印对话框:How do you prevent printing dialog when using Excel PrintOut method

[EDIT] see Seyren's answer for a working solution on what you want. Yet, take care about the performance if you reallywanted to loop 100 times.

[编辑] 有关您想要的工作解决方案,请参阅 Seyren 的回答。但是,如果您真的想循环 100 次,请注意性能。

回答by Alex K.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    '//supress recursion into this event when we print
    Application.EnableEvents = False
    '//increment
    If Not IsNumeric(ActiveSheet.Range("A1").Value) Then ActiveSheet.Range("A1").Value = 0
    ActiveSheet.Range("A1").Value = ActiveSheet.Range("A1").Value + 1
    '//do a default print
    ActiveSheet.PrintOut
    Application.EnableEvents = True
    '//prevent the default print
    Cancel = True
End Sub