2

I am trying to control the Excel application via a WPF project.

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

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

It works fine for few times, after few days the same line gives an exception shown below. Logging off and logging on to the system fixes issue temporarily.

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: 

How can I solve this issue?

mm8
  • 163,881
  • 10
  • 57
  • 88
kaliprasad
  • 11
  • 6

1 Answers1

1

Setting Width is not working when Excel window is maximized.

Before setting Width property you should set xla.WindowState = xlNormal

smartobelix
  • 748
  • 5
  • 16
  • hi.. ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application(); ws.Cells[1, 1].Font.Bold = true; when the compiler encounters the above line, it consume lot of time (min 20 sec), to complete that line. Even I tried to run these set of line in the Task.Factory.StartNew, but no use.. please help me on this. – kaliprasad Aug 01 '17 at 04:42
  • 20 seconds comes from application startup. `ExcelSettings.xla = new Microsoft.Office.Interop.Excel.Application();` means new Excel process is started. This time cannot be shortened. You can optimize later operations i.e. not close Excel between operations etc. What you can do - i.e. start Excel during loading your program to speed up later operations. – smartobelix Aug 01 '17 at 08:53
  • hi.. thanks for the response.. actually, system is not taking time to create the object, but setting the font bold to true is. ws.Cells[1, 1].Font.Bold = true; // this line.. – kaliprasad Aug 01 '17 at 10:42