Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-08-2013, 11:59 PM
norgro norgro is offline Highlight parent word Windows XP Highlight parent word Office 2007
Novice
Highlight parent word
 
Join Date: Feb 2013
Location: Perh Australia
Posts: 12
norgro is on a distinguished road
Default Highlight parent word


I am creating macros to assist with the teaching of Braille.

I need to create a macro to highlight all words in a document that contain the text "ever".

I thought ".parent" would refer to the word that contained the search text, but it did not seem to work.

Your help would be appreciated.

Norman


My code:
Sub FindEver()
Set oRng = ActiveDocument.Range
' Search document for specified text and highlight parent word.

Selection.HomeKey Unit:=wdStory


With oRng.Find
.Text = "ever"
.Wrap = wdFindStop 'stops at the end of the document
While .Execute
oRng.Characters.Parent.HighlightColorIndex = wdYellow
Wend
End With
End Sub
Reply With Quote
  #2  
Old 03-09-2013, 12:18 AM
macropod's Avatar
macropod macropod is offline Highlight parent word Windows 7 64bit Highlight parent word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi norgro,

You can't select discontiguous ranges with a macro, so your Find/Replace would have to use something like:
Code:
Sub Demo()
With ActiveDocument.Content.Find
  .ClearFormatting
  .Text = Selection.Text
  With .Replacement
    .ClearFormatting
    .Text = "^&"
    .Highlight = True
  End With
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With
End Sub
The problem with any Find/Replace approach is that it changes your document's content. However, if you select the input expression, press Ctrl-F, right-arrow, Word will temporarily highlight all occurences of the term.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-09-2013, 01:24 AM
norgro norgro is offline Highlight parent word Windows XP Highlight parent word Office 2007
Novice
Highlight parent word
 
Join Date: Feb 2013
Location: Perh Australia
Posts: 12
norgro is on a distinguished road
Default

Paul
Thank you for replying. I am not sure that I explained my problem very well.
Given text that has words containing "ever", for example: ever never clever beverage reverberate, I want to highlight every word in that text that contains "ever". So, for example, I want the whole word "never" to be highlighted, and every other whole word that contains "ever".
In creating the code, I assumed, for example, that "never" was the parent of "ever", but that apparently is not the case.
Reply With Quote
  #4  
Old 03-09-2013, 01:58 AM
macropod's Avatar
macropod macropod is offline Highlight parent word Windows 7 64bit Highlight parent word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi norgro,

The mere fact that a string can be found in various words doesn't make one the parent of the other. For what you're after, try:
Code:
Sub Demo()
With ActiveDocument.Content
  With .Find
    .ClearFormatting
    .Text = Selection.Text
    With .Replacement
      .ClearFormatting
      .Text = ""
    End With
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  While .Find.Found = True
    .Expand wdWord
    If Right(.Text, 1) = " " Then
      .End = .End - 1
    End If
    .HighlightColorIndex = wdBrightGreen
    .Collapse wdCollapseEnd
    .Find.Execute
  Wend
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-09-2013, 02:13 AM
norgro norgro is offline Highlight parent word Windows XP Highlight parent word Office 2007
Novice
Highlight parent word
 
Join Date: Feb 2013
Location: Perh Australia
Posts: 12
norgro is on a distinguished road
Default

Paul

Your suggestion worked brilliantly!!

My first attempt failed because of a typographical error on my part.

Thanks again

Norman
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlight parent word Find and highlight multiple words in MS Word document AtaLoss Word VBA 37 09-22-2021 12:04 PM
Highlight parent word how to selectively highlight text in word cnyoon2 Word 1 08-04-2015 08:16 AM
Highlight parent word Want "cloud" shape in Word, to highlight changes Arbie Word 1 03-31-2012 05:22 AM
Highlight un-identifiable wolfer Word VBA 0 08-24-2011 06:37 AM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft