私は特定のワークシートのExcelの範囲でいくつかの値を書き込む必要があるC#単純なアプリケーションを持っています。 存在しない場合はExcelアプリケーションのインスタンスを作成しますが、存在する場合はアクティブに設定し、コード内で使用する場合はインスタンスを取得します。ハンドルでC#でExcelアプリケーションのインスタンスを取得
私は、新しいアプリケーションを作成するには、このコードを使用し:
Microsoft.Office.Interop.Excel app =
new Microsoft.Office.Interop.Excel.Application();
app.Visible = true;
私はでExcelアプリケーションのインスタンスを取得することができますどのようにこのAPI
[DllImportAttribute("User32.dll")]
private static extern int FindWindow(String ClassName, String WindowName);
を使用して、アクティブExcelウィンドウのハンドルを取得するにはハンドル?
int hWnd = FindWindow(null, "Microsoft Excel - MySheet.xlsx");
Microsoft.Office.Interop.Excel app = ....(hWnd)
私にとって、 'instance = new Excel.ApplicationClass();'はうまくいかなかった。私は 'instance = new Excel.Application();'を使う必要がありました。 –
@DavidMurdochこれは[interopタイプの埋め込みのためのVS2010の変更]の結果です(http://blogs.msdn.com/b/mshneer/archive/2009/12/07/interop-type-xxx-cannot-be -embedded-use-the-applicable-interface-instead.aspx)を使用します。回避策は、 'ApplicationClass()'の 'Application()'に切り替えることです。同じ結果が得られます。 – James