Be the first user to complete this post

  • 0
Add to List

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

To Find a word in a specific paragraph and change its formatting in an existing Microsoft Word Document using Microsoft Excel, you need to follow the steps below:

  • Create the object of Microsoft Word
  • Create Doc object using MS word object, Open the existing word document by providing the complete path
  • Make the MS Word visible
  • Get the paragraphs count
  • Get the paragraph object using Paragraphs.Range
  • Find the specific word
  • Do the formatting
  • Save the word document
  • Close the word document

Create the object of Microsoft Word

Set objWord = CreateObject(“Word.Application”)

Create Doc object using MS word object, Open the existing word document by providing the complete path

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

Make the MS Word Visible

objWord.Visible = True

Get the paragraphs count

objDoc.Paragraphs.Count

Get the paragraph object using Paragraphs.Range

objDoc.Paragraphs(i).Range

Find the specific word

objParagraph.Find.Text = "paragraph"

Do

objParagraph.Find.Execute

If objParagraph.Find.Found Then

‘Formatting

Loop While objParagraph.Find.Found

Do the formatting

objParagraph.Font.Name = "Times New Roman"

Save the Word Document

objDoc.Save

Close the word document

objWord.Quit

Complete 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



Also Read:

  1. VBA-Excel: Format already written text in a word document – Format Paragraphs
  2. Excel-VBA : Send Unique Images Embedded to Mail Body, With Every Mail From MS Outlook using Excel.
  3. VBA-Excel: Add Worksheets For All The Given Dates Except Weekends and Copy The Common Template In Each Worksheet
  4. Send Mail With Multiple Different Attachments From MS Outlook using Excel.
  5. VBA-Excel: Format already written text in a word document – Format All Content