【发布时间】:2013-01-29 15:02:52
【问题描述】:
我有一个函数,我正在向其中传递一个 Microsoft.Office.Interop.Excel.Application 实例。该函数使用 Windows 的内置传真打印机或 Microsoft XPS Document Writer,将文件保存为 tiff 图像。
但是,当我尝试分配给应用程序的 ActivePrinter 属性时,会抛出带有以下消息的 COMException:
HRESULT 异常:0x800A03EC
代码如下:
'Save the current default printer
Dim strDefaultPrinter As String = excelApp.ActivePrinter
'Assign printer string constant to ActivePrinter - throws exception
excelApp.ActivePrinter = FAX_PRINTER
excelApp.ActiveWorkbook.PrintOutEx(, , , , , True, "c:\RestOfFilePath...") ' Print to file = true
'Reset the default printer
excelApp.ActivePrinter = strDefaultPrinter
所使用的打印机都被确认为已安装/在注册表中。采用 Word 应用程序类的类似函数可以正常工作。 我对 COM 相关的东西还很陌生,我觉得这可能只是我与 excel 相关的无知在起作用,但是在搜索 google/stackoverflow 时我几乎找不到任何与此相关的东西,除了一两个旧的、未回答的线程。有一些与大量数据/大范围有关,但与 ActivePrinter 属性无关
编辑 - 答案的简要总结,在 M Patel 的链接中有详细说明:
Excel 对设置它的 ActivePrinter 属性很挑剔;而不是单独的打印机名称,它需要打印机和端口,例如“Ne01 上的传真:”。 这个端口应该可以从注册表中获得,或者在:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices
或
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Devices
使用链接中详述的方法,或者在我的情况下使用 Microsoft.Win32.Registry.GetValue()。后者将返回类似于“winspool,Ne01:”的内容。 以“Fax on Ne01:”的方式将该字符串的最后一部分连接到打印机名称上,可以毫无例外地设置 ActivePrinter 属性。
我还应该注意,我的问题发生在 excel 2010 中
【问题讨论】:
-
在 Excel 2013 中问题依然存在。并且在 Excel 中将连接字符串与用户语言进行了翻译。我已将我的解决方案添加到 stackoverflow.com/questions/29921150/c-sharp-setting-a-printer/…
标签: .net vb.net excel com comexception