You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA中Printer与Printers对象是否需引用库?报错求助

Excel VBA中Printer与Printers对象的问题解决

关于引用库的说明

  • Printer和Printers是Excel VBA的内置对象,不需要在【工具->引用】中额外添加任何引用库,可直接使用。

代码报错的原因及修复

你的代码存在两个关键问题:

  1. 对象调用错误:Printers集合必须通过Application对象访问,直接写Printers会被VBA识别为未定义变量,触发“object is required”错误。
  2. 变量大小写不一致:循环变量定义为prt,但输出时写成了Prt,VBA对变量大小写敏感,会导致无法正确调用对象属性。

修复后的代码:

Sub ListPrinters()
    Dim prt As Printer
    For Each prt In Application.Printers
        Debug.Print prt.DeviceName
    Next prt
End Sub

额外排查点

如果修复后Printers仍为空,需检查系统配置:

  • 确认Windows系统中已安装至少一台打印机(包括虚拟打印机)
  • 确保Excel有访问打印机的权限,可尝试重启Excel或系统后重试

内容的提问来源于stack exchange,提问作者RedMapple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 17:04:49