Change CustomDocumentProperties

L

lmxu

Hi,

I have a need to change certain custom propertie value of the word document.
I have a mehtod like so

private void changeWordCustomProperty(String changeText, String changeValue)
{
Word._Document oDoc;
object oDocCustomProps;
try
{
oDoc = this.wordApp.ActiveDocument;
oDocCustomProps = oDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
object[] oArgs = {changeText,false,
MsoDocProperties.msoPropertyTypeString,
changeValue};
typeDocCustomProps.InvokeMember("Item",BindingFlags.Default |
BindingFlags.SetProperty, null,
oDocCustomProps, new object[]{changeText,changeValue} );
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
}

using the debugger, I see the text and value passed in are both correct.
However, this mehtod is NOT changing the value of the custom property. after
it's called. I went to word file->properties->custom tab, i see the value is
still the same. What went wrong?

btw, using the debugger, i don't see exception being thrown either. Advice
and tips is really appreicated. thanks
 
C

Cindy M -WordMVP-

L

lmxu

Hello Cindy,

I think i've talked with you on google groups. You are everywhere! thanks
for your reply again.

yes, i did see this article and in fact, that's where my code was based on.

i used

//Set the Subject property.
strIndex = "Subject";
strValue = "The Subject";
typeDocAuthorProp.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null,oDocBuiltInProps,
new object[] {strIndex,strValue} );

The only different was i used oDoc.CustomDocumentProperites and he used
oDoc.BuiltInDocumentProperties cuz I wanted to mdoifiy the custom
properites.
 
C

Cindy M -WordMVP-

Hi Imxu,

Sorry about the delayed reply; I needed to find time while I had a C# system
available to test on...

I just tried out the code in the article, and that basic approach does work. As
best I can tell, you've skipped a step in your interpretation of the code. You
first need to get the "property bag" for a single property before you can set a
value for the property.

this worked for me:

object docProps = wdDoc.CustomDocumentProperties;
Type docPropsType = docProps.GetType();
object Prop = docPropsType.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, docProps,
new object[] {propName} );
Type PropType = Prop.GetType();
PropType.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null, docProps,
new object[] {propName,propValue} );
yes, i did see this article and in fact, that's where my code was based on.

i used

//Set the Subject property.
strIndex = "Subject";
strValue = "The Subject";
typeDocAuthorProp.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null,oDocBuiltInProps,
new object[] {strIndex,strValue} );

The only different was i used oDoc.CustomDocumentProperites and he used
oDoc.BuiltInDocumentProperties cuz I wanted to mdoifiy the custom
properites.

Cindy M -WordMVP- said:
Hi =?Utf-8?B?bG14dQ==?=,

Have you worked through this article, yet?

http://support.microsoft.com/default.aspx?scid=kb;en-us;303296

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in
the newsgroup and not by e-mail :)
 
L

lmxu

hi Cindy,

Just to let you know it works!! THANK YOU SO MUCH.

It's weird that I have to invoke an extra phrase

object Prop = docPropsType.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, docProps,
new object[] {propName} );
Type PropType = Prop.GetType();

when the next line has "SetProperty" using key, value pair. Like you said,
I assumed and skipped this one important step.

Really really appreicate your help again.

Cindy M -WordMVP- said:
Hi Imxu,

Sorry about the delayed reply; I needed to find time while I had a C# system
available to test on...

I just tried out the code in the article, and that basic approach does work. As
best I can tell, you've skipped a step in your interpretation of the code. You
first need to get the "property bag" for a single property before you can set a
value for the property.

this worked for me:

object docProps = wdDoc.CustomDocumentProperties;
Type docPropsType = docProps.GetType();
object Prop = docPropsType.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, docProps,
new object[] {propName} );
Type PropType = Prop.GetType();
PropType.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null, docProps,
new object[] {propName,propValue} );
yes, i did see this article and in fact, that's where my code was based on.

i used

//Set the Subject property.
strIndex = "Subject";
strValue = "The Subject";
typeDocAuthorProp.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null,oDocBuiltInProps,
new object[] {strIndex,strValue} );

The only different was i used oDoc.CustomDocumentProperites and he used
oDoc.BuiltInDocumentProperties cuz I wanted to mdoifiy the custom
properites.

Cindy M -WordMVP- said:
Hi =?Utf-8?B?bG14dQ==?=,

I have a need to change certain custom propertie value of the word document.

Have you worked through this article, yet?

http://support.microsoft.com/default.aspx?scid=kb;en-us;303296

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


This reply is posted in the Newsgroup; please post any follow question or reply in
the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top