Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select every table in a Microsoft Word document using VBA Macro

Tags:

ms-word

vba

I am looking for a way (or decent introduction) into how to select every table in a Microsoft Word 2013 Document and autofit the contents. Each table is independent of one another and separated by text.

I have established the following code so far:

Sub autofit()

    Selection.Tables(1).AutoFitBehavior (wdAutoFitContent)

End Sub

Which works for individual tables and every column in said table, I understand the format of the "for loop", but would like a nudge to how to transform my individual selection to the entire document.

This is my first post so apologies for any conventions I have missed.

like image 980
Cellobin22 Avatar asked Dec 20 '22 08:12

Cellobin22


1 Answers

Its pretty trivial to loop them all;

Dim t As Table
For Each t In ActiveDocument.Tables
    t.AutoFitBehavior wdAutoFitContent
Next
like image 76
Alex K. Avatar answered Dec 21 '22 20:12

Alex K.