Link to home
Start Free TrialLog in
Avatar of guxiyou
guxiyou

asked on

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

Open in new window

SOLUTION
Avatar of Bradley Haynes
Bradley Haynes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guxiyou
guxiyou

ASKER

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

Avatar of guxiyou

ASKER

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.
Avatar of guxiyou

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial