#1
|
|||
|
|||
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 Mark Last edited by macropod; 10-29-2012 at 02:40 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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 |
#5
|
||||
|
||||
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] |
#6
|
|||
|
|||
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,}" Humbly, M |
#7
|
||||
|
||||
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] |
#8
|
|||
|
|||
Could you expand if one was to use find and replace and have all the replaced characters/ words highlighted.
|
#9
|
||||
|
||||
I don't understand your question - the macro already highlights the found text.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
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 |
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 |