format a bookmark

M

mark kurten

If i have a bookmark in a word doc which has the text

this is a sample sentence

How do i go about Bolding and Underlining the word sample only?

thanks.

i think i saw the font property as being part of the selection object, but
the selection object would have the whole bookmark..right?
 
M

macropod

Hi Mark,

Having found & selected the bookmarked text, simply search the selection for
the required string and either select the found text and apply the required
formatting, or replace it with the same string with the required font
attributes.

Cheers
 
M

mark kurten

that's what my intentions are, but i don't know what methods of what objects
to use..
thanks
 
P

Peter Hewett

Hi mark kurten

Try this code:

Public Sub WordInBM()
Dim rngBookmark As Word.Range
Dim lngPos As Long
Dim strFindText As String

strFindText = "sample"

Set rngBookmark = ActiveDocument.Bookmarks("BM1").Range
With rngBookmark
lngPos = InStr(1, .Text, strFindText, vbTextCompare)
If lngPos > 0 Then

' Remap to strFindText so that we can modify the actual document
.Start = .Start + lngPos - 1
.End = .Start + Len(strFindText)

' Modify strFindText's attributes in the document
.Font.Bold = True
.Font.Underline = wdUnderlineSingle
End If
End With
End Sub

This works well as long as the bookmark does not contain truely huge amounts of text. It
still works but it's *slow* loading text from the document into a string.

HTH + Cheers - Peter
 

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