Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-29-2012, 07:49 AM
mpdsal mpdsal is offline Help with VBA code to find and highlight text Windows XP Help with VBA code to find and highlight text Office 2007
Novice
Help with VBA code to find and highlight text
 
Join Date: Nov 2011
Posts: 19
mpdsal is on a distinguished road
Default Help with VBA code to find and highlight text

I did a search before posing this but did not find a solution for my particular request.



I have the following code which is working beautifully however I want not only the found words to be highlighted but all the text in that row, which may include spaces and text. For example, after I find Procedure Step: I want it to highlight all the information following it.

Procedure Step: Inbound Staging
Procedure Step: FIFO

Here is the code I have so far:

Code:
Sub Highlight_Word()
Dim sFindText As String
'Start from the top of the document
Selection.HomeKey wdStory
sFindText = "Procedure Step:"
Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
  Selection.Range.HighlightColorIndex = wdYellow
  Selection.MoveRight
  Selection.Find.Execute
Loop
End Sub
Thanks
Mark

Last edited by macropod; 10-29-2012 at 02:40 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 10-29-2012, 02:38 PM
macropod's Avatar
macropod macropod is offline Help with VBA code to find and highlight text Windows 7 64bit Help with VBA code to find and highlight text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

By "all the text in that row" do you mean an entire paragraph, all the text in a table row (including in other cells on that row), or a single line within a paragraph?

PS: When posting code, please use code tags.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-30-2012, 08:42 AM
mpdsal mpdsal is offline Help with VBA code to find and highlight text Windows XP Help with VBA code to find and highlight text Office 2007
Novice
Help with VBA code to find and highlight text
 
Join Date: Nov 2011
Posts: 19
mpdsal is on a distinguished road
Default

It would be a sentence in this case. I can select the text I want hightlighted manually by moving the cursor to that row and pressing shift End. That selects the entire text string I would like to highlight. Problem is when I record that and insert the code in my macro when it loops through the logic again the new serach string includes all the text which is selected instead of just "Procedure Step:" as it should. Hope this helps.

Thanks
Reply With Quote
  #4  
Old 10-30-2012, 10:15 AM
mpdsal mpdsal is offline Help with VBA code to find and highlight text Windows XP Help with VBA code to find and highlight text Office 2007
Novice
Help with VBA code to find and highlight text
 
Join Date: Nov 2011
Posts: 19
mpdsal is on a distinguished road
Default

I think I got it. It may be crude but it works. I think within a week or two I can be an MPV.

Code:
Sub Highlight_WordN()
Dim sFindText As String
'Start from the top of the document
 Selection.HomeKey wdStory
 
sFindText = "Procedure Step:"
Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.Range.HighlightColorIndex = wdYellow
        Selection.MoveRight
        Selection.Find.Execute
Loop
End Sub
Reply With Quote
  #5  
Old 10-30-2012, 01:48 PM
macropod's Avatar
macropod macropod is offline Help with VBA code to find and highlight text Windows 7 64bit Help with VBA code to find and highlight text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

I think you'll find the following does what you want far more efficiently:
Code:
Sub Highlight_Words()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Content.Find
  .ClearFormatting
  .Text = "Procedure Step:[!^13]{1,}"
  With .Replacement
    .Text = "^&"
    .ClearFormatting
    .Highlight = True
  End With
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 10-31-2012, 08:38 AM
mpdsal mpdsal is offline Help with VBA code to find and highlight text Windows XP Help with VBA code to find and highlight text Office 2007
Novice
Help with VBA code to find and highlight text
 
Join Date: Nov 2011
Posts: 19
mpdsal is on a distinguished road
Default

OK, you win. That was brilliant. My code was crude in comparison. Not quite MVP yet it turns out.

Can you explain what this is doing? I've never seen this logic before. What is happening with the carat "^" and brackets?

Code:
Text = "Procedure Step:[!^13]{1,}"
Thanks!

Humbly,

M
Reply With Quote
  #7  
Old 10-31-2012, 02:03 PM
macropod's Avatar
macropod macropod is offline Help with VBA code to find and highlight text Windows 7 64bit Help with VBA code to find and highlight text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

The '[!^13]{1,}' is a wildcard expression (you'll see that I have '.MatchWildcards = True'). The [] tells Word to find any of the enclosed character(s). In this case, the '!' says it's a character other than what follows. The '^13' represents a paragraph break. The {1,} says there is at least one, such character, but we don't necessarily know what the upper limit is. So Word is being told to find 'Procedure Step:' followed by any number of chracters other than a paragraph break.

Wildcard expressions greatly enhance Word's Find/Replace capacity. See: http://word.mvps.org/FAQs/General/UsingWildcards.htm

FWIW, you don't even need the macro in this case - it's doing nothing more than you can do through the Find/Replace dialogue.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 09-11-2014, 06:29 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline Help with VBA code to find and highlight text Windows 7 32bit Help with VBA code to find and highlight text Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default

Could you expand if one was to use find and replace and have all the replaced characters/ words highlighted.
Reply With Quote
  #9  
Old 09-11-2014, 03:55 PM
macropod's Avatar
macropod macropod is offline Help with VBA code to find and highlight text Windows 7 64bit Help with VBA code to find and highlight text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

I don't understand your question - the macro already highlights the found text.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find out the duplicates and highlight them? Learner7 Excel 6 06-08-2017 06:04 AM
Help with VBA code to find and highlight text Find and highlight all words ending in -ly RBLampert Word VBA 13 10-23-2012 04:45 PM
Keyboard shortcut for 'not highlight' in 'Find'? bertietheblue Word 0 05-02-2012 06:16 AM
Highlight text and find next instance DrDOS Word 0 11-15-2010 04:02 PM
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 04:28 AM.


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