none
Change the font of all the words in tables RRS feed

  • Question

  • I want to change the font of all the words (everything) in the tables of a Word document, for example, to "Arial". How to write the code?
    Wednesday, June 1, 2011 7:37 AM

Answers

  • Well, the best solution would be to create a style and apply that style to the table text. But I'm guessing that's not really what you're looking for.

    The following macro should work:

    Sub ChangeFontInAllTables()
      Dim tbl As word.Table
      Dim doc As word.Document
      
      Set doc = ActiveDocument
      For Each tbl In doc.Tables
        tbl.Range.Font.Name = "Arial"
      Next
    End Sub
    
    

    Cindy Meister, VSTO/Word MVP
    Wednesday, June 1, 2011 8:29 AM
    Moderator
  • It's basically the same as I gave you before, just some small changes. Substitute your style names in the code for the ones I used in my test scenario.

    Sub ChangeStylesInAllTables()
      Dim tbl As word.Table
      Dim doc As word.Document
      Dim bodyRows As word.Range
      Dim headingStyle As String
      Dim bodyStyle As String
      
      Set doc = ActiveDocument
      headingStyle = "Heading 1"
      bodyStyle = "body text"
      For Each tbl In doc.Tables
        tbl.Rows(1).Range.Style = headingStyle
        Set bodyRows = tbl.Range
        bodyRows.Start = tbl.Rows(2).Range.Start
        bodyRows.End = tbl.Rows(tbl.Rows.Count).Range.End
        bodyRows.Style = bodyStyle
      Next
    End Sub
    
    

    Cindy Meister, VSTO/Word MVP
    Thursday, June 2, 2011 6:57 AM
    Moderator

All replies

  • Well, the best solution would be to create a style and apply that style to the table text. But I'm guessing that's not really what you're looking for.

    The following macro should work:

    Sub ChangeFontInAllTables()
      Dim tbl As word.Table
      Dim doc As word.Document
      
      Set doc = ActiveDocument
      For Each tbl In doc.Tables
        tbl.Range.Font.Name = "Arial"
      Next
    End Sub
    
    

    Cindy Meister, VSTO/Word MVP
    Wednesday, June 1, 2011 8:29 AM
    Moderator
  • Cindy Meister, thank you.

    Yes, I agree with you. I have created a style for table header, called tb_heading, and a style for table body called tb_body.

    I want the headers of all tables in the Word document have the tb_heading style, and the body of all tables have the tb_body style. 

    Could you, or anyone please help me with that?

    Wednesday, June 1, 2011 3:23 PM
  • It's basically the same as I gave you before, just some small changes. Substitute your style names in the code for the ones I used in my test scenario.

    Sub ChangeStylesInAllTables()
      Dim tbl As word.Table
      Dim doc As word.Document
      Dim bodyRows As word.Range
      Dim headingStyle As String
      Dim bodyStyle As String
      
      Set doc = ActiveDocument
      headingStyle = "Heading 1"
      bodyStyle = "body text"
      For Each tbl In doc.Tables
        tbl.Rows(1).Range.Style = headingStyle
        Set bodyRows = tbl.Range
        bodyRows.Start = tbl.Rows(2).Range.Start
        bodyRows.End = tbl.Rows(tbl.Rows.Count).Range.End
        bodyRows.Style = bodyStyle
      Next
    End Sub
    
    

    Cindy Meister, VSTO/Word MVP
    Thursday, June 2, 2011 6:57 AM
    Moderator
  • Thank you. You are a true "Meister," which means "master" in German.
    Thursday, June 2, 2011 7:45 AM