2017-05-30 6 views
2

ExcelアプリケーションをWPFプロジェクトで制御しようとしています。Excelアプリケーションで幅の設定に関する例外が発生する

var xla = new Microsoft.Office.Interop.Excel.Application(); 

xla.Width = 400; // This line is throwing the below exception. 

数行でうまく動作しますが、数日後には同じ行に例外が表示されます。ログオフしてシステムにログオンすると、問題が一時的に修正されます。

System.Runtime.InteropServices.COMException was unhandled 
    HResult=-2146827284 
    Message=Exception from HRESULT: 0x800A03EC 
    Source=Microsoft.Office.Interop.Excel 
    ErrorCode=-2146827284 
    StackTrace: 
     at Microsoft.Office.Interop.Excel.ApplicationClass.set_Width(Double RHS) 
     at EmbedExcel1.Form1.button1_Click(Object sender, EventArgs e) in d:\Test Projects\EmbedExcel1\EmbedExcel1\Form1.cs:line 27 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at EmbedExcel1.Program.Main() in d:\Test Projects\EmbedExcel1\EmbedExcel1\Program.cs:line 19 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

どうすればこの問題を解決できますか?

+0

Windowsフォームアプリケーションを設定する必要があります。正しいものは何ですか? – dymanoid

答えて

1

Excelウィンドウが最大化されている場合、設定幅が機能していません。

Widthプロパティを設定する前に、あなたはxla.WindowState = xlNormalあなたはWPFは、スタックトレースを示して使用言っている

+0

ありがとうございます..それは働いています.. – user2131048

+0

ハイ.. ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application(); ws.Cells [1、1] .Font.Bold = true; コンパイラが上記の行に遭遇すると、その行を完了するのに多くの時間(min 20 sec)を消費します。 でも、Task.Factory.StartNewでこれらの行を実行しようとしましたが、使用しません。 私にこれを手伝ってください。 – user2131048

+0

アプリケーションの起動から20秒が経過しています。 ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application();は、新しいExcelプロセスが開始されたことを意味します。この時間は短縮できません。後の操作を最適化することができます。つまり、操作間でExcelを閉じることはできません。できること - 後で操作を高速化するためにプログラムをロードする際にExcelを起動する。 – smartobelix

関連する問題