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 });
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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 { }
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Doc
Open in new window