Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Testing whether a custom document property aleady exists

1,816 views
Skip to first unread message

Paul H Griffiths

unread,
Sep 10, 2001, 8:23:42 AM9/10/01
to
I'm really embarassed about asking such a basic question, but it does
say "beginners", so...

What I want to do is (in pseudo-code):

IF I haven't done this already
THEN add a whole bunch of custom document properties
END IF

Adding the properties is easy, but what's the conditional?

TIA

Mark Tangard

unread,
Sep 10, 2001, 9:29:11 AM9/10/01
to

Paul,

If this is a "basic" question for you, you ain't no beginner!

If I understand you correctly you want to check to see if the
custom document properties you want to set are already set.
If so, you just have to loop through the document properties
collection, and see if any of them match (one of?) the ones
you want added:

Dim p As DocumentProperty, YeahItsThere As Boolean
For Each p In ActiveDocument.CustomDocumentProperties
If p.Name = "foo" Then
YeahItsThere = True
Exit For
End If
Next p
If Not YeahItsThere Then
ActiveDocument.CustomDocumentProperties.Add Name:="foo", _
LinkToContent:=False, Type:=msoPropertyTypeString, _
Value:="one stringy-dingy..."
End If

-- Mark Tangard <mtan...@speakeasy.net>, MS Word MVP -------------
-- See the MVP FAQ at http://www.mvps.org/word --------------------
-- http://www.speakeasy.org/~mtangard -----------------------------
----------- "Life is nothing if you're not obsessed." --John Waters
-------------------------------------------------------------------
Please reply ONLY to the newsgroup.

Paul H Griffiths

unread,
Sep 10, 2001, 10:10:05 AM9/10/01
to
Thanks for this Mark

I had been trying something like:

If ActiveDocument.CustomDocumentProperties.Count = 0
Then ...

but I couldn't get it to work. (Why, I wonder?)

>.
>

Perry

unread,
Sep 10, 2001, 12:53:07 PM9/10/01
to
Hi Paul,

No need for loops.
This is an example of a function to check whether a named item of
a collection exists.
Use it with any kind of (Word) object collection

Function DocPropExists(ByVal DocPropName As String) As Boolean
On Error Goto NotExist
'test on it's own name, if not there an error occurs
DocPropExists = (DocPropName = _
ActiveDocument.CustomDocumentProperties(DocPropName).Name)

Exit Function
NotExist:
'function remains false
Err.Clear
End Function

How to utilize the function?
Debug.Print DocPropExists("NameOfMyDocProp")

Kind regards,
Perry

"Paul H Griffiths" <paulhgr...@hotmail.com> schreef in bericht
news:b459374b.01091...@posting.google.com...

0 new messages