Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 39

Thread: vba script find and highlight multi-word phrases

  1. #1

    vba script find and highlight multi-word phrases

    I use MS Word 2003. I'm trying to find and highlight words in Word docs (consisting of single words and milti-word phrases like "I think that") but can't figure out how to get the script to highlight multi-word phrases.

    I tried the following two script and had the following issues:

    1.
    `````````````
    Sub CompareWordList()
    Dim sCheckDoc As String
    Dim docRef As Document
    Dim docCurrent As Document
    Dim wrdRef As Object

    sCheckDoc = "c:\checklist.doc"
    Set docCurrent = Selection.Document
    Set docRef = Documents.Open(sCheckDoc)
    docCurrent.Activate

    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Font.Bold = True
    .Replacement.Text = "^&"
    .Forward = True
    .Format = True
    .MatchWholeWord = True
    .MatchCase = True
    .MatchWildcards = False
    End With

    For Each wrdRef In docRef.Words
    If Asc(Left(wrdRef, 1)) > 32 Then
    With Selection.Find
    .Wrap = wdFindContinue
    .Text = wrdRef
    .Execute Replace:=wdReplaceAll
    End With
    End If
    Next wrdRef

    docRef.Close
    docCurrent.Activate
    End Sub
    PROBLEM: It highlights each word of my multi-word items instead of the whole thing and it highlights parts of whole words. I put all my words in my word doc seperated by spaces and quotes around the multi-word entries.

    `````````````
    2.
    ```````````````
    Const wdReplaceAll = 2

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    Set objDoc = objWord.Documents.Open("C:\edit.doc")
    Set objSelection = objWord.Selection

    arrWords = Array("I think that", "very")

    For Each strWord In arrWords
    objSelection.Find.Text = strWord
    objSelection.Find.Forward = True
    objSelection.Find.MatchWholeWord = True

    objSelection.Find.Replacement.Highlight = True

    objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
    Next

    PROBLEM: It appears this script just skipped the multi-word items and didn't highlight it. It only highlighted the 2nd word.

    ``````````````

    Whether I use a seperate file with all my words or input all the words and phrases into an array, I simply need it to recognize multi-word entries as a unit and not individual words.

    Thank you for your time,
    Shane

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Try:

    [VBA]Sub ScratchMacro()
    Dim oRng As Word.Range
    Dim arrWords
    Dim i As Long
    arrWords = Array("I think that", "very")
    For i = 0 To UBound(arrWords)
    Set oRng = ActiveDocument.Range
    With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = arrWords(i)
    .MatchWholeWord = True
    .Replacement.Highlight = True
    .Execute Replace:=wdReplaceAll
    End With
    Next
    End Sub
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Worked like a charm Greg. Thank you kindly.

  4. #4

    Complete macro

    Hi,
    I am new to this forum, I need help. I am not clear with the above discussion as well as the macro. Please provide me with a single macro to find the multi-word phrases. So that i can copy and pase in macro editor. It will be more useful for me.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "Please provide me with a single macro to find the multi-word phrases. So that i can copy and pase in macro editor"

    You have already been provided with it. An example of it is the code Greg posted. Copy it into the VBE - Visual Basic Editor (macro editor). If it may help, I will walk you through the code.[vba]
    Sub ScratchMacro()
    Dim oRng As Word.Range
    Dim arrWords
    Dim i As Long
    arrWords = Array("I think that", "very")
    For i = 0 To UBound(arrWords)
    Set oRng = ActiveDocument.Range
    With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = arrWords(i)
    .MatchWholeWord = True
    .Replacement.Highlight = True
    .Execute Replace:=wdReplaceAll
    End With
    Next
    End Sub
    [/vba]
    1. declare the variables
    [vba] Dim oRng As Word.Range
    Dim arrWords
    Dim i As Long [/vba]

    2. give values to the array[vba]
    arrWords = Array("I think that", "very") [/vba]The array arrWords is now composed of two items: "I think that" and "very". Numerically, this means:

    arrWords(0) = "I think that"
    arrWords(1) = "very"

    Arrays are by default 0=-based.

    3. for each item in the array (from 0 to the UPPER BOUND, the last...so it could 1 in this case..or 101)
    [vba] For i = 0 To UBound(arrWords)
    [/vba]The first one will be "I think that".

    4. make a range object of the entire document[vba] Set oRng = ActiveDocument.Range
    [/vba]

    5. using Find, search for all instances of the array item (e.g. "I think that") and replace it with the same text, but highlighted[vba] With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = arrWords(i)
    .MatchWholeWord = True
    .Replacement.Highlight = True
    .Execute Replace:=wdReplaceAll
    End With
    [/vba]

    6. go on to the next item in the arry ("very") and repeat 1 to 5.

  6. #6
    Fine, Thanks friend for your detailed explanation.
    Can you give me a macro to higlight any repeated words in the document. Say, "the the", "industry industry" or any words.
    and another is
    "David, 1999; David, 1899". can the macro find and higlight the repeated name "David". It may be any word. but the occurence of the word is that i have given above.

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Maheshdx,

    What have you coded so far?

    Coding for repeated words like "the the", or "industry industry", is not so difficult, but coding to recognise 'David' in a string like "David, 1999; David, 1899" is more problematic and would need a clear statement of what does and does not count as a repeat in such cases.

    Plus, since this is a quite different issue to what the original thread was about, I think you really should starty a new thread for this problem.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Hi
    Thanks for your replies from 1000 km far way. Sorry, i don't know how to start new thread. Can you do that. But is it possible to get the macro for "David, 1999; David, 1983". Help...

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Maheshdx,

    I can't start the new thread - you have to do that.

    And, when you do, please provide (a) what you have coded so far to solve your problem and (b) the rules for deciding when strings that aren't simple repeats of the same word (eg "David, 1999; David, 1899") should be highlighted. For example, I don't suppose you would want 'that' highlighted in a string like "Can you do that, if that is OK?".
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    Hi
    I have started the new thread "Macro to higlight repeated words". I am poor in creating macros. But have ideas, thats why need your help. Please help.....

  11. #11
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Is it possible to get it to highlight only if it exactly matches? Say for example you want to highlight AbC but not ABC?

  12. #12
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Gavin,

    Before the '.Execute' line, insert a line with '.MatchCase = True'
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  13. #13
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Is there a way to have this saved in one doc and open it, then open a 2nd doc and run the code on it like you can in excel?

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You can execute code in any document you like if the code is in a global template.

  15. #15
    Hi
    I think only a limited number of words can be added in the array. IF there are some 500 to 1000 words what can we do? arrWords = Array("I think that", "very"...)

  16. #16
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You certainly can have over 500 words (elements) in an array. For an example with 1500 words, see the file attached to my post at:
    http://lounge.windowssecrets.com/ind...owtopic=763675
    The array in this case, is simply a very long text string.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  17. #17
    If I may take advantage of this thread....

    How do I highlight all instances of "win" (light blue, say) and "sit" (light red) excluding/omitting "twin", "swing", "site" and "visitor"?

    Hope I haven't hijacked the thread.

    Thank you.

  18. #18
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The '.MatchWholeWord = True' setting takes care of the whole words part and, if you combine that with '.Font.ColorIndex = wdPink, or whatever, the words found will only be of the specified color. To get this to work for different colours in conjunction with the word list, you might have a case statement inside the array, a separate array for each colour, or a two-dimension array, with the second dimension holding the colour values (eg wdPink= 5).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  19. #19
    A little beyond my present level. A good guideline, however. I'll take it, it's a challenge. Thank you very much, macropod.

  20. #20
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi HJ,
    When I said 'case statement inside the array', that should have been 'case statement inside the loop'.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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