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:
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.
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.