Group:  Microsoft Word ยป microsoft.public.word.oleinterop
Thread: CustomDocumentProperties Questions

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

CustomDocumentProperties Questions
tartancli 16.08.2006 21:01:02
Hi, I have some questions about the functionality of
CustomDocumentProperties. I have written methods to both add and check
CustomDocumentProperties. Both of these functions throw exceptions when I use
late binding to invoke the methods "add" and "item". However, if i ignore the
exceptions the functions seem to work. The exception that I get is
"TargetInvocationException SystemArgumentException the parameter is
incorrect". As I say the methods seem to work ok, but i would like to know
the reason for the exception? Also, I subsequently want to change the value
of the property that i have added. How can i do this? I tried adding (again)
but this does not work.
The code that i am using is as follows:



public static void addCustomProperties( Word.Document oDoc, String index,
String value )
{
object oDocCustomProps = oDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();

try
{
String strIndex = index;
String strValue = value;
object[] oArgs = {strIndex,false,
MsoDocProperties.msoPropertyTypeString,
strValue};

typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
BindingFlags.InvokeMethod, null,
oDocCustomProps, oArgs );
}
catch (Exception e )
{
//It seems that an exception is thrown if here but it still
// seems to set the Custom Protperty alright. So, just
// sink the exception here
}

} // end addCustomProperies

public static String CheckCustomDocumentProperties( Word.Document oDoc,
String index )
{
object oDocCustomProps = oDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
String strIndex = index;
String strValue;

try
{
object oDocOfficesivityTypeProp = typeDocCustomProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocCustomProps,
new object[] {strIndex} );

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

}
catch (Exception e)
{
// It seems that an Exception is thrown if the Custom Property does not
exist
// Just sink the exception here
strValue = "Not Found";
}
//System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
document is: " + strValue );

return strValue;

} // end CheckCustomDocumentProperties


Re: CustomDocumentProperties Questions
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 17.08.2006 11:20:55
Try using document variables rather than document properties.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
news:94D47176-C853-4B8C-95B3-959586528DB7[ at ]microsoft.com...
[Quoted Text]
> Hi, I have some questions about the functionality of
> CustomDocumentProperties. I have written methods to both add and check
> CustomDocumentProperties. Both of these functions throw exceptions when I
> use
> late binding to invoke the methods "add" and "item". However, if i ignore
> the
> exceptions the functions seem to work. The exception that I get is
> "TargetInvocationException SystemArgumentException the parameter is
> incorrect". As I say the methods seem to work ok, but i would like to know
> the reason for the exception? Also, I subsequently want to change the
> value
> of the property that i have added. How can i do this? I tried adding
> (again)
> but this does not work.
> The code that i am using is as follows:
>
>
>
> public static void addCustomProperties( Word.Document oDoc, String index,
> String value )
> {
> object oDocCustomProps = oDoc.CustomDocumentProperties;
> Type typeDocCustomProps = oDocCustomProps.GetType();
>
> try
> {
> String strIndex = index;
> String strValue = value;
> object[] oArgs = {strIndex,false,
> MsoDocProperties.msoPropertyTypeString,
> strValue};
>
> typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
> BindingFlags.InvokeMethod, null,
> oDocCustomProps, oArgs );
> }
> catch (Exception e )
> {
> //It seems that an exception is thrown if here but it still
> // seems to set the Custom Protperty alright. So, just
> // sink the exception here
> }
>
> } // end addCustomProperies
>
> public static String CheckCustomDocumentProperties( Word.Document oDoc,
> String index )
> {
> object oDocCustomProps = oDoc.CustomDocumentProperties;
> Type typeDocCustomProps = oDocCustomProps.GetType();
> String strIndex = index;
> String strValue;
>
> try
> {
> object oDocOfficesivityTypeProp = typeDocCustomProps.InvokeMember("Item",
> BindingFlags.Default |
> BindingFlags.GetProperty,
> null,oDocCustomProps,
> new object[] {strIndex} );
>
> Type typeDocOfficesivityTypeProp = oDocOfficesivityTypeProp.GetType();
> strValue = typeDocOfficesivityTypeProp.InvokeMember("Value",
> BindingFlags.Default |
> BindingFlags.GetProperty,
> null,oDocOfficesivityTypeProp,
> new object[] {} ).ToString();
>
> }
> catch (Exception e)
> {
> // It seems that an Exception is thrown if the Custom Property does not
> exist
> // Just sink the exception here
> strValue = "Not Found";
> }
> //System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
> document is: " + strValue );
>
> return strValue;
>
> } // end CheckCustomDocumentProperties
>
>


Re: CustomDocumentProperties Questions
"Peter Jamieson" <pjj[ at ]KillmapSpjjnet.demon.co.uk> 18.08.2006 00:16:07
Is a C++ "String" (which I think is what you are using) definitely the
correct variable type to use in this call? I would guess a bStr or variant
may be more appropriate, but I'm just guessing.

Peter Jamieson
"tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
news:94D47176-C853-4B8C-95B3-959586528DB7[ at ]microsoft.com...
[Quoted Text]
> Hi, I have some questions about the functionality of
> CustomDocumentProperties. I have written methods to both add and check
> CustomDocumentProperties. Both of these functions throw exceptions when I
> use
> late binding to invoke the methods "add" and "item". However, if i ignore
> the
> exceptions the functions seem to work. The exception that I get is
> "TargetInvocationException SystemArgumentException the parameter is
> incorrect". As I say the methods seem to work ok, but i would like to know
> the reason for the exception? Also, I subsequently want to change the
> value
> of the property that i have added. How can i do this? I tried adding
> (again)
> but this does not work.
> The code that i am using is as follows:
>
>
>
> public static void addCustomProperties( Word.Document oDoc, String index,
> String value )
> {
> object oDocCustomProps = oDoc.CustomDocumentProperties;
> Type typeDocCustomProps = oDocCustomProps.GetType();
>
> try
> {
> String strIndex = index;
> String strValue = value;
> object[] oArgs = {strIndex,false,
> MsoDocProperties.msoPropertyTypeString,
> strValue};
>
> typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
> BindingFlags.InvokeMethod, null,
> oDocCustomProps, oArgs );
> }
> catch (Exception e )
> {
> //It seems that an exception is thrown if here but it still
> // seems to set the Custom Protperty alright. So, just
> // sink the exception here
> }
>
> } // end addCustomProperies
>
> public static String CheckCustomDocumentProperties( Word.Document oDoc,
> String index )
> {
> object oDocCustomProps = oDoc.CustomDocumentProperties;
> Type typeDocCustomProps = oDocCustomProps.GetType();
> String strIndex = index;
> String strValue;
>
> try
> {
> object oDocOfficesivityTypeProp = typeDocCustomProps.InvokeMember("Item",
> BindingFlags.Default |
> BindingFlags.GetProperty,
> null,oDocCustomProps,
> new object[] {strIndex} );
>
> Type typeDocOfficesivityTypeProp = oDocOfficesivityTypeProp.GetType();
> strValue = typeDocOfficesivityTypeProp.InvokeMember("Value",
> BindingFlags.Default |
> BindingFlags.GetProperty,
> null,oDocOfficesivityTypeProp,
> new object[] {} ).ToString();
>
> }
> catch (Exception e)
> {
> // It seems that an Exception is thrown if the Custom Property does not
> exist
> // Just sink the exception here
> strValue = "Not Found";
> }
> //System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
> document is: " + strValue );
>
> return strValue;
>
> } // end CheckCustomDocumentProperties
>
>


Re: CustomDocumentProperties Questions
tartancli 21.08.2006 14:07:02

Hi Doug,

Thanks for suggestion. I have tried changing to use variables. It seems to
work, but i do get unexpected exception here also. When i try to do something
like Variable.get_Item() i get an exception "object has been deleted"

Also, what is the main differences between using variables and custom
properties? Thanks again


"Doug Robbins - Word MVP" wrote:

[Quoted Text]
> Try using document variables rather than document properties.
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP
>
> "tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
> news:94D47176-C853-4B8C-95B3-959586528DB7[ at ]microsoft.com...
> > Hi, I have some questions about the functionality of
> > CustomDocumentProperties. I have written methods to both add and check
> > CustomDocumentProperties. Both of these functions throw exceptions when I
> > use
> > late binding to invoke the methods "add" and "item". However, if i ignore
> > the
> > exceptions the functions seem to work. The exception that I get is
> > "TargetInvocationException SystemArgumentException the parameter is
> > incorrect". As I say the methods seem to work ok, but i would like to know
> > the reason for the exception? Also, I subsequently want to change the
> > value
> > of the property that i have added. How can i do this? I tried adding
> > (again)
> > but this does not work.
> > The code that i am using is as follows:
> >
> >
> >
> > public static void addCustomProperties( Word.Document oDoc, String index,
> > String value )
> > {
> > object oDocCustomProps = oDoc.CustomDocumentProperties;
> > Type typeDocCustomProps = oDocCustomProps.GetType();
> >
> > try
> > {
> > String strIndex = index;
> > String strValue = value;
> > object[] oArgs = {strIndex,false,
> > MsoDocProperties.msoPropertyTypeString,
> > strValue};
> >
> > typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
> > BindingFlags.InvokeMethod, null,
> > oDocCustomProps, oArgs );
> > }
> > catch (Exception e )
> > {
> > //It seems that an exception is thrown if here but it still
> > // seems to set the Custom Protperty alright. So, just
> > // sink the exception here
> > }
> >
> > } // end addCustomProperies
> >
> > public static String CheckCustomDocumentProperties( Word.Document oDoc,
> > String index )
> > {
> > object oDocCustomProps = oDoc.CustomDocumentProperties;
> > Type typeDocCustomProps = oDocCustomProps.GetType();
> > String strIndex = index;
> > String strValue;
> >
> > try
> > {
> > object oDocOfficesivityTypeProp = typeDocCustomProps.InvokeMember("Item",
> > BindingFlags.Default |
> > BindingFlags.GetProperty,
> > null,oDocCustomProps,
> > new object[] {strIndex} );
> >
> > Type typeDocOfficesivityTypeProp = oDocOfficesivityTypeProp.GetType();
> > strValue = typeDocOfficesivityTypeProp.InvokeMember("Value",
> > BindingFlags.Default |
> > BindingFlags.GetProperty,
> > null,oDocOfficesivityTypeProp,
> > new object[] {} ).ToString();
> >
> > }
> > catch (Exception e)
> > {
> > // It seems that an Exception is thrown if the Custom Property does not
> > exist
> > // Just sink the exception here
> > strValue = "Not Found";
> > }
> > //System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
> > document is: " + strValue );
> >
> > return strValue;
> >
> > } // end CheckCustomDocumentProperties
> >
> >
>
>
>
Re: CustomDocumentProperties Questions
tartancli 21.08.2006 14:10:02
Hi Peter,

Thanks for suggestion. I noticed that the MS samples use "string" instead
of "String" so I changed to use "string". I still get exception though when i
check for a property that has not been set (unspecified error) and when
adding a property that has already been set. I therefore, still cannot change
the value of a property that has been previously added.

"Peter Jamieson" wrote:

[Quoted Text]
> Is a C++ "String" (which I think is what you are using) definitely the
> correct variable type to use in this call? I would guess a bStr or variant
> may be more appropriate, but I'm just guessing.
>
> Peter Jamieson
> "tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
> news:94D47176-C853-4B8C-95B3-959586528DB7[ at ]microsoft.com...
> > Hi, I have some questions about the functionality of
> > CustomDocumentProperties. I have written methods to both add and check
> > CustomDocumentProperties. Both of these functions throw exceptions when I
> > use
> > late binding to invoke the methods "add" and "item". However, if i ignore
> > the
> > exceptions the functions seem to work. The exception that I get is
> > "TargetInvocationException SystemArgumentException the parameter is
> > incorrect". As I say the methods seem to work ok, but i would like to know
> > the reason for the exception? Also, I subsequently want to change the
> > value
> > of the property that i have added. How can i do this? I tried adding
> > (again)
> > but this does not work.
> > The code that i am using is as follows:
> >
> >
> >
> > public static void addCustomProperties( Word.Document oDoc, String index,
> > String value )
> > {
> > object oDocCustomProps = oDoc.CustomDocumentProperties;
> > Type typeDocCustomProps = oDocCustomProps.GetType();
> >
> > try
> > {
> > String strIndex = index;
> > String strValue = value;
> > object[] oArgs = {strIndex,false,
> > MsoDocProperties.msoPropertyTypeString,
> > strValue};
> >
> > typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
> > BindingFlags.InvokeMethod, null,
> > oDocCustomProps, oArgs );
> > }
> > catch (Exception e )
> > {
> > //It seems that an exception is thrown if here but it still
> > // seems to set the Custom Protperty alright. So, just
> > // sink the exception here
> > }
> >
> > } // end addCustomProperies
> >
> > public static String CheckCustomDocumentProperties( Word.Document oDoc,
> > String index )
> > {
> > object oDocCustomProps = oDoc.CustomDocumentProperties;
> > Type typeDocCustomProps = oDocCustomProps.GetType();
> > String strIndex = index;
> > String strValue;
> >
> > try
> > {
> > object oDocOfficesivityTypeProp = typeDocCustomProps.InvokeMember("Item",
> > BindingFlags.Default |
> > BindingFlags.GetProperty,
> > null,oDocCustomProps,
> > new object[] {strIndex} );
> >
> > Type typeDocOfficesivityTypeProp = oDocOfficesivityTypeProp.GetType();
> > strValue = typeDocOfficesivityTypeProp.InvokeMember("Value",
> > BindingFlags.Default |
> > BindingFlags.GetProperty,
> > null,oDocOfficesivityTypeProp,
> > new object[] {} ).ToString();
> >
> > }
> > catch (Exception e)
> > {
> > // It seems that an Exception is thrown if the Custom Property does not
> > exist
> > // Just sink the exception here
> > strValue = "Not Found";
> > }
> > //System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
> > document is: " + strValue );
> >
> > return strValue;
> >
> > } // end CheckCustomDocumentProperties
> >
> >
>
>
>
Re: CustomDocumentProperties Questions
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 21.08.2006 19:14:09
To access the value of a document variable using vba, you need to use

ActiveDocument.Variables("varname").Value

The difference between using document variables and document properties is
that the value of a variable can only be set or deleted using vba. They can
both however be used to display the information stored in the variable or
property by using the respective field type docvariable or docproperty.

I find variables easier to work with.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
news:5720B644-AF6D-4EE8-AC1C-C3EFB36ED065[ at ]microsoft.com...
[Quoted Text]
>
> Hi Doug,
>
> Thanks for suggestion. I have tried changing to use variables. It seems to
> work, but i do get unexpected exception here also. When i try to do
> something
> like Variable.get_Item() i get an exception "object has been deleted"
>
> Also, what is the main differences between using variables and custom
> properties? Thanks again
>
>
> "Doug Robbins - Word MVP" wrote:
>
>> Try using document variables rather than document properties.
>>
>> --
>> Hope this helps.
>>
>> Please reply to the newsgroup unless you wish to avail yourself of my
>> services on a paid consulting basis.
>>
>> Doug Robbins - Word MVP
>>
>> "tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
>> news:94D47176-C853-4B8C-95B3-959586528DB7[ at ]microsoft.com...
>> > Hi, I have some questions about the functionality of
>> > CustomDocumentProperties. I have written methods to both add and check
>> > CustomDocumentProperties. Both of these functions throw exceptions when
>> > I
>> > use
>> > late binding to invoke the methods "add" and "item". However, if i
>> > ignore
>> > the
>> > exceptions the functions seem to work. The exception that I get is
>> > "TargetInvocationException SystemArgumentException the parameter is
>> > incorrect". As I say the methods seem to work ok, but i would like to
>> > know
>> > the reason for the exception? Also, I subsequently want to change the
>> > value
>> > of the property that i have added. How can i do this? I tried adding
>> > (again)
>> > but this does not work.
>> > The code that i am using is as follows:
>> >
>> >
>> >
>> > public static void addCustomProperties( Word.Document oDoc, String
>> > index,
>> > String value )
>> > {
>> > object oDocCustomProps = oDoc.CustomDocumentProperties;
>> > Type typeDocCustomProps = oDocCustomProps.GetType();
>> >
>> > try
>> > {
>> > String strIndex = index;
>> > String strValue = value;
>> > object[] oArgs = {strIndex,false,
>> > MsoDocProperties.msoPropertyTypeString,
>> > strValue};
>> >
>> > typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
>> > BindingFlags.InvokeMethod, null,
>> > oDocCustomProps, oArgs );
>> > }
>> > catch (Exception e )
>> > {
>> > //It seems that an exception is thrown if here but it still
>> > // seems to set the Custom Protperty alright. So, just
>> > // sink the exception here
>> > }
>> >
>> > } // end addCustomProperies
>> >
>> > public static String CheckCustomDocumentProperties( Word.Document oDoc,
>> > String index )
>> > {
>> > object oDocCustomProps = oDoc.CustomDocumentProperties;
>> > Type typeDocCustomProps = oDocCustomProps.GetType();
>> > String strIndex = index;
>> > String strValue;
>> >
>> > try
>> > {
>> > object oDocOfficesivityTypeProp =
>> > typeDocCustomProps.InvokeMember("Item",
>> > BindingFlags.Default |
>> > BindingFlags.GetProperty,
>> > null,oDocCustomProps,
>> > new object[] {strIndex} );
>> >
>> > Type typeDocOfficesivityTypeProp = oDocOfficesivityTypeProp.GetType();
>> > strValue = typeDocOfficesivityTypeProp.InvokeMember("Value",
>> > BindingFlags.Default |
>> > BindingFlags.GetProperty,
>> > null,oDocOfficesivityTypeProp,
>> > new object[] {} ).ToString();
>> >
>> > }
>> > catch (Exception e)
>> > {
>> > // It seems that an Exception is thrown if the Custom Property does not
>> > exist
>> > // Just sink the exception here
>> > strValue = "Not Found";
>> > }
>> > //System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
>> > document is: " + strValue );
>> >
>> > return strValue;
>> >
>> > } // end CheckCustomDocumentProperties
>> >
>> >
>>
>>
>>


Re: CustomDocumentProperties Questions
"Peter Jamieson" <pjj[ at ]KillmapSpjjnet.demon.co.uk> 22.08.2006 10:33:34
[Quoted Text]
> I still get exception though when i
> check for a property that has not been set (unspecified error)

Yes,
a. in Word's UI, you cannot set the value of a Custom Document Property to
""
b. in VBA, you can set the value to "" and the property survives. However,
it would not surprise me if properties set to "" are not always handled
correctly
c. if a property does not exist, trying to access it using
DocProperties("the property name") will cause an "Invalid procedure call or
argument" exception.
d. You cannot use .Add to add a property whose name already exists

Not sure (a) or (b) is relevant, but (c) and (d) mean you are experiencing
normal behaviour. In VBA, you would typically have to check that a name
existed either by using DocProperties("the property name") and trapping the
error, or using a For Each loop to look through the values - at its simplest

Dim objCustomProperty As DocumentProperty
Dim objCustomProperties As DocumentProperties
For Each objCustomProperty In ActiveDocument.CustomDocumentProperties
Debug.Print objCustomProperty.Name, objCustomProperty.Value
Next

Once you know a property exists, you should be able to assign a new value.
otherwise, you should be able to .Add a new one safely.

I don't know how you do the equivalent in C/C variants.

I think most of the above also applies to Document Variables as mentioned by
Doug Robbins.

Peter Jamieson

"tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
news:A9A90A3C-C5C4-4395-9191-B74057F3BFB0[ at ]microsoft.com...
> Hi Peter,
>
> Thanks for suggestion. I noticed that the MS samples use "string" instead
> of "String" so I changed to use "string". I still get exception though
> when i
> check for a property that has not been set (unspecified error) and when
> adding a property that has already been set. I therefore, still cannot
> change
> the value of a property that has been previously added.
>
> "Peter Jamieson" wrote:
>
>> Is a C++ "String" (which I think is what you are using) definitely the
>> correct variable type to use in this call? I would guess a bStr or
>> variant
>> may be more appropriate, but I'm just guessing.
>>
>> Peter Jamieson
>> "tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
>> news:94D47176-C853-4B8C-95B3-959586528DB7[ at ]microsoft.com...
>> > Hi, I have some questions about the functionality of
>> > CustomDocumentProperties. I have written methods to both add and check
>> > CustomDocumentProperties. Both of these functions throw exceptions when
>> > I
>> > use
>> > late binding to invoke the methods "add" and "item". However, if i
>> > ignore
>> > the
>> > exceptions the functions seem to work. The exception that I get is
>> > "TargetInvocationException SystemArgumentException the parameter is
>> > incorrect". As I say the methods seem to work ok, but i would like to
>> > know
>> > the reason for the exception? Also, I subsequently want to change the
>> > value
>> > of the property that i have added. How can i do this? I tried adding
>> > (again)
>> > but this does not work.
>> > The code that i am using is as follows:
>> >
>> >
>> >
>> > public static void addCustomProperties( Word.Document oDoc, String
>> > index,
>> > String value )
>> > {
>> > object oDocCustomProps = oDoc.CustomDocumentProperties;
>> > Type typeDocCustomProps = oDocCustomProps.GetType();
>> >
>> > try
>> > {
>> > String strIndex = index;
>> > String strValue = value;
>> > object[] oArgs = {strIndex,false,
>> > MsoDocProperties.msoPropertyTypeString,
>> > strValue};
>> >
>> > typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
>> > BindingFlags.InvokeMethod, null,
>> > oDocCustomProps, oArgs );
>> > }
>> > catch (Exception e )
>> > {
>> > //It seems that an exception is thrown if here but it still
>> > // seems to set the Custom Protperty alright. So, just
>> > // sink the exception here
>> > }
>> >
>> > } // end addCustomProperies
>> >
>> > public static String CheckCustomDocumentProperties( Word.Document oDoc,
>> > String index )
>> > {
>> > object oDocCustomProps = oDoc.CustomDocumentProperties;
>> > Type typeDocCustomProps = oDocCustomProps.GetType();
>> > String strIndex = index;
>> > String strValue;
>> >
>> > try
>> > {
>> > object oDocOfficesivityTypeProp =
>> > typeDocCustomProps.InvokeMember("Item",
>> > BindingFlags.Default |
>> > BindingFlags.GetProperty,
>> > null,oDocCustomProps,
>> > new object[] {strIndex} );
>> >
>> > Type typeDocOfficesivityTypeProp = oDocOfficesivityTypeProp.GetType();
>> > strValue = typeDocOfficesivityTypeProp.InvokeMember("Value",
>> > BindingFlags.Default |
>> > BindingFlags.GetProperty,
>> > null,oDocOfficesivityTypeProp,
>> > new object[] {} ).ToString();
>> >
>> > }
>> > catch (Exception e)
>> > {
>> > // It seems that an Exception is thrown if the Custom Property does not
>> > exist
>> > // Just sink the exception here
>> > strValue = "Not Found";
>> > }
>> > //System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
>> > document is: " + strValue );
>> >
>> > return strValue;
>> >
>> > } // end CheckCustomDocumentProperties
>> >
>> >
>>
>>
>>


Re: CustomDocumentProperties Questions
tartancli 23.08.2006 08:23:02
Thanks Doug and Peter. Most helpful!

"Peter Jamieson" wrote:

[Quoted Text]
> > I still get exception though when i
> > check for a property that has not been set (unspecified error)
>
> Yes,
> a. in Word's UI, you cannot set the value of a Custom Document Property to
> ""
> b. in VBA, you can set the value to "" and the property survives. However,
> it would not surprise me if properties set to "" are not always handled
> correctly
> c. if a property does not exist, trying to access it using
> DocProperties("the property name") will cause an "Invalid procedure call or
> argument" exception.
> d. You cannot use .Add to add a property whose name already exists
>
> Not sure (a) or (b) is relevant, but (c) and (d) mean you are experiencing
> normal behaviour. In VBA, you would typically have to check that a name
> existed either by using DocProperties("the property name") and trapping the
> error, or using a For Each loop to look through the values - at its simplest
>
> Dim objCustomProperty As DocumentProperty
> Dim objCustomProperties As DocumentProperties
> For Each objCustomProperty In ActiveDocument.CustomDocumentProperties
> Debug.Print objCustomProperty.Name, objCustomProperty.Value
> Next
>
> Once you know a property exists, you should be able to assign a new value.
> otherwise, you should be able to .Add a new one safely.
>
> I don't know how you do the equivalent in C/C variants.
>
> I think most of the above also applies to Document Variables as mentioned by
> Doug Robbins.
>
> Peter Jamieson
>
> "tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
> news:A9A90A3C-C5C4-4395-9191-B74057F3BFB0[ at ]microsoft.com...
> > Hi Peter,
> >
> > Thanks for suggestion. I noticed that the MS samples use "string" instead
> > of "String" so I changed to use "string". I still get exception though
> > when i
> > check for a property that has not been set (unspecified error) and when
> > adding a property that has already been set. I therefore, still cannot
> > change
> > the value of a property that has been previously added.
> >
> > "Peter Jamieson" wrote:
> >
> >> Is a C++ "String" (which I think is what you are using) definitely the
> >> correct variable type to use in this call? I would guess a bStr or
> >> variant
> >> may be more appropriate, but I'm just guessing.
> >>
> >> Peter Jamieson
> >> "tartancli" <tartancli[ at ]discussions.microsoft.com> wrote in message
> >> news:94D47176-C853-4B8C-95B3-959586528DB7[ at ]microsoft.com...
> >> > Hi, I have some questions about the functionality of
> >> > CustomDocumentProperties. I have written methods to both add and check
> >> > CustomDocumentProperties. Both of these functions throw exceptions when
> >> > I
> >> > use
> >> > late binding to invoke the methods "add" and "item". However, if i
> >> > ignore
> >> > the
> >> > exceptions the functions seem to work. The exception that I get is
> >> > "TargetInvocationException SystemArgumentException the parameter is
> >> > incorrect". As I say the methods seem to work ok, but i would like to
> >> > know
> >> > the reason for the exception? Also, I subsequently want to change the
> >> > value
> >> > of the property that i have added. How can i do this? I tried adding
> >> > (again)
> >> > but this does not work.
> >> > The code that i am using is as follows:
> >> >
> >> >
> >> >
> >> > public static void addCustomProperties( Word.Document oDoc, String
> >> > index,
> >> > String value )
> >> > {
> >> > object oDocCustomProps = oDoc.CustomDocumentProperties;
> >> > Type typeDocCustomProps = oDocCustomProps.GetType();
> >> >
> >> > try
> >> > {
> >> > String strIndex = index;
> >> > String strValue = value;
> >> > object[] oArgs = {strIndex,false,
> >> > MsoDocProperties.msoPropertyTypeString,
> >> > strValue};
> >> >
> >> > typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
> >> > BindingFlags.InvokeMethod, null,
> >> > oDocCustomProps, oArgs );
> >> > }
> >> > catch (Exception e )
> >> > {
> >> > //It seems that an exception is thrown if here but it still
> >> > // seems to set the Custom Protperty alright. So, just
> >> > // sink the exception here
> >> > }
> >> >
> >> > } // end addCustomProperies
> >> >
> >> > public static String CheckCustomDocumentProperties( Word.Document oDoc,
> >> > String index )
> >> > {
> >> > object oDocCustomProps = oDoc.CustomDocumentProperties;
> >> > Type typeDocCustomProps = oDocCustomProps.GetType();
> >> > String strIndex = index;
> >> > String strValue;
> >> >
> >> > try
> >> > {
> >> > object oDocOfficesivityTypeProp =
> >> > typeDocCustomProps.InvokeMember("Item",
> >> > BindingFlags.Default |
> >> > BindingFlags.GetProperty,
> >> > null,oDocCustomProps,
> >> > new object[] {strIndex} );
> >> >
> >> > Type typeDocOfficesivityTypeProp = oDocOfficesivityTypeProp.GetType();
> >> > strValue = typeDocOfficesivityTypeProp.InvokeMember("Value",
> >> > BindingFlags.Default |
> >> > BindingFlags.GetProperty,
> >> > null,oDocOfficesivityTypeProp,
> >> > new object[] {} ).ToString();
> >> >
> >> > }
> >> > catch (Exception e)
> >> > {
> >> > // It seems that an Exception is thrown if the Custom Property does not
> >> > exist
> >> > // Just sink the exception here
> >> > strValue = "Not Found";
> >> > }
> >> > //System.Windows.Forms.MessageBox.Show( "The OfficesivityType of the
> >> > document is: " + strValue );
> >> >
> >> > return strValue;
> >> >
> >> > } // end CheckCustomDocumentProperties
> >> >
> >> >
> >>
> >>
> >>
>
>
>

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net