views:

218

answers:

2

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:

+1  A: 

Is the code running on a background thread? If so, you need to set the ApartmentState for the thread to STA - you'll have to new up an actual Thread to do this and call the SetApartmentState method.

http://goo.gl/elyd for more on that.

Coder 42
yes using thread. but still problem thr
Lalit
Not working for me
Lalit
+1  A: 

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.

Andy Johnson
Not working for me
Lalit
When its running locally, is that also under IIS? Or the Visual Studio integrated web server? Could you post the cross-thread marshaling code?
Andy Johnson