Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Oct 2007
    Location
    Wirral, UK
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nullify Selection object in VBA (VBA/Word/2003)

    I have a large collection of documents which require certain fixed keywords to be set to Bold (always Plain to start with). I have written a macro which loads the keywords (hard coded) into a collection, then loops through the collection doing a Find/Select on each one. It then uses 'Selection.Boldrun' to set Bold. Here is the macro:
    Dim Keywords As New Collection
    Dim Keyword As String
    Dim index2 As Integer

    Set Keywords = Nothing
    'load keywords into the collection object
    Keywords.Add Item:="cap type:"
    Keywords.Add Item:="edibility:"
    Keywords.Add Item:="fungus colour:"
    Keywords.Add Item:="habitat:"
    Keywords.Add Item:="location:"
    Keywords.Add Item:="normal size:"
    Keywords.Add Item:="spore colour:"
    Keywords.Add Item:="stem type:"
    index2 = 1
    'loop through the keywords in the collection
    For Each x In Keywords
    Keyword = Keywords.Item(index2)
    'find and select the keyword...
    With Selection.Find
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute FindText:=Keyword
    End With
    '...and set its format to Bold
    Selection.BoldRun
    index2 = index2 + 1
    Next

    It works fine until I come to a document which is missing one of the keywords. In this case I expected the Selection object to be empty. In fact it remembers the previous selection and applies the Boldrun method to it a second time, undoing its previous work and setting the format back to Plain. I think what I need to do is either empty the Selection object before each Find, or make the Boldrun conditional on a successful Find; but I can't find a way to do either. Any ideas anyone?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Nullify Selection object in VBA (VBA/Word/2003)

    A belated welcome to Woody's Lounge!

    The Execute method of the Find object returns True if the search text was found, False otherwise. So you can replace the line

    .Execute FindText:=Keyword

    with

    If .Execute(FindText:=Keyword) Then
    Selection.BoldRun
    End If

    By the way, your code will only make one instance of each keyword bold. If you want all instances to become bold, you have to loop through the document. You could do it like this:

    Set Keywords = Nothing
    ' Load keywords into the collection object
    Keywords.Add Item:="cap type:"
    Keywords.Add Item:="edibility:"
    Keywords.Add Item:="fungus colour:"
    Keywords.Add Item:="habitat:"
    Keywords.Add Item:="location:"
    Keywords.Add Item:="normal size:"
    Keywords.Add Item:="spore colour:"
    Keywords.Add Item:="stem type:"
    ' Set up Find
    With Selection.Find
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    .Wrap = wdFindStop
    ' Loop through the keywords in the collection
    For Each x In Keywords
    Selection.HomeKey Unit:=wdStory
    .Text = x
    ' Loop through document
    Do While .Execute
    ' Set format to Bold
    Selection.BoldRun
    Loop
    Next x
    End With

  3. #3
    Lounger
    Join Date
    Oct 2007
    Location
    Wirral, UK
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Nullify Selection object in VBA (VBA/Word/2003)

    Thanks HansV, I've just tried it out and it's a good un!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •