c#设置打印机 [英] c# setting a printer

查看:175
本文介绍了c#设置打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,感谢您在这个问题上的时间,我正在尝试根据用户在excel中选择的选项更改活动打印机。但是我有一些麻烦。由于某种原因,它不断给我同样的错误。

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. However I'm having some trouble. For some reason it keeps giving me the same error.


DailyReport.dll中发生了类型为System.Runtime.InteropServices.COM异常
的异常,但未处理在用户代码中,来自HRESULT的异常
:0X800A03EC

"An exception of type 'System.Runtime.InteropServices.COM Exception' occurred in DailyReport.dll but was not handled in user code Exception from HRESULT:0X800A03EC"

我一直在偷窥这个错误,任何事情,我找到一个链接 COM异常,他们提供了一个链接到另一个网站,但似乎当我尝试去那个网站它不打开。

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.

我曾经尝试过:

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!!

推荐答案

正确答案是(即):

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

重要的部分是Ne00:这是可以找到此打印机的端口。每个计算机上都有所不同,可以在注册表中找到 HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices

An important part is the 'Ne00:' This is the port on which this printer can be found. This is different on each computer and can be found in registry at HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices.

另一个问题是连接字符串'on'。这可能是有效的,当使用英语excel,但它被翻译成其他语言!

Another problem is the concatenation string 'on'. This may be valid when working with an English excel but it's translated to other languages!

我有同样的问题,但没有很多完整的例子,我可以这样找到是我的:

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();

这是非常完美的,因为我不知道任何其他的翻译。如果使用空格翻译on,则上面将失败。但是我猜测该解决方案对大多数客户端都是有效的。您可以通过查看ActivePrinter的正确值来轻松获取当前的级联字符串。

It's far for perfect since I'm not aware of any other translations. If 'on' is translated with spaces, above will fail. 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.

更多的故障保护方法是剥离打印机名称和分配的端口以及剩余的内容是连接字符串。但是,您必须循环使用所有已安装的打印机并检查是否匹配。

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.

另外测试我亲自检查打印机是否安装在系统上: p>

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...
}

这篇关于c#设置打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆