Share via

Macro for formatting nested tables

Anonymous
2017-11-21T09:41:38+00:00

Hi There, 

 I'm hoping maybe someone can help me -  I am trying to Autofit to Window a series of nested tables via macro. 

Any help would be greatly appreciated.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2017-11-25T13:57:32+00:00

    Hi James,

    If you are building one from scratch programmatically you have to build the host table first and when you do, set its AutoFitBehavior property to wdAutoFitWindow. Then insert the nested cells and let the AutoFitBehavior default.

    Sample code below:

    Sub NestedTable()

        Dim doc As Word.Document, rng As Word.Range, tbl As Word.Table

        Set doc = ActiveDocument

        doc.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _

            2, AutoFitBehavior:=wdAutoFitWindow

        Set tbl = Selection.Tables(1)

        Set rng = tbl.Rows(1).Cells(1).Range

        rng.Collapse Word.WdCollapseDirection.wdCollapseStart

        rng.Tables.Add rng, NumRows:=1, NumColumns:=1

    End Sub

    If this doesn't help, provide a sample of the code you are using and tell us what is happening that you want to correct.

    Was this answer helpful?

    0 comments No comments
  2. Charles Kenyon 167.7K Reputation points Volunteer Moderator
    2017-11-21T10:55:04+00:00

    Rather than using a macro, have you considered saving your table in Building Blocks?

    I do not understand what you mean when you say "Autofit to Window."

    Was this answer helpful?

    0 comments No comments