收到COMException分配给Excel.Application.ActivePrinter时Excel、COMException、ActivePrinter、Application

2023-09-07 08:59:47 作者:血染、寂寞

我有一个函数,在其中我传递一个Microsoft.Office.Interop.Excel.Application实例。该功能使用Windows内置的传真打印机或Microsoft XPS文档编写器,将文件保存为TIFF图像。

I have a function, into which I'm passing an Microsoft.Office.Interop.Excel.Application instance. The function uses Windows' built in Fax printer, or the Microsoft XPS Document Writer, to save the file as a tiff image.

然而,当我尝试分配给应用程序的ActivePrinter属性,具有以下消息收到COMException被抛出:

However, when I try to assign to the application's ActivePrinter property, a COMException with the following message is thrown:

从HRESULT异常:0x800A03EC

Exception from HRESULT: 0x800A03EC

这里的code:

'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应用程序类工作正常。 我是pretty的新COM相关的东西,我有一种感觉,这可能只是我在玩的excel相关的无知,但我能找到谷歌搜索/计算器时,除了一两个与此几乎没有老了,没有答案的线程。有一些是涉及到大量数据/大范围,而不是ActivePrinter属性

The printers used are all confirmed as installed/in the registry. A similar function that takes a Word application class works fine. I'm pretty new to COM related stuff, and I have a feeling this may just be my excel-related ignorance at play, but I can find almost nothing relating to this when searching google/stackoverflow, except for one or two old, unanswered threads. There are a few that relate to large amounts of data/large ranges, but not the ActivePrinter property

编辑 - 答案的一个小结,在并购帕特尔的链接详细说明:

EDIT - A brief summary of the answer, detailed in M Patel's link:

Excel是挑剔,设置它的ActivePrinter属性;而不是单独的打印机名称,它同时需要打印机和端口,如在Ne01传真:。 该端口应该可以从注册表中,无论是在:

Excel is picky about setting it's ActivePrinter property; instead of printer name alone, it requires both printer and port, e.g. "Fax on Ne01:". This port should be available from the registry, either at:

HKEY_CURRENT_USER \软件\微软\的Windows NT \ CURRENTVERSION \设备

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices

HKEY_LOCAL_MACHINE \ SOFTWARE \微软\的Windows NT \ CURRENTVERSION \设备

强强强 助力企业疫情防控,员工健康信息智能自动化 收集 汇总 分析

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Devices

使用中的链接详细说明的方法,或在我的情况下使用Microsoft.Win32.Registry.GetValue()。后者将返回的东西线沿线的WINSPOOL,Ne01:。 串联到的方式在打印机名称字符串的最后部分上Ne01传真:,允许ActivePrinter属性要没有异常设置

Using the method detailed in the link, or in my case using Microsoft.Win32.Registry.GetValue(). The latter will return something along the lines of "winspool,Ne01:". Concatenating the last part of that string on to the printer name in the manner "Fax on Ne01:", allows the ActivePrinter property to be set without the exception.

我还应该注意到,我的问题是发生在Excel 2010

I should also note that my problem was occurring in excel 2010

推荐答案

也许下面的链接将帮助?它看起来像你分配给ActivePrinter属性事项的名称格式。

Maybe the link below will help? It looks like the formatting of the name you assign to the ActivePrinter property matters.

http://netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-the-heck-can-i-find-the-right-printer-name-and-port-combination-that-excel-wants.aspx

 
精彩推荐
图片推荐