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

VBA-Excel: Change Font, Color, Weight of Table Data in the Word document

To Change Font, Color, Weight of Table Data in the Word doc­u­ment using Microsoft Excel, you need to fol­low the steps below (First we will cre­ate a table and while fill­ing the data we change the font, color and weight of data):

  • Cre­ate the object of Microsoft Word
  • Cre­ate a doc­u­ment object and add doc­u­ments to it
  • Make the MS Word visible
  • Cre­ate a Range object.
  • Cre­ate Table using Range object and define no of rows and columns.
  • Get the Table object
  • Enable the bor­ders of table using table object.
  • Fill the data in table (Here we will insert some code to make changes in data)
  • Save the document


 

Cre­ate the object of Microsoft Word

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

Cre­ate a doc­u­ment object and add doc­u­ments to it

Set obj­Doc = objWord.Documents.Add

Make the MS Word Visible

objWord.Visible = True

Cre­ate a Range object.

Set objRange = objDoc.Range

Cre­ate Table using Range object and define no of rows and columns.

objDoc.Tables.Add objRange, int­NoOfRows, intNoOfColumns

Get the Table object

Set objTable = objDoc.Tables(1)

Enable the bor­ders of table using table object.

objTable.Borders.Enable = True

Fill the data in table

objTable.Cell(1, 1).Range.Text = “Col­ored Data”

objTable.Cell(1, 1).Range.Font.Color = RGB(255, 0, 0)
objTable.Cell(1, 1).Range.Bold = True

Save the Document

objDoc.SaveAs (“D:\MyFirstSave”)

 

Com­plete Code:

Function FnAddTableToWordDocument()

   Dim intNoOfRows

   Dim intNoOfColumns

   Dim objWord

   Dim objDoc

   Dim objRange

   Dim objTable

 intNoOfRows = 5

 intNoOfColumns = 3

  Set objWord = CreateObject("Word.Application")

  objWord.Visible = True    

  Set objDoc = objWord.Documents.Add    

  Set objRange = objDoc.Range

  objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns    

  Set objTable = objDoc.Tables(1)

    objTable.Borders.Enable = True   

  For i = 1 To intNoOfRows

     For j = 1 To intNoOfColumns

       objTable.Cell(i, j).Range.Text = "Colored Data_" & i & j
       objTable.Cell(i, j).Range.Font.Color = RGB(255, 0, 0)
       objTable.Cell(i, j).Range.Bold = True
     Next
Next

End Function

 

Change Font, Color, Weight of Table Data in the Word document

Change Font, Color, Weight of Table Data in the Word document

You may also like...

Leave a Reply

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

%d bloggers like this: