Apply column widths of currently selected table to all tables using VBA

Dear Experts:

I got a document with ...
... hundreds of tables.
... all of them have the exact same number of columns

Now I would like to manually adjust the column widths in just one (1) table and then by running the macro (with the cursor residing in the table whose columns got adjusted), the column widths of the currently selected table are applied to all of the remaining tables.

Is it possible to run such a macro?  

And one more thing: One of the documents may have tables with just 3 columns, other documents may have tables with 7 columns (as I stated before, all of the tables in the documents have the same number of columns). The macro should be able to accommodate any numbers of columns. .

I wonder whether this is possible? Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steven HarrisPresidentCommented:
One of the documents may have tables with just 3 columns, other documents may have tables with 7 columns

(as I stated before, all of the tables in the documents have the same number of columns). The macro should be able to accommodate any numbers of columns.

Can you clarify this?  Do they all have the same number of columns or not?
0
Andreas HermleTeam leaderAuthor Commented:
HI ThinkSpaceSolutions:

I would like to run the macro on different documents.
In one document for example all of the tables have 3 columns,
in another document all of the tables have 7 columns
in another document all of the tables have 2 columns
etc.

So the macro should be able to do its work in all documents. Within one document all tables have the same number of columns, between documents they can vary.

Thank you. Regards, Andreas
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Andreas

How about:

Sub Q_28227045_1()
Dim tbl As Table
Dim cols As Integer
Dim widths() As Double
Dim intCol As Integer

    If Not Selection.Information(wdWithInTable) Then Exit Sub
    cols = Selection.Tables(1).Columns.Count
    ReDim widths(1 To cols)
    
    For intCol = 1 To cols
        widths(intCol) = Selection.Tables(1).Columns(intCol).Width
    Next
    
    For Each tbl In ActiveDocument.Tables
'        If tbl.Columns.Count = cols Then
            For intCol = 1 To cols
                tbl.Columns(intCol).Width = widths(intCol)
            Next
'        End If
    Next

End Sub

Open in new window


Note I have deliberately disabled the count on columns ... I would generally add this check for  reliability but I seem to recall you are certain on the table uniformity.  If I am wrong then just remove the comment prefix on the two lines.


Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andreas HermleTeam leaderAuthor Commented:
Chris,

great job, Chris. Thank you very much for your professional help.

Regards, Andreas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.