Re: Highlighting booksmarks
From: Joe (anonymous_at_discussions.microsoft.com)
Date: 04/08/04
- Next message: Ed: "Can't find proper object(?)/methods(?) for Format Picture?"
- Previous message: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- In reply to: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- Next in thread: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- Reply: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- Reply: Dave Lett: "Re: Highlighting booksmarks"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 8 Apr 2004 09:41:02 -0700
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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
- Next message: Ed: "Can't find proper object(?)/methods(?) for Format Picture?"
- Previous message: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- In reply to: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- Next in thread: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- Reply: Jean-Guy Marcil: "Re: Highlighting booksmarks"
- Reply: Dave Lett: "Re: Highlighting booksmarks"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|