Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-27-2017, 12:40 PM
mike0215 mike0215 is offline Macro to search for specific words in a document Windows 10 Macro to search for specific words in a document Office 2016
Novice
Macro to search for specific words in a document
 
Join Date: Nov 2017
Posts: 4
mike0215 is on a distinguished road
Smile Macro to search for specific words in a document

Hi,

I want to create a macro that will run at the end of a much longer macro that will search for specific key words in the newly edited document. I've done some searching here and online and found a code that I thought would work, which is supposed to identify specific words and highlight them. The code runs but nothing seems to happen upon completion. Here's that code:

Code:
Sub FindAndHighlight()
'
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
StrFnd = "tasks,architecture,java"
For i = 0 To UBound(Split(StrFnd, ","))
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = Split(StrFnd, ",")(i)
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
'
End Sub

I want the macro to search for 3 unrelated key words in the document and to alert the user that either there are no matches or that there are some. If this could happen via some sort of dialog box that would be great / highlighting the specific words as the code above describes.



Any suggestions as to what's wrong with the code above / what I can do? Thanks!

Last edited by macropod; 11-27-2017 at 04:08 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 11-27-2017, 04:15 PM
macropod's Avatar
macropod macropod is offline Macro to search for specific words in a document Windows 7 64bit Macro to search for specific words in a document 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 code you posted assumes Word's highlighting has already been set to whatever highlights you want to use; if it's set to 'no highlighting, that's what you'll get. Try:
Code:
Sub FindAndHighlight()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdBrightGreen
Dim StrFnd As String, i As Long, StrRpt As String
StrFnd = "tasks,architecture,java"
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Replacement.Highlight = True
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchCase = False
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = True
  For i = 0 To UBound(Split(StrFnd, ","))
    .Text = Split(StrFnd, ",")(i)
    .Replacement.Text = "^&"
    .Execute Replace:=wdReplaceAll
    If .Found = True Then StrRpt = StrRpt & vbCr & Split(StrFnd, ",")(i)
  Next
End With
If StrRpt = "" Then
  MsgBox "None of the terms were found.", vbOKOnly
Else
  MsgBox "The following terms were found:" & StrRpt, vbInformation
End If
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-28-2017, 07:25 AM
mike0215 mike0215 is offline Macro to search for specific words in a document Windows 10 Macro to search for specific words in a document Office 2016
Novice
Macro to search for specific words in a document
 
Join Date: Nov 2017
Posts: 4
mike0215 is on a distinguished road
Default

Thanks, works like a charm!
Reply With Quote
Reply

Tags
find word highlight para, search, word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to search for specific words in a document Help creating VBA code to search multiple word documents for specific group of words dmreno Word VBA 3 07-30-2019 02:31 PM
Search the words of different document Sandhya Word 15 11-24-2014 01:29 PM
Macro to Search for all words with (R) and place in new document kd12 Word VBA 2 06-10-2014 02:25 PM
Macro to search for specific words in a document Need VBA For Macro On How To Remove Specific Words netchie Word VBA 6 08-28-2012 03:37 PM
Macro to search for specific words in a document Macro for highlighting specific number of words icsjohn Word VBA 2 12-07-2011 06:44 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 - 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