Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 03-15-2019, 05:15 AM
Asuryan33 Asuryan33 is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2017
Posts: 9
Asuryan33 is on a distinguished road
Default Highlight sentences from a list located in another file

Hello all.

I'm trying to find a way to highlight word and sentences in a file from a list located in another file.

I found a a macro online but the problem is that it will highligh all single words.
What should I do if I want to highlight a list of strings composed of several words (Like phrasal verb or expression for exemple)
For exemple, I want it to highlight all "Fill out" but leaving alone all "fill" and "out" that are not a part of "Fill out"

Thank you

the Macro I found on https://wordribbon.tips.net/ is:

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
Options.DefaultHighlightColorIndex = wdYellow

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = 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 = Trim(wrdRef)
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdRef

docRef.Close
docCurrent.Activate
End Sub
Reply With Quote
  #2  
Old 03-15-2019, 06:16 AM
gmayor's Avatar
gmayor gmayor is online now Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,671
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

You would need either to put the phrases in a table or in separate paragraphs then instead of searching for words you would search for the contents of the table cells or the paragraphs e.g.


Code:
For i = 1 To docRef.Paragraphs.Count
    Set oPara = docRef.Paragraphs(1).Range
    oPara.End = oPara.End - 1
    strFind = oPara.Text
    'do stuff with opara.text
Next i
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-15-2019, 06:25 AM
Asuryan33 Asuryan33 is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2017
Posts: 9
Asuryan33 is on a distinguished road
Default

Hello.

Yes, I was thinking that it would be better to use an Excel file for my list.

I'm very sorry. I know nothing about VBA.
What should I modify on the code ?
Where to put:



Code:
For i = 1 To docRef.Paragraphs.Count
    Set oPara = docRef.Paragraphs(1).Range
    oPara.End = oPara.End - 1
    strFind = oPara.Text
    'do stuff with opara.text
Next i
??


thx you
Reply With Quote
  #4  
Old 03-15-2019, 10:07 PM
gmayor's Avatar
gmayor gmayor is online now Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,671
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

You replace the original loop with this one e.g. as follows
checkphrases.docx sholuld have each word or phrase to find in a new paragraph (with no empty paragraphs).



Code:
Sub ComparePhraseList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim i As Integer
Dim oPara As Range


sCheckDoc = "c:\path\checkphrases.docx"  'Change to the path where the document is located.
   Set docCurrent = Selection.Document
    Set docRef = Documents.Open(sCheckDoc)
    docCurrent.Activate
    Options.DefaultHighlightColorIndex = wdYellow

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

    For i = 1 To docRef.Paragraphs.Count
        Set oPara = docRef.Paragraphs(i).Range
        oPara.End = oPara.End - 1
        With Selection.Find
            .Wrap = wdFindContinue
            .Text = oPara.Text
            .Execute Replace:=wdReplaceAll
        End With
    Next i

    docRef.Close
    docCurrent.Activate
    Set docRef = Nothing
    Set docCurrent = Nothing
    Set oPara = Nothing
   
End Sub
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 03-16-2019, 10:42 AM
Asuryan33 Asuryan33 is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2017
Posts: 9
Asuryan33 is on a distinguished road
Default

Hello.

Thank you for your answer.
After I wrote the last comment I realised that my question was stupid :P

I just tried it and it's working very well. I just changed the case sensitive for "False".

Thx a lot for your help
Reply With Quote
  #6  
Old 03-18-2019, 05:09 AM
Asuryan33 Asuryan33 is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2017
Posts: 9
Asuryan33 is on a distinguished road
Default

Hello.

I have a last question.

I see the macro and ... what is beween the first With Selection.Find // End With.. could be moved to the code you added (I tried and it works same)

Also, if I track modification on word, I see that the macro highlight the word first, then erase it and replace it with non highlight word.
If the change tracking is not activated, I see highlited word as final result.

It's not a big deal, since a can desactivate the tracking before the macro. But I'm curious to know why it's like that.

Code:
   With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Highlight = True
        .Replacement.Text = "^&"
        .Forward = True
        .Format = True
        .MatchWholeWord = True
        .MatchCase = True
        .MatchWildcards = False
    End With

    For i = 1 To docRef.Paragraphs.Count
        Set oPara = docRef.Paragraphs(i).Range
        oPara.End = oPara.End - 1
        With Selection.Find
            .Wrap = wdFindContinue
            .Text = oPara.Text
            .Execute Replace:=wdReplaceAll
        End With
    Next i

Last edited by Asuryan33; 03-18-2019 at 10:28 AM. Reason: More information
Reply With Quote
Reply

Tags
highlight, vba, word 2016

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlight words from a list Nanaia Word VBA 3 09-07-2018 02:13 PM
how to highlight all "indexed" sentences using find and replace? smallxyz Word 2 02-06-2016 02:54 AM
Macro to highlight repeated words in word file and extract into excel file aabri Word VBA 1 06-14-2015 07:20 AM
Error: The file may be corrupted, located on a server that is not responding, or read naeemeh Excel 2 11-20-2011 03:38 AM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM


All times are GMT -7. The time now is 01:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft