简体   繁体   English

通过自动化在MS Word中设置正确的打印机

[英]Setting correct printer in MS Word through automation

I have the following automation code: 我有以下自动化代码:

lPrintSetup := fWordObject.Application.Dialogs.Item(wdDialogFilePrintSetup);
lPrintSetup.Printer := 'MyPrinter';
lPrintSetup.DoNotSetAsSysDefault := True;
lPrintSetup.Execute;
lPrintSetup := Null;

The Printer property is giving me some problems, sometimes Execute crashes with an EOleException (0x800A1460 (error code 5216): There is a printer error) because of a wrong printername. Printer属性给我一些问题,有时由于错误的打印机名称而导致EOleException(0x800A1460(错误代码5216):存在打印机错误)导致Execute崩溃。

I have printer information of all printers in a _PRINTER_INFO_2 record which I obtained by a EnumPrinters API-call. 我通过EnumPrinters API调用获得的_PRINTER_INFO_2记录中具有所有打印机的打印机信息。 How can I compose the right printername for Word given the information in a _PRINTER_INFO_2 record? 给定_PRINTER_INFO_2记录中的信息,如何为Word组成正确的_PRINTER_INFO_2 It has work with at least Windows 2000, Word 2000 and Citrix. 它至少可与Windows 2000,Word 2000和Citrix一起使用。

Some background info: 一些背景信息:

Our application first filled the Printername with a self constructed printername. 我们的应用程序首先用一个自建的打印机名填充Printername。 This gave problems with Citrix clients, so for Citrix clients we took the _PRINTER_INFO_2.pPortName and deleted the Client:#: part. 这给Citrix客户端带来了问题,因此对于Citrix客户端,我们采用_PRINTER_INFO_2.pPortName并删除了Client:#:部分。 This is working for the majority of our customers, but sometimes still the printer error shows up. 这适用于大多数客户,但有时仍会显示打印机错误。

What I have tried so far (on Windows XP SP3, Word 2007): 到目前为止,我已经尝试过(在Windows XP SP3,Word 2007上):

  • Just take the _PRINTER_INFO_2.pPrinterName . 只需使用_PRINTER_INFO_2.pPrinterName Problem is that when you modify printernames on purpose (renaming 'PDFCreator' to 'HP DESKJET 520 on MYPC') it crashes on the latter (while selecting this printer in Word works). 问题是,当您有意修改打印机名称时(将“ PDFCreator”重命名为“ MYPC上的HP DESKJET 520”),它将在后者上崩溃(在Word中选择此打印机时有效)。
  • Composing a printername like this: lPrintSetup.Printer := PRINTER_INFO_2.pPrinterName + ' on ' + PRINTER_INFO_2.pPortname . 像这样组成一个lPrintSetup.Printer := PRINTER_INFO_2.pPrinterName + ' on ' + PRINTER_INFO_2.pPortnamelPrintSetup.Printer := PRINTER_INFO_2.pPrinterName + ' on ' + PRINTER_INFO_2.pPortname Seems to work always! 似乎总是可以工作! But googling around showed that ' on ' is localized, so I'm not sure if that's going to work on non-english Windows versions. 但是谷歌搜索显示“ on”已本地化,因此我不确定这是否适用于非英语的Windows版本。 Edit: does not work always :( 编辑:并不总是工作:(

Another solution I found on the web: 我在网上找到的另一个解决方案:

When reading the printername from Word it has the form of "Printername on Ne01:", where Ne01 is ranged from Ne00: to Ne99:. 从Word读取打印机名称时,其格式为“ Ne01:上的打印机名称”,其中Ne01的范围从Ne00:到Ne99:。 The solution suggested taking the printername and just try to set it while looping from Ne00: to Ne99:. 该解决方案建议采用打印机名称,并尝试在从Ne00:到Ne99:循环时进行设置。 When .Execute doesn't crash, you've got the right one. 当.Execute不崩溃时,您已经找到了正确的选择。 I'm not very fond of this 'trail and error' method. 我不太喜欢这种“跟踪与错误”方法。

I'm not sure if you've tried this, or if its of any use, but you can get a list of all the printers on the system from the Printer.Printers object make sure you add Printers to the Uses clause of your unit. 我不确定您是否尝试过此方法,或者它是否有任何用途,但是您可以从Printer.Printers对象获取系统上所有打印机的列表,确保将Printers添加到设备的Uses子句中。

This should then list the actual names on the system and you may be able to use this information to do what you want. 然后,这应该列出系统上的实际名称,您也许可以使用此信息来执行所需的操作。

As stated you can get a list of printer names using the Printer.Printers which is a TStringList with the name of the printer on each item. 如前所述,您可以使用Printer.Printers获取一个打印机名称列表,该列表是一个TStringList,其中每个项目均带有打印机名称。

This code gives the default printer name 此代码提供默认的打印机名称

Printer.Printers[Printer.PrinterIndex]

几分钟前,我了解到,word2k不仅希望使用打印机名称,例如“ Ne01上的打印机名称:”,还希望端口(NEnn)大写“ NE01上的打印机名称:”

I figured it out. 我想到了。 Word has the printername in the form of "Printername on NE01:". Word具有“ NE01上的打印机名称:”形式的打印机名称。 Ne01: is the printerport as specified in the devices section of win.ini . Ne01:是win.inidevice部分中指定的打印机端口。 So now I compose the printername as _PRINTER_INFO_2.pPrinterName + ' on ' + <PrinterPort from win.ini> and set that name for the printer property of the FilePrintSetup dialog. 因此,现在我_PRINTER_INFO_2.pPrinterName + ' on ' + <PrinterPort from win.ini>打印机名称组成为_PRINTER_INFO_2.pPrinterName + ' on ' + <PrinterPort from win.ini>并为FilePrintSetup对话框的打印机属性设置该名称。

This is much better than to resort to the trail-and-error method mentioned in my question. 这比求助于我在问题中提到的跟踪和错误方法要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM