将打印机设置回默认打印机的 VBA 代码

问题描述 投票:0回答:1

所以最近我不得不向遗留但 100% 仍在生产的 Excel 项目添加一些代码,以便它将打印工作簿或仅将页面打印为 PDF。我基本上借用了现有 Print_Page 和 Print_Shipper(工作簿)子例程的代码,然后对其进行了更改以执行此操作。

'set the active printer to Microsoft PDF printer using the function
   Application.ActivePrinter = FindPrinter("Microsoft Print to PDF")

因此,这工作得很好,但在同一个 Excel 会话中,如果他们想返回“打印页面”或“打印机发件人”(到打印机),它仍然会转到 PDF。

那么打印PDF子例程运行后,如何使用VBA将其设置回默认打印机?每个人都有不同的打印机名称,我不让 Windows 设置默认值,但他们确实有一个默认设置。需要能够提取“默认值”并将其设置回原来的值。

vba printing default
1个回答
0
投票

请尝试一下。

Sub demo()
    Dim sPrinter As String
    sPrinter = Application.ActivePrinter
    Application.ActivePrinter = FindPrinter("Microsoft Print to PDF")
    ' Your code
    '...
    'Restore printer setting
    Application.ActivePrinter = sPrinter
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.