Re: Highlighting booksmarks

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Joe (anonymous_at_discussions.microsoft.com)
Date: 04/08/04


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
     
     
     
     



Relevant Pages

  • Re: C++ vs. C#
    ... To make this viable, you have to be able to bookmark, highlight, annotate, and search. ... MSDN magazine as opposed to being on my under 3lb X60 Lenovo laptop. ... dont like Kindle mainly because its only for reading. ...
    (microsoft.public.vc.mfc)
  • Re: How do I Manage a manual using Word?
    ... scroll as the user scrolls with a link back to the TOC, ... While still highlighted I also gave it a refcode called "Table" as a bookmark ... I highlight the new tableicon and hyperlink it to the "table" ... "Are you talking about an external object to be repeated here (picture, ...
    (microsoft.public.word.pagelayout)
  • Re: Highlighting booksmarks
    ... || highlight the text on the line where the bookmark is. ... work in regards to your expectations. ... Dim myTest As Range ... you may want to compare with this code where the GoTo method is not ...
    (microsoft.public.word.vba.general)
  • Re: How do I Manage a manual using Word?
    ... I'm making a manual for the next intern and any other future interns during ... While still highlighted I also gave it a refcode called "Table" as a bookmark ... I highlight the new tableicon and hyperlink it to the "table" ... "Are you talking about an external object to be repeated here (picture, ...
    (microsoft.public.word.pagelayout)
  • Re: How do I Manage a manual using Word?
    ... scroll as the user scrolls with a link back to the TOC, ... While still highlighted I also gave it a refcode called "Table" as a bookmark ... I highlight the new tableicon and hyperlink it to the "table" ... "Are you talking about an external object to be repeated here (picture, ...
    (microsoft.public.word.pagelayout)