Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Find a word in a specific paragraph and change its formatting

To Find a word in a spe­cific para­graph and change its for­mat­ting in an exist­ing Microsoft Word Doc­u­ment using Microsoft Excel, you need to fol­low the steps below:

  • Cre­ate the object of Microsoft Word
  • Cre­ate Doc object using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path
  • Make the MS Word visible
  • Get the para­graphs count
  • Get the para­graph object using Paragraphs.Range
  • Find the spe­cific word
  • Do the formatting
  • Save the word document
  • Close the word document

 

 

Cre­ate the object of Microsoft Word

Set obj­Word = CreateObject(“Word.Application”)

Cre­ate Doc object using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path

Set obj­Doc = objWord.Documents.Open(“D:\OpenMe.docx”)

Make the MS Word Visible

objWord.Visible = True

Get the para­graphs count

objDoc.Paragraphs.Count

Get the para­graph object using Paragraphs.Range

objDoc.Paragraphs(i).Range

Find the spe­cific word

objParagraph.Find.Text = “paragraph”

Do

objParagraph.Find.Execute

If objParagraph.Find.Found Then

For­mat­ting

Loop While objParagraph.Find.Found

Do the formatting

objParagraph.Font.Name = “Times New Roman”

Save the Word Document

objDoc.Save

Close the word document

obj­Word.Quit

 

Com­plete Code:

Function FnFindAndFormat()

   Dim objWord

   Dim objDoc

   Dim intParaCount

   Dim objParagraph

   Set objWord = CreateObject("Word.Application")

   Set objDoc = objWord.Documents.Open("D:\OpenMe.docx")

   objWord.Visible = True 

   intParaCount = objDoc.Paragraphs.Count

   'MsgBox intParaCount

 Set objParagraph = objDoc.Paragraphs(2).Range    

     objParagraph.Find.Text = "paragraph"

     Do

         objParagraph.Find.Execute

         If objParagraph.Find.Found Then

            objParagraph.Font.Name = "Times New Roman"

            objParagraph.Font.Size = 20

            objParagraph.Font.Bold = True

            objParagraph.Font.Color = RGB(200, 200, 0)

        End If

 Loop While objParagraph.Find.Found  

End Function
Format-find and format

Format-find and format

 

You may also like...

1 Response

  1. Neil says:

    How can you get the page # and Line # of each word found?

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: