Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word 2010 table formatting using VBA macros

    Dear All,

    There are around 50 tables in my word document and I want to change the height and width of every cell in the document.

    I was able to get a macro which would select the entire tables but when I am changing the height its only getting applied to that specific table.

    I tried googling a lot and couldn't get any information.

    So could someone help me with a macro code which I could use? Height 1.5" and Width is 2.3"

  2. #2
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,719
    Thanks
    59
    Thanked 66 Times in 64 Posts
    You've discovered that while the Macro Recorder can be helpful, it is far from complete. There are many things it does not pick up.

    On my computer all that is picked up is the selection of the table.

    I don't know how to do this but expect it would help someone else helping you if you can specify whether you have any nested tables (tables within tables) and whether all of your tables have rows where all cells in a given row are the same height and whether all of the cells in a given column in each table are the same width.
    Charles Kyle Kenyon
    Madison, Wisconsin

  3. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,719
    Thanks
    59
    Thanked 66 Times in 64 Posts
    You may want to look at "Tables" here:
    http://word.mvps.org/FAQs/MacrosVBA/index.htm
    Charles Kyle Kenyon
    Madison, Wisconsin

  4. #4
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,719
    Thanks
    59
    Thanked 66 Times in 64 Posts
    I found the following by Bill Coan:
    http://word.mvps.org/FAQs/MacrosVBA/...CellsInTbl.htm
    Code:
    For Each oCell In oTable.Range.Cells
      oCell.Width = InchesToPoints(1)
      oCell.Shading.BackgroundPatternColorIndex = wdBlue
      oCell.Range.Font.Name = "Arial"
      oCell.Range.Font.Size = 20
    Next oCell
    Charles Kyle Kenyon
    Madison, Wisconsin

  5. #5
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,719
    Thanks
    59
    Thanked 66 Times in 64 Posts
    The following appears to do what you want. Give it a try:

    Code:
    Sub ChangeCells()
    ' Resizes all cells in active document to a set size (in inches)
    Dim oTable As Table, oCell As Cell
    For Each oTable In ActiveDocument.Tables
        For Each oCell In oTable.Range.Cells
          oCell.Width = InchesToPoints(2.3)
          oCell.Height = InchesToPoints(1.5)
        Next oCell
    Next oTable
    End Sub
    If you haven't worked with vba directly before, see Installing Macros by Graham Mayor.
    Last edited by Charles Kenyon; 2013-10-07 at 15:22.
    Charles Kyle Kenyon
    Madison, Wisconsin

  6. #6
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,719
    Thanks
    59
    Thanked 66 Times in 64 Posts
    There is almost certainly a more elegant way controlling rows and columns rather than individual cells.

    Because it took a bit of time and may be useful to someone else, I have put this code on my page about Tables.
    Last edited by Charles Kenyon; 2013-10-07 at 16:49.
    Charles Kyle Kenyon
    Madison, Wisconsin

  7. #7
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    197
    Thanks
    4
    Thanked 46 Times in 38 Posts
    You don't have to set each individual cell. As I replied to Whity's identical post on answers.microsoft.com,

    Code:
    Sub SetCellSize()
        Dim tbl As Table
        For Each tbl In ActiveDocument.Tables
            tbl.Rows.Height = InchesToPoints(1.5)
            tbl.AutoFitBehavior wdAutoFitFixed
            tbl.Columns.Width = InchesToPoints(2.3)
        Next
    End Sub

  8. The Following User Says Thank You to jjfreedman For This Useful Post:

    Charles Kenyon (2013-10-07)

  9. #8
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,719
    Thanks
    59
    Thanked 66 Times in 64 Posts
    Jay's code is a better method to do what you want. Mine may result in unnecessary complexity in the underlying document structure and should take longer to run.
    Charles Kyle Kenyon
    Madison, Wisconsin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •