Checking To See If BookMark Text Exists

B

bhartman33

Hi, Everyone.

I've got a form which I'm using to populate a document at certain
bookmark points. I use optionbuttons for this task. Here is a few
lines of code:

ActiveDocument.Bookmarks("CEATitle").Range.Text = CEATitle.Value


where "CEATitle" is a textbox.

Here's my problem:

Multiple people have to use this document and contribute to it. I want
the macro to be able to detect if someone has already assigned text to
the bookmark, so that it's not duplicated each time someone opens and
contributes their part to the document.

Can anyone give me some advice on how to do this? Basically, I only
want the first person contributing to set the title. The rest of the
people should only be able to contribute their parts (but I still want
the title in the form, if possible.)

Thanks!
 
G

Greg Maxey

Your code is not assigning text to the bookmark it is just putting text
after the bookmark
The next persons text is put after the bookmard and before the last
persons and so on.

Consider a single empty bookmark named "Test" and run this code:

Sub Test()
Dim i As Long
For i = 1 To 10
ActiveDocument.Bookmarks("Test").Range.Text = i
Next i
End Sub

To do what you want, you need to define a range = to the bookmark and
then assign text to that range. This creates a problem. It wipes the
bookmark so you have to put it back. Now if the bookmarks text is not
empy (already filled in) then you want to move on. Try:

Sub Test2()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("Test").Range
If Len(oRng.Text) < 1 Then
oRng.Text = "Your text"
ActiveDocument.Bookmarks.Add "Test", oRng
End If
End Sub
 
J

Jezebel

A better solution is not to use bookmarks at all. Use document properties
and DocProperty fields. Then you can reset them as often as you like.
 
B

bhartman33

Greg said:
To do what you want, you need to define a range = to the bookmark and
then assign text to that range. This creates a problem. It wipes the
bookmark so you have to put it back. Now if the bookmarks text is not
empy (already filled in) then you want to move on. Try:

Sub Test2()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("Test").Range
If Len(oRng.Text) < 1 Then
oRng.Text = "Your text"
ActiveDocument.Bookmarks.Add "Test", oRng
End If
End Sub

I'm not sure why, but "If Len(oRng.Text) < 1" never fails, regardless
of whether or not there's text there. This isn't working for me.

I haven't tried messing with the Document Properties yet. Maybe that's
what I really need. I don't think that will work in other places,
though. I need to do this in a few places where fields don't already
exist in Word.
 
B

bhartman33

I'm not sure why, but "If Len(oRng.Text) < 1" never fails, regardless
of whether or not there's text there. This isn't working for me.

I haven't tried messing with the Document Properties yet. Maybe that's
what I really need. I don't think that will work in other places,
though. I need to do this in a few places where fields don't already
exist in Word.

Hi, Everyone.

I got it working. Here's the final code:

=============================

If ActiveDocument.Bookmarks.Exists("test") = False Then ' Checks to see
if the bookmark "test" exists
ActiveDocument.Bookmarks.Add ("test") ' If it doesn't, it adds it
ActiveDocument.Bookmarks("CEATitle").Range.Text = CEATitle.Value ' adds
the text after the CEATitle bookmark
ActiveDocument.Bookmarks("RQMN").Range.Text = RQMN.Value 'adds the text
to the RQMN bookmark
Else
End If 'if the bookmark exists, don't do anything here.

It might not be the cleanest solution, but it does work. :)
 

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