+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    03-21-2009
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    57

    Find a word and highlight it

    Hi Friends,

    I am new to word macros. I have a word document in that need to find a word and highlight that word. Suppose if the word occured 10 times in 10 places the word need to be highlighted. Please give me appropriate code to create a macro.


    Thanks & Regards
    Ramesh

  2. #2
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,042

    Re: Find a word and highlight it

    How about this

    Code:
    Sub HighLight()
    '
    ' enter a search string to be highlighted throughout the document
    '
    '
    Dim strWord As String
    strWord = InputBox("Enter a word", "What do you want to find?")
        Selection.HomeKey Unit:=wdStory
        Options.DefaultHighlightColorIndex = wdYellow
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.HighLight = True
        With Selection.Find
            .Text = strWord
            .Replacement.Text = strWord
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Replacement.HighLight = True
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  3. #3
    Registered User
    Join Date
    03-21-2009
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    57

    Re: Find a word and highlight it

    Hi Teylyn,


    Thank you for your reply. It is working in word. Could you please provide the for the same purpose.


    What I want in excel is we find a word in entire workbook and need to highlight it.


    Please kindly provide the code for excel.


    Thanks
    Ramesh

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Find a word and highlight it

    What I want in excel is we find a word in entire workbook and need to highlight it.


    Please kindly provide the code for excel.
    In which case post a new question in the Excel Programming Forum.

  5. #5
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    310

    Re: Find a word and highlight it

    Code:
    Sub myHighLight_TEXT()
    
    Dim i As Long
    Dim myRange As Range
    
    Selection.WholeStory
    
    i = InStr(Selection, "Microsoft")
    
    Selection.Collapse
    
    Set myRange = ActiveDocument.Range( _
        Start:=ActiveDocument.Characters(i).Start, _
        End:=ActiveDocument.Characters(i + 9).End)
    
    myRange.HighlightColorIndex = wdYellow
    
    Set myRange = Nothing
    
    End Sub

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0