VBA & Bookmarks

Q

Q

hi,
i am automating a letter thru vba with bookmarks, but i
need a certain number to appear in the letter three
times. everytime i put the bookmark for that number in
the next place it removes it from the last place i put
it. is it possible that one bookmark can be used multiple
times in one document? thanks in advance

~q
 
J

Jean-Guy Marcil

Hi "Q",

Use REF field whereever you want to refer to the first bookmarked item.

See the online help and the examples for REF fields, it is pretty explicit.

For example, let's say you have text represented by the "MyText" Bookmark,
whereever you want "MyText" to be repeated, put a field (Use CTRL-F9 to add
the curly braces):
{ REF MyText }.

HTH

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

Q

I'm not sure if i'm doing it right, but i'm using {REF
Bookmark_Title} just like you said and when i run the
macro, it doesn't do anything. The online help says to
pretty much do exactly what you suggested, but it's not
working, do i have to put an incerpt in my code or
something, or should it be able to run of itself? thanks
again in advance

~q
 
J

Jezebel

Document properties and document variables are another (in my view easier)
way to deal with this task. Create a custom document property to contain
your number, then insert DocProperty fields in the document wherever you
want the number to appear.
 
L

Laura Townsend

Is is possible your ref fields aren't updating? Or that your bookmark is
getting wiped out by when you put the text at it (so there's no bookmark
left for your ref fields to see?) ALSO -- when I do this, I just use
multiple bookmarks, like "Recipient1", "Recipient2", "Recipient3" instead of
a cross reference field. That way, the person using the gets all text, and
no fields to confuse them, and I don't have to worry about updating fields.

Here are some code snippets for whatever route you take.

You can put the following line of code to update all fields in the body of
the document (this won't go into headers/footers, let me know if you need
to) after you fill in the bookmark:

activedocument.fiels.update

If you want to make sure you only update your ref fields, use something like
this:

' declare a variable of type Field
Dim fldTheText As Field

' look at each field in the doc, if it's a ref field, update it
For Each fldTheText In ActiveDocument.Fields
If fldTheText.Type = wdFieldRef Then
fldTheText.Update
End If
Next fldTheText

When you complete the bookmark, replace it in the document so it's still
there for the reference field to see.
This code puts the text at the bookmark range and then puts the bookmark
back around the text.

'declare a range variable to be the bookmark range
Dim rngBookmark As Range

'set that variable to the bookmark range
Set rngBookmark = ActiveDocument.Bookmarks("BookmarkName").Range
'put the text at the bookmark range
rngBookmark.Text = "theText"
'Puts the bookmark back in the doc for future use in case it got wiped out.
ActiveDocument.Bookmarks.Add "BookmarkName", rngBookmark








I'm not sure if i'm doing it right, but i'm using {REF
Bookmark_Title} just like you said and when i run the
macro, it doesn't do anything. The online help says to
pretty much do exactly what you suggested, but it's not
working, do i have to put an incerpt in my code or
something, or should it be able to run of itself? thanks
again in advance

~q
 
J

Jean-Guy Marcil

Hi "Q",

Can you show us the code you are sing to populate the bookmarks in your
document? That will avoid unecessary shots in the dark and guesswork...

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


"Q" <[email protected]> a écrit dans le message de [email protected]...
I'm not sure if i'm doing it right, but i'm using {REF
Bookmark_Title} just like you said and when i run the
macro, it doesn't do anything. The online help says to
pretty much do exactly what you suggested, but it's not
working, do i have to put an incerpt in my code or
something, or should it be able to run of itself? thanks
again in advance

~q
 

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