Custom Document Properties

D

Debra Farnham

Hello again

I'm still using Word 2K on Win2K

How can I check to see if a custom document property with the name Test1
already exists and if so, create a new one named Test2?

In otherwords:

If customdocumentproperty named "Test1" exists Then


With ActiveDocument.CustomDocumentProperties
.Add Name:="Test2", LinkToContent:=False, Value:="Whatever", _
Type:=msoPropertyTypeString

End With
End If

The next iteration would have to check for Test2 and create a new
customdocumentproperty named Test3 .... etc etc. I'm quite certain I need a
loop but I'm a lost cause when it comes to devising loops.

I could possibly end up with several custom document properties named Test
and a number. I would just like the number to increment by one based on the
highest number already in use.

I sure hope this makes sense to someone.

Thanks in advance for any help.

Debra
 
G

Greg Maxey

Debra,

Maybe something like:

Sub ScratchMacro()
Dim oDocProp As DocumentProperty
Dim i As Long
i = 1
Dim oPropName As String
Dim oPropValue As String

oPropValue = "whatever"

For Each oDocProp In ActiveDocument.CustomDocumentProperties
If Left(oDocProp.Name, 4) = "Test" Then
i = i + 1
End If
Next
oPropName = "Test" & i
ActiveDocument.CustomDocumentProperties.Add _
Name:=oPropName, LinkToContent:=False, Value:=oPropValue, _
Type:=msoPropertyTypeString
End Sub
 
D

Debra Farnham

To my rescue once again I see Greg!

(I actually managed to get part of the way there on my own - there's hope I
think)

Thanks tons! I owe you bigtime! It works like a charm.

Deb
 
G

Greg Maxey

Debra,

Actually it doesn't do quite what you asked which was to check to sed "if" a
Test1 exist and "if" it does create a Test2. I cobbled together the
following that requires a Test1 to exist prior to doing anything else.

Sub ScratchMacro()
Dim oDocProp As DocumentProperty
Dim i As Long
i = 1
Dim oPropName As String
Dim oPropValue As String
Dim bExist As Boolean

oPropValue = "whatever"
bExist = False
For Each oDocProp In ActiveDocument.CustomDocumentProperties
If oDocProp.Name = "Test1" Then bExist = True
Exit For
Next
If bExist Then
For Each oDocProp In ActiveDocument.CustomDocumentProperties
If Left(oDocProp.Name, 4) = "Test" Then
i = i + 1
End If
Next
oPropName = "Test" & i
ActiveDocument.CustomDocumentProperties.Add _
Name:=oPropName, LinkToContent:=False, Value:=oPropValue, _
Type:=msoPropertyTypeString
End If
End Sub
 
D

Debra Farnham

I completely overlooked the IF part when I noticed that the property had
been added. OOPS.

Thanks again Greg!

Your time and effort are HUGELY appreciated!

Deb
 

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