N
Nicu
Hello!
I'm developing an addin for Microsoft Office 2003, using VS 2005 and VSTO
2005SE with all the updates. I got random, so weird, an exception that says:
Unable to cast COM object of type
'Microsoft.Office.Interop.Word.DocumentClass' to interface type
'Microsoft.Office.Interop.Word._Document'. This operation failed because the
QueryInterface call on the COM component for the interface with IID
'{0002096B-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)).
I searched over the internet but I couldn't find anything to help. I'm
releasing the COM objects used at ThisAddIn_Shutdown, with the following
lines of code:
private Office.CommandBarPopup foundMenu;
private Office.CommandBarPopup newMenuBar;
if (foundMenu != null) Marshal.ReleaseComObject(foundMenu);
if (newMenuBar != null) Marshal.ReleaseComObject(menuBar);
I do this even when I try to remove the menu bar:
private void RemoveMenuBar()
{
object temporary = true;
try
{
// If the found menu exists already
if (foundMenu != null) Marshal.ReleaseComObject(foundMenu);
foundMenu = (CommandBarPopup)
this.Application.CommandBars.ActiveMenuBar.FindControl(MsoControlType.msoControlPopup, missing, menuTag, true, true);
if (foundMenu != null)
{
foundMenu.Delete(temporary);
}
}
catch (Exception ex)
{
OfficeLibraryUtils.logger.Error(ex.Message);
OfficeLibraryUtils.HandleException(ex);
}
}
I read about the [MTAThread] over the internet. I tried to use it, but I
didn't get rid of this mighty weird exception.
What am I doing wrong? Please help. Hope someone can light my problem, it
will be very appreciated!
Thanks in advance,
Nicu
I'm developing an addin for Microsoft Office 2003, using VS 2005 and VSTO
2005SE with all the updates. I got random, so weird, an exception that says:
Unable to cast COM object of type
'Microsoft.Office.Interop.Word.DocumentClass' to interface type
'Microsoft.Office.Interop.Word._Document'. This operation failed because the
QueryInterface call on the COM component for the interface with IID
'{0002096B-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)).
I searched over the internet but I couldn't find anything to help. I'm
releasing the COM objects used at ThisAddIn_Shutdown, with the following
lines of code:
private Office.CommandBarPopup foundMenu;
private Office.CommandBarPopup newMenuBar;
if (foundMenu != null) Marshal.ReleaseComObject(foundMenu);
if (newMenuBar != null) Marshal.ReleaseComObject(menuBar);
I do this even when I try to remove the menu bar:
private void RemoveMenuBar()
{
object temporary = true;
try
{
// If the found menu exists already
if (foundMenu != null) Marshal.ReleaseComObject(foundMenu);
foundMenu = (CommandBarPopup)
this.Application.CommandBars.ActiveMenuBar.FindControl(MsoControlType.msoControlPopup, missing, menuTag, true, true);
if (foundMenu != null)
{
foundMenu.Delete(temporary);
}
}
catch (Exception ex)
{
OfficeLibraryUtils.logger.Error(ex.Message);
OfficeLibraryUtils.HandleException(ex);
}
}
I read about the [MTAThread] over the internet. I tried to use it, but I
didn't get rid of this mighty weird exception.
What am I doing wrong? Please help. Hope someone can light my problem, it
will be very appreciated!
Thanks in advance,
Nicu