Re: Word 2000: Formatting words within a Bookmark by VBA
VBA
Thu Dec 04 08:23:44 CST 2003
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).
"Charles Maxson" <charles@officezealot.remove.com> wrote in message
news:eiX9aAjuDHA.684@TK2MSFTNGP09.phx.gbl...
> 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
>
> --
> Charles
> www.officezealot.com
>
>
> "VBA Coder" <noone@account.com.NO_SPAM.> wrote in message
> news:OSjZTAduDHA.2308@TK2MSFTNGP09.phx.gbl...
> > 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.
> >
> >
>
>