VBA:临时更改 Windows 7 默认打印机

发布于 2024-12-22 10:46:37 字数 279 浏览 7 评论 0原文

是否有可能仅更改一项“作业”的 Windows 默认打印机?在 Windows XP / Vista / 7 下无需管理员权限是否可以使用?

我想要做什么:

  • 默认打印机是 Canon ABC
  • 想要
  • 在打印后通过 Epson XYZ 打印想要再次恢复我的默认打印机 Canon ABC。

最好的情况是它无需管理权限且无需“消息框”或 UI 对话框即可工作。

应该在 VBA 下使用或(勉强)在 C# / .NET 下使用

is there any possibility to change the windows default printer for one "job" only? Does it work without having adminstrative permissions under Windows XP / Vista / 7?

what i want to do:

  • Default printer is Canon ABC
  • want to print via Epson XYZ
  • after printing want to having back my default printer Canon ABC again.

Best would be if it will work without administrative permissions and without "messageboxes" or UI Dialogs.

Should be used under VBA or (grudgingly) under C# / .NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡渣熟男 2024-12-29 10:46:37

在 C# 中,您可以打印到计算机上安装的任何打印机。您无需更改默认打印机。我不知道它在 VBA 中是否相同,但它在 C# 中有效。

编辑

由于您的评论,我添加了一个小示例,希望能够引导您:

public void DoPrint()
{
    var printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        var printDocument = new PrintDocument
            {
                DefaultPageSettings = { PrinterSettings = printDialog.PrinterSettings }
            };
        printDocument.PrintPage += OnPrintPage;
    }
}

private void OnPrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.DrawString("Hello");
}

这将在您在对话框中选择的打印机上打印“Hello”。

In C# you can print to any printer installed on the computer. You don't need to change the default printer. I don't know exactly if it's the same in VBA, but it works in C#.

Edit:

Due to your comment I added a little sample, hoping to lead you on the way:

public void DoPrint()
{
    var printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        var printDocument = new PrintDocument
            {
                DefaultPageSettings = { PrinterSettings = printDialog.PrinterSettings }
            };
        printDocument.PrintPage += OnPrintPage;
    }
}

private void OnPrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.DrawString("Hello");
}

This will print "Hello" to the printer you've selected in the dialog.

过潦 2024-12-29 10:46:37

来自 Access 帮助:
以下示例将 Printers 集合中的第一台打印机设置为系统的默认打印机,然后报告其名称、驱动程序信息和端口信息。

  Dim prtDefault As Printer
  dim strOldDefault as string
  strOldDefault = Application.printer

  Set Application.Printer = Application.Printers(0)

  Set prtDefault = Application.Printer

  With prtDefault
      MsgBox "Device name: " & .DeviceName & vbCr _
          & "Driver name: " & .DriverName & vbCr _
          & "Port: " & .Port
  End With

编辑:您可以在更改默认打印机之前轻松保存默认打印机,并在打印后重新设置。另外,由于更改默认打印机是一个缓慢的过程,我建议您在更改之前检查默认打印机是什么,以避免不必要的更改。


保存/恢复:

Dim strOldPrinter As String
'save old printer'
strOldPrinter = Application.Printer.DeviceName
'....do stuff'
'restore old Printer'
Set Application.Printer = Application.Printers(strOldPrinter)

From Access help:
The following example makes the first printer in the Printers collection the default printer for the system, and then reports its name, driver information, and port information.

  Dim prtDefault As Printer
  dim strOldDefault as string
  strOldDefault = Application.printer

  Set Application.Printer = Application.Printers(0)

  Set prtDefault = Application.Printer

  With prtDefault
      MsgBox "Device name: " & .DeviceName & vbCr _
          & "Driver name: " & .DriverName & vbCr _
          & "Port: " & .Port
  End With

Edit: You can easily save the default printer before changing it, and set it back again after printing. Also, since changing the default printer is a slow process, I would recommend that you check what is the default printer before changing it, in order to avoid un-necessary changes.


To save/restore:

Dim strOldPrinter As String
'save old printer'
strOldPrinter = Application.Printer.DeviceName
'....do stuff'
'restore old Printer'
Set Application.Printer = Application.Printers(strOldPrinter)
总攻大人 2024-12-29 10:46:37

由于您没有具体说明您的“工作”实际上是什么,我将在这里提供一个更简单的答案。

作为编程方法的替代方法,如果您专门尝试通过特定打印机打印特定的报告,MS Access 中的内置功能可以实现此目的。

只需选择报告的“打印预览”视图,然后选择:“

页面设置”->“页面设置”->“打印预览”。页面选项卡->使用特定打印机 ->然后浏览并选择!

这最终满足了我的需求,所以希望它也能帮助你。

Being that you did not specify what your "job" actually is, I will supply a simpler answer here.

As an alternative to the programmatic methods, if you are specifically trying to print a particular report via a certain printer, there is built-in functionality in MS Access to accomplish this.

Simply select the Print Preview view of your report, and then select:

Page Setup -> Page tab -> Use Specific Printer -> Then browse and choose!

This wound up working for my needs, so hopefully it will help you as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文