Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this exception?

I am getting this exception while reading the shapes in excel sheet in c#: on code line of

if (worksheet.Shapes.Count >= iCurrentRowIndex)
{ }

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel._Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).

Edited:

This application locally running perfect but when i deploy it on IIS , it throws exception. What should the reason? I use code for thread as

string strImageSavedPath = string.Empty;
ThreadStart cbThreadStater = delegate { strImageSavedPath =  CopyToClipBoard(worksheet, (int)iRowindex, strApplicationPath); };
Thread thrd = new Thread(cbThreadStater);
thrd.SetApartmentState(ApartmentState.STA);
thrd.Start();
thrd.Join(); 

Where CopyToClipBoard method reads the image of supplied row index, saves the image in file system and return the path.

System.InvalidCastException was unhandled Message="Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel._Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))." Source="Microsoft.Office.Interop.Excel"
StackTrace: at Microsoft.Office.Interop.Excel._Worksheet.get_Shapes() at KGD6ExcelReader.ExcelManager.CopyToClipBoard(Worksheet worksheet, Int32 iRowindex, String strApplicationPath) in D:\ParallelMinds\Shared\KGD6ExcelReader\KGD6ExcelReader\ExcelManager.cs:line 522 at KGD6ExcelReader.ExcelManager.<>c__DisplayClass3.b__0() in D:\ParallelMinds\Shared\KGD6ExcelReader\KGD6ExcelReader\ExcelManager.cs:line 376 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

like image 492
Red Swan Avatar asked Nov 24 '25 00:11

Red Swan


2 Answers

Using Office automation objects from IIS is not recommended and not supported. So the fact that your app runs successfully locally, and unsuccessfully on IIS, is not entirely unexpected.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

That doesn't mean you can't try, but make sure you understand everything in the "Considerations for server-side Automation of Office" support article before you proceed.

like image 153
Joe White Avatar answered Nov 25 '25 16:11

Joe White


The COM interface reference (probably worksheet) that you are trying to use is for a proxy that is in a different appartment to the current thread. See link text.

Try marshalling the reference to the current thread.

like image 41
Andy Johnson Avatar answered Nov 25 '25 15:11

Andy Johnson