Code for Adding Custom Document Property

J

JoAnn

I have revised an existing template to contain a new custom doc property.
Since old documents (not containing the new property) will sometimes be
attaching to the template, I need to add code that checks the attached
document for the existence of the property & if it's not there, create it.

I know the code I need to use to add the custom document
property(ActiveDocument.CustomDocumentProperties.Add ...) , but I don't know
the best way to check for the property & tell it to execute the code if the
property is missing.

Also ... I read in another email thread that it may not be a good idea to
create the customdocument property with ActiveDocument since the property
exists in the template but not in the open doc. If the property existing in
the template but not in the active document creates a conflict, then what
should I use instead to create it?

Thanks for your help.
 
J

Jean-Guy Marcil

JoAnn was telling us:
JoAnn nous racontait que :
I have revised an existing template to contain a new custom doc
property. Since old documents (not containing the new property) will
sometimes be attaching to the template, I need to add code that
checks the attached document for the existence of the property & if
it's not there, create it.

I know the code I need to use to add the custom document
property(ActiveDocument.CustomDocumentProperties.Add ...) , but I
don't know the best way to check for the property & tell it to
execute the code if the property is missing.

Also ... I read in another email thread that it may not be a good
idea to create the customdocument property with ActiveDocument since
the property exists in the template but not in the open doc. If the
property existing in the template but not in the active document
creates a conflict, then what should I use instead to create it?

Not quite. Active always refers to, wait for it... the active document! :)
But, if you want to refer to the attached template, it can be done, as long
as the code is part of the template, use ThisDocument instead of
ActiveDocument in the code below. This will refer to the template on which
the active document is based because ThisDocument refers to the document (or
template) that is the code container.
Thanks for your help.

Here is some code to get you going.

'_______________________________________
Dim propCustom As DocumentProperty
Dim i As Long
Const strPropName As String = "MyCustomProperty"

With ActiveDocument
If .CustomDocumentProperties.Count > 0 Then
For i = 1 To .CustomDocumentProperties.Count
If .CustomDocumentProperties(i).Name = strPropName Then
Set propCustom = .CustomDocumentProperties(i)
Exit For
End If
Next
End If

If propCustom Is Nothing Then
Set propCustom = .CustomDocumentProperties.Add( _
Name:=strPropName, _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="This is a custom property.")
End If

End With
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
B

Bear

JoAnn and Jean-Guy:

Jean-Guy's strategy is to try to find the custom property, and then set a
variable object to that custom property. He can then test whether or not the
variable object is Nothing -- meaning the custom property wasn't found, and
create it as required. All this happens without errors, no matter whether or
not the custom property exists.

I've also seen it done with an error-trapping strategy. You just try to do
create the custom property, and if that causes an error (because it was
already there) you say "oh well" and continue on with your processing.

On Error Resume Next
Set propCustom = .CustomDocumentProperties.Add( _
Name:=strPropName, _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="This is a custom property.")
On Error GoTo 0

I don't feel comfy with this strategy, but an experienced VBA programmer
showed it to me, and I may just be harboring novice-type prejudices.

Bear
 
P

Perry

I've also seen it done with an error-trapping strategy. You just try to do
create the custom property, and if that causes an error (because it was
already there) you say "oh well" and continue on with your processing.

True

To expand *with* an Error handling bookmark:
(whereby objDoc is a valid variable pointing to a Word document)

On Local Error GoTo ErrPropNotExists
objDoc.CustomDocumentProperties(strPropName).value = _
"MyValue"

'<< all the other code goes here
Exit Sub '(or Exit function) end of the subroutine

ErrPropNotExists:
Set propCustom = objDoc.CustomDocumentProperties.Add( _
Name:=strPropName, _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="MyValue")

Resume Next 'continue code execution

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
J

JoAnn

Thanks for the alternate approach ... I'll check it out.
--
JoAnn


Perry said:
I've also seen it done with an error-trapping strategy. You just try to do
create the custom property, and if that causes an error (because it was
already there) you say "oh well" and continue on with your processing.

True

To expand *with* an Error handling bookmark:
(whereby objDoc is a valid variable pointing to a Word document)

On Local Error GoTo ErrPropNotExists
objDoc.CustomDocumentProperties(strPropName).value = _
"MyValue"

'<< all the other code goes here
Exit Sub '(or Exit function) end of the subroutine

ErrPropNotExists:
Set propCustom = objDoc.CustomDocumentProperties.Add( _
Name:=strPropName, _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="MyValue")

Resume Next 'continue code execution

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
T

Toni

Pls, I try to do the same in an ADP but i can't get. How can it be done in
Access and in a ADP?
Thk a lot.
 
J

Jean-Guy Marcil

Toni was telling us:
Toni nous racontait que :
Pls, I try to do the same in an ADP but i can't get. How can it be
done in Access and in a ADP?
Thk a lot.

Sorry, but I have no idea what you are writing about...

Either wait for someone who knows, or write back providing actual details
pertaining to your situation.

It is often better to start a new thread instead of latching on an existing
one when we have a different query, even though the topic may be similar.


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Toni

i already did, started a new topic, but get no answer there. for this i tried
to get some answer from here.
Just want to access to my custom properties of database, and i saw it works
for .mdb but not for .adp, unless i tried to do it but doesn't work. anyway
thk for the reply.
 
D

Doug Robbins - Word MVP

It would be more appropriate for you to be asking this question in an Access
Newsgroup - possibly microsoft.public.access.adp.sqlserver than this
newsgroup which is intended for obtaining assistance with the use of Visual
Basic for Applications with Word

--
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
 
T

Toni

thk for ur reply, i just put this question in many others newgroup but i had
no answer. anyway, i'll try to put in many others again.
thk for ur help, Doug
 

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