1

Microsoft.Office.Interop.Excel.Application インスタンスを渡す関数があります。この関数は、Windows の組み込みの Fax プリンターまたは 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関連の無知に過ぎないのではないかと感じています.スレッド。大量のデータ/大きな範囲に関連するものはいくつかありますが、ActivePrinter プロパティには関連しません。

編集 - M Patelのリンクに詳述されている回答の簡単な要約:

Excel は ActivePrinter プロパティの設定にうるさいです。プリンター名だけではなく、プリンターとポートの両方が必要です (例: "Fax on 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で発生していたことにも注意してください

4

2 に答える 2

2

Excel相互運用を使用してExcelドキュメントを印刷しているときに、同じ例外が発生しました。

MS Word を使用:-

document.Application.ActivePrinter = "Brother MFC.. Printer"; // Works without exception

しかし、MS Excel では :-

document.Application.ActivePrinter = "Brother MFC.. Printer"; // throws COM exception

以下は、オフィス相互運用機能を使用して任意のオフィス (MS Word、MS Excel、PS Powerpoint) ドキュメントを印刷するための一般的な機能です。

    void PrintToPrinter(dynamic app, dynamic document, string printer, int numberOfCopies)
    {
        bool PrintToFile = false;

        // Trying to print document without activation throws print exception
        document.Activate();

        // The only way to change printer is to set the default printer of document or of application
        // Remember the active printer name to reset after printing document with intended printer
        oldPrinterName = document.Application.ActivePrinter;

        for (int retry = 0; retry < retryLimit; retry++)
        {
            try
            {
                if (!GetActivePrinter(document).Contains(printer))
                {
                    try
                    {
                        document.Application.ActivePrinter = printer;
                        docPrinterChanged = true;                            
                    }
                    catch (Exception)
                    {
                        try
                        {
                            app.ActivePrinter = printer;
                            appPrinterChanged = true;
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
                object oMissing = System.Reflection.Missing.Value;
                document.PrintOut(
                   true,            // Background
                   false,           // Append overwrite
                   oMissing,        // Page Range
                   oMissing,        // Print To File - OutputFileName
                   oMissing,        // From page
                   oMissing,        // To page
                   oMissing,        // Item
                   numberOfCopies,  // Number of copies to be printed
                   oMissing,        //
                   oMissing,        //
                   PrintToFile,     // Print To file
                   true             // Collate
                   );
                break;
            }
            catch (Exception)
            {
                continue;
            }
        }
        try
        {
            if(docPrinterChanged)
                document.Application.ActivePrinter = oldPrinterName;
            else if(appPrinterChanged)
                app.ActivePrinter = oldPrinterName;   
        }
        catch (Exception)
        {
        }
    }

    private static string GetActivePrinter(dynamic document)
    {

        string activePrinter = document.Application.ActivePrinter;

        if (activePrinter.Length >= 0)
            return activePrinter;
        return null;
    }

上記の関数を MS Excel に使用する場合、以下に示すようにプリンター名を更新します。MS Excelインスタンスから上記の関数にプリンター名を渡しながら、私は使用します

    bool IFilePrint.PrintFile(string fullFileName, string printerName, int numberOfCopies)
    {

        // .......

        Excel.Workbook document = null;
        try
        {
            document = this.Application.Workbooks.Open(fullFileName);
        }
        catch
        {
            document = null;
        }

        string portNumber = null;

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

        // Get current concatenation string ('on' in en, 'auf' in de, etc..)
        var split = this.Application.ActivePrinter.Split(' ');
        if (split.Length >= 3)
            printerName = String.Format("{0} {1} {2}", printerName, split[split.Length - 2], port);

        PrintToPrinter(this.Application, document, printerName, numberOfCopies);

        // ...........
        }
        catch (Exception)
        { }
        result = true;   
        return result;
    }

これをさらに確認することができます:

1) PrinterSettings クラスを使用して目的のプリンターが存在するかどうかを確認します。

2) (ファイルに出力) 目的の印刷オプションが To PDF/To XPS/FAX などであるかどうかを確認します。

于 2016-02-01T15:19:58.280 に答える
1

下のリンクが参考になるかも?ActivePrinter プロパティに割り当てる名前の書式設定が重要なようです。

http://netindonesia.net/blogs/jimmy/archive/2011/02/25/how-to-change-the-active-printer-to-specific-printer-in-excel-using-net-and-how-まさかのプリンター名とポートの組み合わせが欲しい.aspx

于 2013-02-13T23:45:17.740 に答える