Sunday, January 29, 2012

How to Search and Highlight/Tag a string in Word VBA

How to Search Content for Specific String/Text using Word VBA

This action is performed often by programmers - there are couple of ways to do

1. Selection.Find
2. Content.Find

We will have a look at how to search a string, highlight the string and tag the same using Word VBA. This needs document to be open

Sub Highlight_Tag_Found_Word()

Dim sFindText As String

sFindText = "Olympics"


Selection.HomeKey wdStory, wdMove


Selection.Find.Execute sFindText


Do Until Selection.Find.Found = False

        Selection.Range.HighlightColorIndex = wdPink
        Selection.InsertBefore "< FoundWord >"
        Selection.InsertAfter < /FoundWord >



End Sub

No comments:

Post a Comment

Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.