Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2005

Tip: Looking for answers? Try searching our database.

re:How To Use Automation to Get and to Set Office Document Properties

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew Hilsher - 03 Feb 2005 21:03 GMT
I have a Word addin in C# that gets and sets Document properties.  I have
used the example in the "How To Use Automation to Get and to Set Office
Document Properties with Visual C# .NET" example. (
http://support.microsoft.com/?kbid=303296 ) My problem is, If i try and read
a property such as "Category" that is not set yet, it is null.  I get an
error at runtime, object reference not set to an instance of an object.

The example they give in the article above reads the "Author" property which
is never null.  How can I check to see if it is null before I read it and get
this error?  Here is the sample code from the article.

Thanks in advance,
Andrew
ahilsher@yahoo.com

//Get the Author property and display it.
  string strIndex = "Author";
  string strValue;
  object oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
                             BindingFlags.Default |
                             BindingFlags.GetProperty,
                             null,oDocBuiltInProps,
                             new object[] {strIndex} );
  Type typeDocAuthorProp = oDocAuthorProp.GetType();
  strValue = typeDocAuthorProp.InvokeMember("Value",
                             BindingFlags.Default |
                             BindingFlags.GetProperty,
                             null,oDocAuthorProp,
                             new object[] {} ).ToString();
  MessageBox.Show( "The Author is: " + strValue,"Author" );
Leigh - 04 Feb 2005 05:01 GMT
Why not just use a Try ... Catch block?
Jezebel - 04 Feb 2005 05:21 GMT
As Leigh says, the usual method is just to try it and trap the error. If you
don't like that, I there's no option but to iterate the collection.

>I have a Word addin in C# that gets and sets Document properties.  I have
> used the example in the "How To Use Automation to Get and to Set Office
[quoted text clipped - 29 lines]
>                              new object[] {} ).ToString();
>   MessageBox.Show( "The Author is: " + strValue,"Author" );
Andrew Hilsher - 04 Feb 2005 06:33 GMT
Thanks Leigh and Jez,
That worked!

=o)

> As Leigh says, the usual method is just to try it and trap the error. If you
> don't like that, I there's no option but to iterate the collection.
[quoted text clipped - 32 lines]
> >                              new object[] {} ).ToString();
> >   MessageBox.Show( "The Author is: " + strValue,"Author" );
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.