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);
btnGen.Start(threadObj);
}
private void callGeneateBtn(ThreadObject threadObject)
{
btngenerate_Click(threadObject.sender,threadObject.e);
}
private void supportCall(object parameter)
{
ThreadObject ob = parameter as ThreadObject;
callGeneateBtn(ob);
}
private void createBtn()
{
Microsoft.Office.Tools.Ribbon.RibbonButton btnStop = this.Factory.CreateRibbonButton();
btnStop.Label = "Stop It";
btnStop.Name = "btnStop";
groupLicense.Items.Add(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
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";
groupLicense.Items.Add(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 |
BindingFlags.GetProperty,
null,oDocBuiltInProps,
new object[] {strIndex} );
Type typeDocAuthorProp = oDocAuthorProp.GetType();
strValue = typeDocAuthorProp.InvokeMember("Value",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocAuthorProp,
new object[] {} ).ToString();
MessageBox.Show( "The Author is: " + strValue,"Author" );
User contributions licensed under CC BY-SA 3.0