multithreading in VSTO


I have a task to provide a button to stop running process and for that sake I am trying to use multithreading but it gave me exception on a certain point I don't understand how to deal with it please provide some solution. Here its code

private void btngenerateNew_Click(object sender, RibbonControlEventArgs e)
    ThreadObject threadObj = new ThreadObject();
    threadObj.sender = sender;
    threadObj.e = e;
    btnGen = new Thread(supportCall);
private void callGeneateBtn(ThreadObject threadObject)
private void supportCall(object parameter)
    ThreadObject ob = parameter as ThreadObject;
private void createBtn()
    Microsoft.Office.Tools.Ribbon.RibbonButton btnStop =  this.Factory.CreateRibbonButton();
    btnStop.Label = "Stop It";
    btnStop.Name = "btnStop";

I get exception on below code:

public string FetchSelectedProperty(string propertyType, string typeStr, ref Microsoft.Office.Interop.Word._Document document)
    string strPropValue = string.Empty;
    DocumentProperties properties;
    properties = (DocumentProperties)document.CustomDocumentProperties;
    object missing = System.Reflection.Missing.Value;

here its throwing the exception:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

please provide some solution

asked on Stack Overflow May 26, 2015 by Sumeet Vishwas • edited Feb 20, 2020 by Eliahu Aaron

1 Answer


First of all, don't use the Office objects on secondary threads. The fact is that Office applications use the single threaded apartment model. You may get an exception or unpredictable value when calling methods and properties from secondary threads.

Moreover, the Fluent UI (Ribbon UI) is a static thing in general. You can't use the following code to add new items:

    Microsoft.Office.Tools.Ribbon.RibbonButton btnStop =  this.Factory.CreateRibbonButton();
    btnStop.Label = "Stop It";
    btnStop.Name = "btnStop";

Instead, you need to use callbacks. You can read more about that in the in the following series of articles in MSDN:

And the How to: Read from and Write to Document Properties article describes how to read document properties. Be aware, sometimes you need to use the late binding technology to avoid exceptions on the way.

The How To Use Automation to Get and to Set Office Document Properties with Visual C# .NET article describes how to get custom document properties, for example:

oDocBuiltInProps = oDoc.BuiltInDocumentProperties;
Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

//Get the Author property and display it.
string strIndex = "Author";
string strValue;
object oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item", 
                           BindingFlags.Default | 
                           new object[] {strIndex} );
Type typeDocAuthorProp = oDocAuthorProp.GetType();
strValue = typeDocAuthorProp.InvokeMember("Value", 
                           BindingFlags.Default |
                           new object[] {} ).ToString();
MessageBox.Show( "The Author is: " + strValue,"Author" );
answered on Stack Overflow May 26, 2015 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0