Word 2000: Formatting words within a Bookmark

V

VBA Coder

Can anyone tell me if this can be done. With VBA code, I want to add some
text to a bookmark within my document but as I am adding the text to the
bookmark, I'd like to turn on/off Bold, add Underline, add comments, etc...

I am able to do this outside of a bookmark with the Selection method, but
wanted to know if I can do this with a bookmark.

My sample code to format the text of a selection is below, which I would
like to be able to do the same thing for a bookmark:

With Selection
.TypeText Text:="This is my first paragraph"
.TypeParagraph
.TypeText Text:="This is my 2nd paragraph. Now I will "
.Font.Bold = True
.TypeText "BOLD some words. "
.Font.Bold = False
.TypeText Text:="Bold is now turned OFF. Now, add a comment
here"
.Comments.Add Selection.Range, "This is my comment"
.TypeParagraph
.TypeText Text:="This is my 3rd paragraph."
.EndKey unit:=wdLine
End With

Is this possible to do with a bookmark? My code to select the bookmark is:
ActiveDocument.Bookmarks("myBookmark").Select

If I try the same thing using my bookmark, as I did in my above example, the
text gets put after the bookmark, not within the bookmark. And, if I rerun
my code over and over, I get repeating paragraphs, because the bookmark does
not get cleared out, since my sample text is actually in the document, not
within the bookmark.
 
C

Charles Maxson

VBA Coder,

Try using the Range Property of the Bookmark (to get a Range object). The
Range object is more useful (and more predictable/better performant) than
the Selection object and TypeText....

Sub InsertBookmarkText()

If ActiveDocument.Bookmarks.Exists("Sample") Then
ActiveDocument.Bookmarks("Sample").Range.Text = "RealText"
End If

End Sub
 
V

VBA Coder

I had used the Range object and had success, however, trying to do things
such as applying Bold to a bunch of words, or underline a bunch of words,
required a lot more code than just applying the ".Font.Bold = True" to turn
Bold ON and ".Font.Bold = False" to turn Bold OFF. Plus, with using a Range
object when trying to underline or bold words within the range, you had to
somehow know the exact index of the word(s) you want to bold or underline or
apply a comment to, whereas with the selection object in my 1st example, it
was as simple as 1) Turn it ON, 2) Place your text, 3) Turn it OFF. I was
hoping there was an easier way to do this with Bookmarks.

The other thing with Range objects and trying to count words is, periods,
semi-colons, left parenthesis, right parenthesis, etc..., are considered a
single word, so trying to count words to determine the proper index to apply
BOLD, UNDERLINE, COMMENTS, etc.., became quite a task, especially when my
text string that I am placing into my bookmark, would be dynamic (can change
each time my function executes).
 
H

Helmut Weber

Hi,
You are quite right, insofar as using ranges requires
more coding then adapting recorded actions. It pays off
in the long run, and if you have to handle lots of docs
and in addition have large documents. I'm using selection
quite often for simple tasks and switch to range in
case it is worth the effort.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, W98
 

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