How can I programmatically remove Custom Document Properties from Word Documents?
I have been working on a project that requires me to be able to add and remove custom document properties from Word documents. It was fairly easy to find demonstrations on how to add/view Custom Document properties, but I have been unable to remove them. How can I do this? I've included examples I found of how to add/view.
public void ReadCustom(){ object oDocCustomProps = _document.CustomDocumentProperties; Type typeDocCustomProps = oDocCustomProps.GetType(); string strIndex = "Knowledge Base Article"; string strValue; try { object oKBProp = typeDocCustomProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, oDocCustomProps, new object[] { strIndex }); Type typeKBProp = oKBProp.GetType(); strValue = typeKBProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, oKBProp, new object[] { }).ToString(); MessageBox.Show("The KB is: " + strValue, "KB"); } catch (TargetInvocationException ex) { MessageBox.Show(strIndex + " does not exist"); }}public void DeleteCustom(){ object oDocCustomProps = _document.CustomDocumentProperties; Type typeDocCustomProps = oDocCustomProps.GetType(); string strIndex = "Knowledge Base Article"; string strValue; object oKBProp = typeDocCustomProps.InvokeMember("Remove", BindingFlags.Default | BindingFlags.GetProperty, null, oDocCustomProps, new object[] { strIndex });}
Sub ClearDocProperties(wdDoc As Word.Document) Dim wdProp As Office.DocumentProperty For Each wdProp In wdDoc.CustomDocumentProperties wdProp.Delete Next wdPropEnd Sub
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
I've see this example in several bogs/help sites, but it doesn't appear to work in C#. Whenever I try it I get errors, specifically I get:
"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)).
public void DeleteCustom(){ DocumentProperties properties = (DocumentProperties) _document.CustomDocumentProperties; foreach (DocumentProperty wdProp in properties) { wdProp.Delete(); }}
I tried the solution that you proposed, and it wouldn't compile, I got a, "Cannot apply indexing with [ ] to an expression of type 'object'. I'm tried casting it, but no matter how I cast it I still get problems.
0
guxiyouAuthor Commented:
Thanks for both your help. Here I found a solution that seems to work:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4e5f1023-6113-4b0a-b6f1-b18a9237c37a/