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" );