Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-09-29
6
Medium Priority
?
6,091 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 8

Assisted Solution

by:Bradley Haynes
Bradley Haynes earned 800 total points
ID: 22601689
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 800 total points
ID: 22603869
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
ID: 22606490
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:guxiyou
ID: 22606567
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
ID: 22608087
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
ID: 22608164
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

688 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