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 などであるかどうかを確認します。