简体   繁体   English

c#设置打印机

[英]c# setting a printer

Hello and thank you for you time on this question, I'm trying change a Active Printer according to the choice that user chooses in excel.您好,感谢您在这个问题上的时间,我正在尝试根据用户在 excel 中选择的选项更改活动打印机。 However I'm having some trouble.但是,我遇到了一些麻烦。 For some reason it keeps giving me the same error.出于某种原因,它不断给我同样的错误。

"An exception of type 'System.Runtime.InteropServices.COM Exception' occurred in DailyReport.dll but was not handled in user code Exception from HRESULT:0X800A03EC" “DailyReport.dll 中发生了‘System.Runtime.InteropServices.COM Exception’类型的异常,但未在来自 HRESULT:0X800A03EC 的用户代码异常中处理”

I've been goggling this error and im having a hard time finding anything, I did find a link COM Exception and they provided a link to another website but seems when I try to go that site it doesn't open.我一直在盯着这个错误,我很难找到任何东西,我确实找到了一个链接COM 异常,他们提供了一个指向另一个网站的链接,但似乎当我尝试去那个网站时它没有打开。

I have Tried:我试过了:

xlApp.ActivePrinter = "CORPPRT58-Copier Room on RR-PS1:";

xlApp.ActivePrinter = "\\RR-PS1\CORPPRT58-Copier Room"; 

xlApp.ActivePrinter = "CORPPRT58-Copier Room on RR-PS1";

I have checked to make sure that the printer is installed and it is.我已检查以确保打印机已安装并且已安装。 if someone could point me in the correct direction that would be great thanks!!如果有人能指出我正确的方向,那就太感谢了!!

The correct answer is (ie):正确答案是(即):

xlApp.ActivePrinter = "\\\\RR-PS1\\CORPPRT58-Copier Room on Ne00:";

An important part is the 'Ne00:' This is the port on which this printer can be found.一个重要的部分是“Ne00:”这是可以找到这台打印机的端口。 This is different on each computer and can be found in registry at HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices .这在每台计算机上都不同,可以在HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices注册表中找到。

Another problem is the concatenation string 'on'.另一个问题是连接字符串“on”。 This may be valid when working with an English excel but it's translated to other languages!这在使用英语 excel 时可能有效,但它已被翻译成其他语言!

I had the same problem but there aren't many complete examples I could find so here is mine:我有同样的问题,但我找不到很多完整的例子,所以这里是我的:

// Open excel document
var path = @"c:\path\to\my\doc.xlsx";

Microsoft.Office.Interop.Excel.Application _xlApp;
Microsoft.Office.Interop.Excel.Workbook _xlBook;
_xlApp = new Microsoft.Office.Interop.Excel.Application();
_xlBook = _xlApp.Workbooks.Open(path);
_xlBook.Activate();

var printer = @"EPSON LQ-690 ESC/P2";
var port = String.Empty;

// Find correct printerport
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(path))
{
    if (key != null)
    {
        object value = key.GetValue(printer);
        if (value != null)
        {
            string[] values = value.ToString().Split(',');
            if (values.Length >= 2) port = values[1];
        }
    }
}

// Set ActivePrinter if not already set
if (!_xlApp.ActivePrinter.StartsWith(printer))
{
    // Get current concatenation string ('on' in enlgish, 'op' in dutch, etc..)
    var split = _xlApp.ActivePrinter.Split(' ');
    if (split.Length >= 3)
    {
        _xlApp.ActivePrinter = String.Format("{0} {1} {2}",
            printer, 
            split[split.Length - 2], 
            port);
    }
}

// Print document
_xlBook.PrintOutEx();

It's far for perfect since I'm not aware of any other translations.这太完美了,因为我不知道任何其他翻译。 If 'on' is translated with spaces, above will fail.如果 'on' 被翻译成空格,上面会失败。 But i'm guessing that the solution will work for most clients.但我猜测该解决方案适用于大多数客户。 You can easily get the current concatenation string by looking at the currect value of ActivePrinter.您可以通过查看 ActivePrinter 的当前值轻松获取当前连接字符串。

A more failproof method would be to strip the printer name and the assigned port and what remains is the concatenation string.一种更防故障的方法是去除打印机名称和分配的端口,剩下的是串联字符串。 But then you would have to loop through all installed printers and check for a match.但是,您必须遍历所有已安装的打印机并检查是否匹配。

Another test I personally do it check if the printer is installed on the system:我亲自做的另一个测试是检查系统上是否安装了打印机:

if(PrinterSettings.InstalledPrinters.Cast<string>().ToList().Contains(printer)) {
    //Set active printer...
}

First set your target printer as the default printer on the control pannel.首先在控制面板上将目标打印机设置为默认打印机。 Then print xlApp.ActivePrinter and get its settings(if you don't set the activeprinter the application gets the default setting of windows).然后打印 xlApp.ActivePrinter 并获取其设置(如果您不设置 activeprinter,应用程序将获取 Windows 的默认设置)。 At last set the value with the print value.最后用打印值设置该值。 And you can change your default printer setting.您可以更改默认打印机设置。

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

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