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:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* 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
Go Back   Tech Support Forum > Microsoft Support > Microsoft Office support
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Microsoft Office support MS Office support forum

Reply
 
LinkBack Thread Tools
Old 09-28-2009, 06:07 AM   #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
KJJones is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
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

Old 09-28-2009, 06:27 PM   #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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-29-2009, 11:52 AM   #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
KJJones is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-29-2009, 01:33 PM   #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.
Attached Files
File Type: zip checklist.zip (9.5 KB, 2 views)
fumei is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-01-2009, 12:09 AM   #5 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Re: Word Macro Issue

how would you modify this so that words such as "in "would not be highlighted but it appears as a phrase in your checklist?
in----checklist of "in case"
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-01-2009, 03:54 AM   #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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-01-2009, 04:02 PM   #7 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Re: Word Macro Issue

is there a criteria?
im meant like phrases?
thanks
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-01-2009, 05:47 PM   #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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-01-2009, 08:44 PM   #9 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Re: Word Macro Issue

im wondering if its possible to never highlight in without it being in the phrase?
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-01-2009, 10:41 PM   #10 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Confused Re: Word Macro Issue

well is there a way to input the minimum amount of letters?
thanks
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-02-2009, 12:56 AM   #11 (permalink)
TSF Enthusiast
 
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista


Re: Word Macro Issue

Hi swaneon,
Quote:
im wondering if its possible to never highlight in without it being in the phrase?
What phrase? If your checklist.doc has a phrase with a word in it, that word won't be highlighted unless it's part of the phrase. Have you even tried the code to find out for yourself? The code I posted takes whole words/phrases from an input list and processes the active document against that list.
Quote:
is there a way to input the minimum amount of letters?
Yes, since my code uses wildcards, it's possible to have a list entry like 'boo*' to highlight every word beginning with 'boo'. You could even tell it to exlude certain variants. To use this to it's fullest potential, you'll need to study Word's wildcard syntax.

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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-02-2009, 08:56 PM   #12 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Re: Word Macro Issue

knowing its possible is nice
ill come up with something and show is that fine?
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-02-2009, 09:59 PM   #13 (permalink)
TSF Enthusiast
 
Join Date: Apr 2008
Location: Australia
Posts: 565
OS: Vista


Re: Word Macro Issue

Hi swaneon,
Quote:
ill come up with something and show is that fine?
That's fine by me.
__________________
Cheers
macropod
(MS MVP -Word)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-02-2009, 11:36 PM   #14 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Re: Word Macro Issue

i did try your code in 07 but it also includes certain words not within a phrase---if wondering what defines a phrase?
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-03-2009, 12:01 AM   #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.
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-03-2009, 12:29 AM   #16 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Re: Word Macro Issue

i dont know if i said this before but i appreciate your help greatly
thank you
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-03-2009, 04:27 PM   #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
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-03-2009, 08:00 PM   #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
As I said, though, I haven't actually tested it - and you may or may not need the last line (wrdRef.MoveEnd wdCharacter, 1).
__________________
Cheers
macropod
(MS MVP -Word)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-04-2009, 01:54 AM   #19 (permalink)
Registered User
 
Join Date: Oct 2009
Posts: 14
OS: Win7


Re: Word Macro Issue

theres error 438 with line
wrdRef.MoveEnd wdCharacter, -1?
swaneon is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-04-2009, 02:20 AM   #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
Note: The checklist.doc will need to have each word or phrase separated by a paragraph break, for example:

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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 09:59 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85