![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Microsoft Office support MS Office support forum |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
Join Date: Sep 2009
Posts: 3
OS: Vista Business SP2
|
Word Macro Issue
I am trying to use a macro to search (in MS Word 2007) a document for whole words (from a list held in a separate document) and highlight them (in green). The macro is below, but the match whole word option doesn't work, it still highlights words within other words. Does anyone know what the issue could be? Any help much appreciated.
Sub CompareWordList() Dim sCheckDoc As String Dim docRef As Document Dim docCurrent As Document Dim wrdRef As Object Options.DefaultHighlightColorIndex = wdBrightGreen sCheckDoc = "c:\checklist.doc" Set docCurrent = Selection.Document Set docRef = Documents.Open(sCheckDoc) docCurrent.Activate With Selection.Find .MatchWholeWord = True .ClearFormatting .Replacement.ClearFormatting .Replacement.Highlight = True .Replacement.Text = "^&" .Format = True .MatchCase = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = 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 |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista
|
Re: Word Macro Issue
Hi KJJones,
Try something like: Code:
Sub CompareWordList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As Object
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdBrightGreen
Set docCurrent = ActiveDocument
sCheckDoc = "c:\checklist.doc"
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate
With docCurrent.Content.Find
.ClearFormatting
With .Replacement
.ClearFormatting
.Highlight = True
.Text = "^&"
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
For Each wrdRef In docRef.Words
If Asc(Left(wrdRef, 1)) > 32 Then
.Text = "<" & Trim(wrdRef) & ">"
.Execute Replace:=wdReplaceAll
End If
Next
End With
docRef.Close
Application.ScreenUpdating = True
End Sub
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
#3 (permalink) |
|
Registered User
Join Date: Sep 2009
Posts: 3
OS: Vista Business SP2
|
Re: Word Macro Issue
Many thanks macropod, but i couldn't get that to work. It seems to run through the macro to the end fine enough, but doesn't highlight anything in the document. Any thoughts?
Cheers Karl |
|
|
|
|
|
#4 (permalink) |
|
Registered User
Join Date: Jul 2009
Posts: 24
OS: Windows XP
|
Re: Word Macro Issue
Works for me. ZIP file attached.
checklist.doc - this has three words "quick", "lazy" and "dog" Lazydoc.doc - this has a bunch of "The quick brown fox jumps over the lazy dog. " The file Lazydog.doc has "Compare Word List" on the top toolbar. Clicking that makes all instances of "quick", "lazy" and "dog" get green highlight. Oh, as the path to checklist.doc is hard coded (c:\checklist.doc), make sure you unzip the file to there. |
|
|
|
|
|
#6 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista
|
Re: Word Macro Issue
Hi swaneon,
Why would you have a phrase in the checklist with words you don't want higlighted? And what would the criteria be for deciding when a word in a phrase should or should not be highlighted?
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
#8 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista
|
Re: Word Macro Issue
Hi swaneon,
What would the criteria be for deciding when a word in a phrase gets highlighted and when it doesn't? For example, would 'in' get highlighted when it's in some phrases, but not when it's in other phrases? If so, how is the macro meant to determine when it should or should not be highlighted?
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
#11 (permalink) | ||
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista
|
Re: Word Macro Issue
Hi swaneon,
Quote:
Quote:
Another option is to have two lists: one for words/phrases to be highlighted and another for words from which the highlighting produced by the first list should be removed. For that, it's just a matter of defining the two source documents and having the required two Find?Replace loops, separated by a line to remove the highlight setting. I'll leave it to you to explore that if you're interested.
__________________
Cheers macropod (MS MVP -Word) |
||
|
|
|
|
|
#15 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista
|
Re: Word Macro Issue
Hi swaneon,
The get the code to process phrases as well as words, you would need to have a checklist.doc that has each word or phrase separated by, say, a paragraph break and modify the test in the line: For Each wrdRef In docRef.Words to handle whole paragraphs (instead of words), excluding the paragraph breaks. For example (untested): For Each wrdRef In docRef.Paragraphs wrdRef.MoveEnd wdCharacter, -1 and then after doing the replace, you may need to use: wrdRef.MoveEnd wdCharacter, 1
__________________
Cheers macropod (MS MVP -Word) Last edited by macropod; 10-03-2009 at 12:05 AM. |
|
|
|
|
|
#17 (permalink) |
|
Registered User
Join Date: Oct 2009
Posts: 14
OS: Win7
|
Re: Word Macro Issue
so this part should be like this?
For Each wrdRef In docRef.Paragraphs wrdRef.MoveEnd wdCharacter, -1 If Asc(Left(wrdRef, 1)) > 32 Then .Text = "<" & Trim(wrdRef) & ">" .Execute Replace:=wdReplaceAll wrdRef.MoveEnd wdCharacter, 1 End If |
|
|
|
|
|
#18 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista
|
Re: Word Macro Issue
Hi swaneon,
Not quite: Code:
For Each wrdRef In docRef.Paragraphs
wrdRef.MoveEnd wdCharacter, -1
If Asc(Left(wrdRef, 1)) > 32 Then
.Text = "<" & Trim(wrdRef) & ">"
.Execute Replace:=wdReplaceAll
End If
wrdRef.MoveEnd wdCharacter, 1
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
#20 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista
|
Re: Word Macro Issue
Hi swaneon,
Try: Code:
Sub ComparePhraseList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim oPara As Paragraph
Dim wrdRef As Range
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdBrightGreen
Set docCurrent = ActiveDocument
sCheckDoc = "c:\checklist.doc"
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate
With docCurrent.Content.Find
.ClearFormatting
With .Replacement
.ClearFormatting
.Highlight = True
.Text = "^&"
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
For Each oPara In docRef.Paragraphs
Set wrdRef = oPara.Range
wrdRef.MoveEnd wdCharacter, -1
.Text = "<" & Trim(wrdRef) & ">"
.Execute Replace:=wdReplaceAll
Next
End With
Set wrdRef = Nothing
docRef.Close
Application.ScreenUpdating = True
End Sub
The quick brown¶ jumped over¶ dog can be used to check the phrases 'The quick brown' and 'jumped over', and the word 'dog'.
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
![]() |
| Thread Tools | |
|
|