Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-19-2019, 11:28 AM
mrplastic mrplastic is offline Find and Replace Selected Text or Macro for finding selected text Windows 7 64bit Find and Replace Selected Text or Macro for finding selected text Office 2013
Novice
Find and Replace Selected Text or Macro for finding selected text
 
Join Date: Dec 2019
Posts: 3
mrplastic is on a distinguished road
Default Find and Replace Selected Text or Macro for finding selected text

I regularly have to search in word documents for a list of words and highlight each occurrence of those words. The problem is the list is variable from document to document so I have to copy+paste each word from the list one at a time in the find box and then replace all with highlighting.



Is there a way to set .text for the find function to whatever word(s) have been selected in the document?

The idea that I'm trying to achieve is the following workflow:

1) select word(s)
2) press shortcut key for macro
3) macro highlights every occurrence in the document of the word(s) from step 1, including my selection
4) repeat for every word in the list.

Thank you for any help you might be able to provide!
Reply With Quote
  #2  
Old 12-19-2019, 01:05 PM
mrplastic mrplastic is offline Find and Replace Selected Text or Macro for finding selected text Windows 7 64bit Find and Replace Selected Text or Macro for finding selected text Office 2013
Novice
Find and Replace Selected Text or Macro for finding selected text
 
Join Date: Dec 2019
Posts: 3
mrplastic is on a distinguished road
Default sample code for my question above

For context, I'm envisioning the following code but with "sample" replaced by code that will refer to whatever has been selected in the document.

Quote:
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "sample"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Last edited by mrplastic; 12-19-2019 at 01:06 PM. Reason: title
Reply With Quote
  #3  
Old 12-19-2019, 02:15 PM
macropod's Avatar
macropod macropod is offline Find and Replace Selected Text or Macro for finding selected text Windows 7 64bit Find and Replace Selected Text or Macro for finding selected text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Try, for example:
Code:
Sub BulkHighlighter()
Application.ScreenUpdating = False
Dim j As Long, StrFnd As String, HiLt As Long
HiLt = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
StrFnd = InputBox("Insert your 'Find' terms with | delimiters, for example:" & vbCr & "the|quick|brown|fox")
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .MatchWholeWord = True
  .MatchCase = False
  .Replacement.Highlight = True
  For j = 0 To UBound(Split(StrFnd, "|"))
    .Text = Split(StrFnd, "|")(j)
    .Replacement.Text = "^&"
    .Execute Replace:=wdReplaceAll
  Next
End With
Options.DefaultHighlightColorIndex = HiLt
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 12-20-2019, 08:09 AM
mrplastic mrplastic is offline Find and Replace Selected Text or Macro for finding selected text Windows 7 64bit Find and Replace Selected Text or Macro for finding selected text Office 2013
Novice
Find and Replace Selected Text or Macro for finding selected text
 
Join Date: Dec 2019
Posts: 3
mrplastic is on a distinguished road
Default didn't work

Hi! Thank you for your help! Unfortunately, I couldn't get this to work.


The other issue is that I'm trying to avoid any message boxes and conduct this task entirely within the document window. When I have preset lists of terms, I have these all plugged into a macro and it highlights them. Now that I have non-standard lists that solution doesn't work.


I'm currently manually entering words into the find & replace window. I'm specifically trying to get the macro to use the words that I select with the cursor in the document as the "find" term to replace with a highlighted version, rather than typing terms into the box.

Last edited by mrplastic; 12-20-2019 at 08:09 AM. Reason: forgot to thank you
Reply With Quote
  #5  
Old 12-20-2019, 01:25 PM
macropod's Avatar
macropod macropod is offline Find and Replace Selected Text or Macro for finding selected text Windows 7 64bit Find and Replace Selected Text or Macro for finding selected text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Quote:
Originally Posted by mrplastic View Post
I'm specifically trying to get the macro to use the words that I select with the cursor in the document as the "find" term to replace with a highlighted version, rather than typing terms into the box.
How do you propose to accommodate phrases, as distinct from individual words? My code accommodates words and phrases.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to find text in between two characters and then format selected text? qcom Word 5 02-19-2015 11:23 PM
Find and Replace Selected Text or Macro for finding selected text Find & selected paras with highlighted text & copy to clipboard marceepoo Word VBA 1 09-14-2012 08:20 PM
Find and Replace Selected Text or Macro for finding selected text Macro Delete Selected Text smonczka Word VBA 2 11-05-2011 03:18 AM
Mac Word - replace within selected text only. bakerbj Word 0 10-31-2011 11:47 PM
Selected text and logo in one macro marijke Word VBA 0 08-24-2009 07:01 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:06 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