How to enumerate all custom or built-in property

S

Sandeep Sharma

Hi,

While using Word 2003 with c# code, I can get value of a
particular document property say author as follow:
object oDocBuiltInProps;
###################
oDocBuiltInProps = aDoc.BuiltInDocumentProperties;
Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
//Display some built-in property - say Author,
string strIndex = "Author";
string strValue = "";
object oDocProp = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default | BindingFlags.GetProperty,
null,
oDocBuiltInProps,
new object[]{strIndex});

Type typeDocProp = oDocProp.GetType();
strValue = typeDocProp.InvokeMember("Value",
BindingFlags.Default | BindingFlags.GetProperty,
null,
oDocProp,
new object[]{}).ToString();

MessageBox.Show("Author of the document is: " + strValue);
##############

But I dont understand how to enumerate all document
properties. I want to list down all my
customDocumentProperties and update a few of them.


Pls help.
Thx.
Sandeep
 
C

Cindy M -WordMVP-

Hi Sandeep,
But I dont understand how to enumerate all document
properties. I want to list down all my
customDocumentProperties and update a few of them.
<ugh>C# really seems to make things complicated when
automating Word. I don't know how you'd write the c# code
for this, but in VBA-speak you ought to be able to do
something like

Sub ListDocProps()
Dim prop As Office.DocumentProperty

On Error GoTo Handler
For Each prop In
ActiveDocument.BuiltInDocumentProperties
Debug.Print prop.Name & prop.Value
Next prop
Exit Sub
Handler:
Select Case Err.Number
Case -2147467259
Debug.Print prop.Name & "***no value***"
Resume Next
Case Else
MsgBox Err.Number & vbCr & Err.Description
End Select
End Sub

You need the error handling (in your case, Try...Catch I
assume) because a particular property may not be valid for
the file type (Word document vs. Powerpoint slide or Excel
workbook) you're working with.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Sep 30 2003)
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
:)
 

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