Solved

How can I programmatically remove Custom Document Properties from Word Documents?

Posted on 2008-09-29
6
5,580 Views
Last Modified: 2008-10-05
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 });

	

}

Open in new window

0
Comment
Question by:guxiyou
  • 4
6 Comments
 
LVL 8

Assisted Solution

by:Bradley Haynes
Bradley Haynes earned 200 total points
Comment Utility
I found this at:  http://www.aspose.com/community/forums/142488/custom-document-properties-yes-no-field/showthread.aspx

This may be what you need:
Document doc = new Document(@"Test064\in.doc");

doc.CustomDocumentProperties["test"].Value = false;

doc.Range.UpdateFields();

doc.Save(@"Test064\out.doc");
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 200 total points
Comment Utility
This is a tested Word macro to do that.
Sub ClearDocProperties(wdDoc As Word.Document)

    Dim wdProp As Office.DocumentProperty

    

    For Each wdProp In wdDoc.CustomDocumentProperties

        wdProp.Delete

    Next wdProp

End Sub

Open in new window

0
 

Author Comment

by:guxiyou
Comment Utility
Graham Skan:

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();

	}

}

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:guxiyou
Comment Utility
b_haynes:

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
 

Author Comment

by:guxiyou
Comment Utility
Thanks for both your help.  Here I found a solution that seems to work:
public void DeleteCustom()

{

	object oDocCustomProps = _document.CustomDocumentProperties;

	Type typeDocCustomProps = oDocCustomProps.GetType();
 

	string strIndex = "PropertyName";

	try

	{

		object oKBProp = typeDocCustomProps.InvokeMember("Item",

															 BindingFlags.Default |

															 BindingFlags.GetProperty,

															 null, oDocCustomProps,

															 new object[] { strIndex });

		Type typeKBProp = oKBProp.GetType();

		typeKBProp.InvokeMember("Delete", BindingFlags.Default | BindingFlags.InvokeMethod, null, oKBProp, null);

	}

	catch { }

}

Open in new window

0
 

Accepted Solution

by:
guxiyou earned 0 total points
Comment Utility
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# single quoting a dynamical sql statement. 4 32
.xltm file opens as .xlsx file 3 36
Expando 4 33
C# Reverse int in fast ways 6 27
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now