我已经在互联网上搜索过,但找不到确切的解决方案...我需要能够从我的 VBA 代码中设置用于打印的颜色和双面设置。
我的目的是使用 VBA 用户窗体按钮来格式化并打印工作簿中的表格。我的缩写代码是:
Application.PrintCommunication = False
With ws.PageSetup
.PrintArea = first & ":" & last
.PrintTitleRows = "$1:$1"
.LeftHeader = "&9&D &T"
.CenterHeader = "&A"
.RightHeader = "&9Page &P of &N"
.Orientation = xlLandscape
.PaperSize = xlPaper11x17
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
End With
Application.PrintCommunication = True
ws.PrintOut Preview:=True, ActivePrinter:="BODHPM750"
这会格式化页面并打开预览窗口,但它默认为黑白单面打印(根据办公室的打印机设置),除非我在打开工作簿后手动更改了它。这些设置与打印机相关联,而不是与工作簿相关联,因此我无法通过 ws.PageSetup 或在预览窗口中更改它们。
编辑:我还意识到 ActivePrinter 定义不起作用。正在为我的计算机上设置的默认打印机打开打印预览,该打印机与我尝试在 ws.PrintOut 命令中调用的打印机不同。
有没有办法让我在按下按钮时将其设置为彩色和双面打印?我找到了有关在 Access 和其他 Office 程序中使用打印机对象的信息,但似乎没有适用于 Excel 的信息。
仅供引用 - 我在 Excel 2016 中使用 VBA,并且我尝试使用的打印机是办公室服务器上的 HP Color LaserJet M750 PCL 6(如果相关的话)。
更新:颜色问题已解决。
最佳答案
部分解决方案:
颜色设置可以通过页面设置完成
我的 PrintOut 调用中的 ActivePrinter 声明不起作用,因为我没有完全按照系统需要的打印机名称。为了查找打印机名称,我通过 Excel 文件>打印对话框将所需的打印机设置为我的事件打印机,然后输出打印机的名称,如下所示:
要在消息框中显示:
MsgBox Application.ActivePrinter
要输出到单元格以便复制粘贴文本:
Sheets("Sheet 1").Range("A1").Value = Application.ActivePrinter
将打印机设置更改为双面打印的解决方法: 更改事件打印机并在 PrintOut 命令之前打开打印对话框: Application.ActivePrinter = "Ne09 上的\SHAHQPS2\BODHPM750DTN1P:" Application.Dialogs(xlDialogPrinterSetup).Show 这将弹出一个对话框,可以在其中更改打印机设置。
未解决:仍在寻找解决方案,以便在代码中将打印机设置更改为双面打印,而不是打开“打印设置”对话框。
当前代码:
'Set print settings
Application.PrintCommunication = False 'Freeze print communication for efficiency
With ws.PageSetup
.PrintArea = first & ":" & last
.PrintTitleRows = "$1:$1"
.LeftHeader = "&9&D &T"
.CenterHeader = "&A"
.RightHeader = "&9Page &P of &N"
.Orientation = xlLandscape
.PaperSize = xlPaper11x17
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
End With
Application.PrintCommunication = True 'Restart print communication
'Set active printer to desired printer so print dialog box defaults to highlighting
'that printer; then open print dialog box so printer settings can be set to print
'duplex before the print preview comes up
'Note: could also use this to choose between printers
Application.ActivePrinter = "BODHPM750 on Ne09:"
Application.Dialogs(xlDialogPrinterSetup).Show
'Send to printer (preview first)
ws.PrintOut Preview:=True, ActivePrinter:="BODHPM750 on Ne09:"
关于excel - Excel 2016 中的 VBA - 在代码中设置打印机设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52426657/