Highlighting booksmarks

J

Joe

I am using Word 2002 and have created 5 bookmarks in my document to be used for formatting. I would like to step through each bookmark and, if the name includes "Topic", I would like to highlight the text on the line where the bookmark is. Here is my code. Can anyone tell me why it doesn't work

Dim b As Bookmar
For Each b In .ActiveDocument.Bookmark
If InStr(LCase(b.Name), "topic") The
.ActiveDocument.GoTo What:=wdGoToBookmark, Name:=b.Nam
With .Selectio
.EndKey Unit:=wdLine, Extend:=wdExten
.Font.Shading.BackgroundPatternColor = wdColorGree
.Font.Color = wdColorWhit
End Wit
End I
Next

TIA

Joe
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, notre ami < Joe > nous laissait savoir que :
In this message, our friend < Joe > informed us that:


|| I am using Word 2002 and have created 5 bookmarks in my document to be
used for formatting. I
|| would like to step through each bookmark and, if the name includes
"Topic", I would like to
|| highlight the text on the line where the bookmark is. Here is my code.
Can anyone tell me why
|| it doesn't work?

"it doesn't work" is a bit vague! Next time, try telling us how it does not
work in regards to your expectations. I am writing this because too often 1)
people complain that it does not work when the code is perfectly all right,
but they have unrealistic expectations, or 2) they may have realistic
expectations but are using the wrong code, or 3) the right code with
errors... see what I mean? I do not know in which category you are, I will
assume that you have appropriate expectations and are using the right code
with some errors...

I see three potential problems:
1)
|| For Each b In .ActiveDocument.Bookmarks
Why the "." before ActiveDocument... is it because this is a snippet from a
longer bit of code and you declare an application object somewhere else? If
so, then never mind this comment.
2) If you want to highlight the bookmark, why do you select the line from
the bookmark to the end of the line? It seems that the line

|| .EndKey Unit:=wdLine, Extend:=wdExtend

is not necessary.

3)
|| .ActiveDocument.GoTo What:=wdGoToBookmark, Name:=b.Name
|| With .Selection

The GoTo method when used with ActiveDocument sets a range object, but does
not actively select it.
If you insist on using GoTo, try this instead:

'_______________________________________
Dim b As Bookmark
Dim myTest As Range

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
Set myTest = ActiveDocument.GoTo(What:=wdGoToBookmark, Name:=b.Name)
myTest.Select
With Selection
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
End With
End If
Next b
'_______________________________________

or

If you use GoTo with Selection, then it gets selected:

'_______________________________________
Dim b As Bookmark

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
Selection.GoTo What:=wdGoToBookmark, Name:=b.Name

With Selection
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
End With
End If
Next b
'_______________________________________

Finally, you may want to compare with this code where the GoTo method is not
necessary:

'_______________________________________
Dim b As Bookmark

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
ActiveDocument.Bookmarks(b.Name).Select
With Selection
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
.Collapse
End With
End If
Next b
'_______________________________________


||
|| Dim b As Bookmark
|| For Each b In .ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| .ActiveDocument.GoTo What:=wdGoToBookmark, Name:=b.Name
|| With .Selection
|| .EndKey Unit:=wdLine, Extend:=wdExtend
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| End With
|| End If
|| Next b
||
|| TIA,
||
|| Joe

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

Joe

Bonjour Jean-Guy,

I tried the code without the GoTo's (below) and none of the bookmarks highlighted. This is what I meant when I said that it didn't work. I tried it with and without the Collapse method. Neither highlighted the text at the bookmarks.

Any ideas?

Joe

Code that I tried:

Dim b As Bookmark

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
ActiveDocument.Bookmarks(b.Name).Select
With Selection
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
.Collapse
End With
End If
Next b

----- Jean-Guy Marcil wrote: -----

Bonjour,

Dans son message, notre ami < Joe > nous laissait savoir que :
In this message, our friend < Joe > informed us that:


|| I am using Word 2002 and have created 5 bookmarks in my document to be
used for formatting. I
|| would like to step through each bookmark and, if the name includes
"Topic", I would like to
|| highlight the text on the line where the bookmark is. Here is my code.
Can anyone tell me why
|| it doesn't work?

"it doesn't work" is a bit vague! Next time, try telling us how it does not
work in regards to your expectations. I am writing this because too often 1)
people complain that it does not work when the code is perfectly all right,
but they have unrealistic expectations, or 2) they may have realistic
expectations but are using the wrong code, or 3) the right code with
errors... see what I mean? I do not know in which category you are, I will
assume that you have appropriate expectations and are using the right code
with some errors...

I see three potential problems:
1)
|| For Each b In .ActiveDocument.Bookmarks
Why the "." before ActiveDocument... is it because this is a snippet from a
longer bit of code and you declare an application object somewhere else? If
so, then never mind this comment.
2) If you want to highlight the bookmark, why do you select the line from
the bookmark to the end of the line? It seems that the line

|| .EndKey Unit:=wdLine, Extend:=wdExtend

is not necessary.

3)
|| .ActiveDocument.GoTo What:=wdGoToBookmark, Name:=b.Name
|| With .Selection

The GoTo method when used with ActiveDocument sets a range object, but does
not actively select it.
If you insist on using GoTo, try this instead:

'_______________________________________
Dim b As Bookmark
Dim myTest As Range

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
Set myTest = ActiveDocument.GoTo(What:=wdGoToBookmark, Name:=b.Name)
myTest.Select
With Selection
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
End With
End If
Next b
'_______________________________________

or

If you use GoTo with Selection, then it gets selected:

'_______________________________________
Dim b As Bookmark

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
Selection.GoTo What:=wdGoToBookmark, Name:=b.Name

With Selection
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
End With
End If
Next b
'_______________________________________

Finally, you may want to compare with this code where the GoTo method is not
necessary:

'_______________________________________
Dim b As Bookmark

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
ActiveDocument.Bookmarks(b.Name).Select
With Selection
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
.Collapse
End With
End If
Next b
'_______________________________________


||
|| Dim b As Bookmark
|| For Each b In .ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| .ActiveDocument.GoTo What:=wdGoToBookmark, Name:=b.Name
|| With .Selection
|| .EndKey Unit:=wdLine, Extend:=wdExtend
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| End With
|| End If
|| Next b
||
|| TIA,
||
|| Joe

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

Jean-Guy Marcil

Bonjour,

Dans son message, notre ami < Joe > nous laissait savoir que :
In this message, our friend < Joe > informed us that:


|| Bonjour Jean-Guy,
||
|| I tried the code without the GoTo's (below) and none of the bookmarks
highlighted. This is what
|| I meant when I said that it didn't work. I tried it with and without the
Collapse method.
|| Neither highlighted the text at the bookmarks.
||

Just to clarify something, are you looking for 1) bookmarks that have the
string "topic" in the bookmark name, or 2) bookmarks that have the string
"topic" in the bookmark text value?

If 1), are you sure that the bookamrk is a range of text and not only an
insertion point ( I mean, if you choose to display the bookmarks from Tools
Options > View, are the bookmarks represented like 1) I"Your text", or 2)
["Your text"]?).

|| Any ideas?
||
|| Joe
||
|| Code that I tried:
||
|| Dim b As Bookmark
||
|| For Each b In ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| ActiveDocument.Bookmarks(b.Name).Select
|| With Selection
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| .Collapse
|| End With
|| End If
|| Next b
||
|| ----- Jean-Guy Marcil wrote: -----
||
|| Bonjour,
||
|| Dans son message, notre ami < Joe > nous laissait savoir que :
|| In this message, our friend < Joe > informed us that:
||
||
|| || I am using Word 2002 and have created 5 bookmarks in my document
to be
|| used for formatting. I
|| || would like to step through each bookmark and, if the name
includes
|| "Topic", I would like to
|| || highlight the text on the line where the bookmark is. Here is my
code.
|| Can anyone tell me why
|| || it doesn't work?
||
|| "it doesn't work" is a bit vague! Next time, try telling us how it
does not
|| work in regards to your expectations. I am writing this because too
often 1)
|| people complain that it does not work when the code is perfectly all
right,
|| but they have unrealistic expectations, or 2) they may have
realistic
|| expectations but are using the wrong code, or 3) the right code with
|| errors... see what I mean? I do not know in which category you are,
I will
|| assume that you have appropriate expectations and are using the
right code
|| with some errors...
||
|| I see three potential problems:
|| 1)
|| || For Each b In .ActiveDocument.Bookmarks
|| Why the "." before ActiveDocument... is it because this is a snippet
from a
|| longer bit of code and you declare an application object somewhere
else? If
|| so, then never mind this comment.
|| 2) If you want to highlight the bookmark, why do you select the line
from
|| the bookmark to the end of the line? It seems that the line
||
|| || .EndKey Unit:=wdLine, Extend:=wdExtend
||
|| is not necessary.
||
|| 3)
|| || .ActiveDocument.GoTo What:=wdGoToBookmark, Name:=b.Name
|| || With .Selection
||
|| The GoTo method when used with ActiveDocument sets a range object,
but does
|| not actively select it.
|| If you insist on using GoTo, try this instead:
||
|| '_______________________________________
|| Dim b As Bookmark
|| Dim myTest As Range
||
|| For Each b In ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| Set myTest = ActiveDocument.GoTo(What:=wdGoToBookmark,
Name:=b.Name)
|| myTest.Select
|| With Selection
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| End With
|| End If
|| Next b
|| '_______________________________________
||
|| or
||
|| If you use GoTo with Selection, then it gets selected:
||
|| '_______________________________________
|| Dim b As Bookmark
||
|| For Each b In ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| Selection.GoTo What:=wdGoToBookmark, Name:=b.Name
||
|| With Selection
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| End With
|| End If
|| Next b
|| '_______________________________________
||
|| Finally, you may want to compare with this code where the GoTo
method is not
|| necessary:
||
|| '_______________________________________
|| Dim b As Bookmark
||
|| For Each b In ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| ActiveDocument.Bookmarks(b.Name).Select
|| With Selection
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| .Collapse
|| End With
|| End If
|| Next b
|| '_______________________________________
||
||
|| ||
|| || Dim b As Bookmark
|| || For Each b In .ActiveDocument.Bookmarks
|| || If InStr(LCase(b.Name), "topic") Then
|| || .ActiveDocument.GoTo What:=wdGoToBookmark,
Name:=b.Name
|| || With .Selection
|| || .EndKey Unit:=wdLine, Extend:=wdExtend
|| || .Font.Shading.BackgroundPatternColor =
wdColorGreen
|| || .Font.Color = wdColorWhite
|| || End With
|| || End If
|| || Next b
|| ||
|| || TIA,
|| ||
|| || Joe
||
|| --
|| Salut!
|| _______________________________________
|| Jean-Guy Marcil - Word MVP
|| (e-mail address removed)
|| Word MVP site: http://www.word.mvps.org

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

Dave Lett

Hi Joe,

Try the following:

Dim b As Bookmark

For Each b In ActiveDocument.Bookmarks
If InStr(LCase(b.Name), "topic") Then
ActiveDocument.Bookmarks(b.Name).Range.Select
With Selection
.EndKey unit:=wdLine, Extend:=wdExtend
.Range.HighlightColorIndex = wdGreen
''' the next line doesn't really apply Highlighting, it applies
shading
''' it looks very similar, true (you asked for highlight so I
thought I'd
''' point this out for what it's worth)
''' .Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
End With
End If
Next b
Joe said:
Bonjour Jean-Guy,

I tried the code without the GoTo's (below) and none of the bookmarks
highlighted. This is what I meant when I said that it didn't work. I tried
it with and without the Collapse method. Neither highlighted the text at
the bookmarks.
 
J

Joe

String "topic" is in the bookmark name

They are insertion points; I always thought that they were insertion points and not text. Apparently I am mistaken

I add a bookmark with VBA, how do I indicate that I want it to refer to the entire text

Merci

Jo

----- Jean-Guy Marcil wrote: ----

Bonjour

Dans son message, notre ami < Joe > nous laissait savoir que
In this message, our friend < Joe > informed us that


|| Bonjour Jean-Guy
|
|| I tried the code without the GoTo's (below) and none of the bookmark
highlighted. This is wha
|| I meant when I said that it didn't work. I tried it with and without th
Collapse method
|| Neither highlighted the text at the bookmarks
|

Just to clarify something, are you looking for 1) bookmarks that have th
string "topic" in the bookmark name, or 2) bookmarks that have the strin
"topic" in the bookmark text value

If 1), are you sure that the bookamrk is a range of text and not only a
insertion point ( I mean, if you choose to display the bookmarks from Tool
Options > View, are the bookmarks represented like 1) I"Your text", or 2
["Your text"]?)

|| Any ideas
|
|| Jo
|
|| Code that I tried
|
|| Dim b As Bookmar
|
|| For Each b In ActiveDocument.Bookmark
|| If InStr(LCase(b.Name), "topic") The
|| ActiveDocument.Bookmarks(b.Name).Selec
|| With Selectio
|| .Font.Shading.BackgroundPatternColor = wdColorGree
|| .Font.Color = wdColorWhit
|| .Collaps
|| End Wit
|| End I
|| Next
|
|| ----- Jean-Guy Marcil wrote: ----
|
|| Bonjour
|
|| Dans son message, notre ami < Joe > nous laissait savoir que
|| In this message, our friend < Joe > informed us that
|
|
|| || I am using Word 2002 and have created 5 bookmarks in my documen
to b
|| used for formatting.
|| || would like to step through each bookmark and, if the nam
include
|| "Topic", I would like t
|| || highlight the text on the line where the bookmark is. Here is m
code
|| Can anyone tell me wh
|| || it doesn't work
|
|| "it doesn't work" is a bit vague! Next time, try telling us how i
does no
|| work in regards to your expectations. I am writing this because to
often 1
|| people complain that it does not work when the code is perfectly al
right
|| but they have unrealistic expectations, or 2) they may hav
realisti
|| expectations but are using the wrong code, or 3) the right code wit
|| errors... see what I mean? I do not know in which category you are
I wil
|| assume that you have appropriate expectations and are using th
right cod
|| with some errors..
|
|| I see three potential problems
|| 1
|| || For Each b In .ActiveDocument.Bookmark
|| Why the "." before ActiveDocument... is it because this is a snippe
from
|| longer bit of code and you declare an application object somewher
else? I
|| so, then never mind this comment
|| 2) If you want to highlight the bookmark, why do you select the lin
fro
|| the bookmark to the end of the line? It seems that the lin
|
|| || .EndKey Unit:=wdLine, Extend:=wdExten
|
|| is not necessary
|
|| 3
|| || .ActiveDocument.GoTo What:=wdGoToBookmark, Name:=b.Nam
|| || With .Selectio
|
|| The GoTo method when used with ActiveDocument sets a range object
but doe
|| not actively select it
|| If you insist on using GoTo, try this instead
||
|| '_______________________________________
|| Dim b As Bookmark
|| Dim myTest As Range
||
|| For Each b In ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| Set myTest = ActiveDocument.GoTo(What:=wdGoToBookmark,
Name:=b.Name)
|| myTest.Select
|| With Selection
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| End With
|| End If
|| Next b
|| '_______________________________________
||
|| or
||
|| If you use GoTo with Selection, then it gets selected:
||
|| '_______________________________________
|| Dim b As Bookmark
||
|| For Each b In ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| Selection.GoTo What:=wdGoToBookmark, Name:=b.Name
||
|| With Selection
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| End With
|| End If
|| Next b
|| '_______________________________________
||
|| Finally, you may want to compare with this code where the GoTo
method is not
|| necessary:
||
|| '_______________________________________
|| Dim b As Bookmark
||
|| For Each b In ActiveDocument.Bookmarks
|| If InStr(LCase(b.Name), "topic") Then
|| ActiveDocument.Bookmarks(b.Name).Select
|| With Selection
|| .Font.Shading.BackgroundPatternColor = wdColorGreen
|| .Font.Color = wdColorWhite
|| .Collapse
|| End With
|| End If
|| Next b
|| '_______________________________________
||
||
|| ||
|| || Dim b As Bookmark
|| || For Each b In .ActiveDocument.Bookmarks
|| || If InStr(LCase(b.Name), "topic") Then
|| || .ActiveDocument.GoTo What:=wdGoToBookmark,
Name:=b.Name
|| || With .Selection
|| || .EndKey Unit:=wdLine, Extend:=wdExtend
|| || .Font.Shading.BackgroundPatternColor =
wdColorGreen
|| || .Font.Color = wdColorWhite
|| || End With
|| || End If
|| || Next b
|| ||
|| || TIA,
|| ||
|| || Joe
||
|| --
|| Salut!
|| _______________________________________
|| Jean-Guy Marcil - Word MVP
|| (e-mail address removed)
|| Word MVP site: http://www.word.mvps.org

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

Dave Lett

Hi Joe,

According to the VBA help file
BackgroundPatternColor: Returns or sets the 24-bit color that's applied to
the background of the Shading object
HighlightColorIndex: Returns or sets the highlight color for the specified
range.

Because they give a similar appearance, the difference becomes important
only in that one might be more difficult to recognize. For example, if
you're a user who abstains from using the highlight tool (even though it's
now a convenient toolbar button) in favor of the following:
On Shading on the Format menu, click Borders and Shading, activate the
Shading tab, set the color, and set the preferred selection from the Apply
to list.
IMHO, the shading is a little more difficult (I use it rarely, and it take
ME a longer time to do/undo than the highlight tool).

HTH,
Dave
 
J

Joe

Dave

Thanks

Jo

----- Dave Lett wrote: ----

Hi Joe

According to the VBA help fil
BackgroundPatternColor: Returns or sets the 24-bit color that's applied t
the background of the Shading objec
HighlightColorIndex: Returns or sets the highlight color for the specifie
range

Because they give a similar appearance, the difference becomes importan
only in that one might be more difficult to recognize. For example, i
you're a user who abstains from using the highlight tool (even though it'
now a convenient toolbar button) in favor of the following
On Shading on the Format menu, click Borders and Shading, activate th
Shading tab, set the color, and set the preferred selection from the Appl
to list
IMHO, the shading is a little more difficult (I use it rarely, and it tak
ME a longer time to do/undo than the highlight tool)

HTH
Dav
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, notre ami < Joe > nous laissait savoir que :
In this message, our friend < Joe > informed us that:


|| String "topic" is in the bookmark name.
||
|| They are insertion points; I always thought that they were insertion
points and not text.
|| Apparently I am mistaken.
||
|| I add a bookmark with VBA, how do I indicate that I want it to refer to
the entire text?
||

Something like:

Dim BkmRange As Range

Set BkmRange = Selection.Range

ActiveDocument.Bookmarks.Add Name:= "MyBookMark", Range:= BkmRange

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

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