Macro to find specific tables

M

MichaelB

I am trying to figure out how to create a Macro that would find specific
tables within a document. I have several tables within some rather large
document that need to be modified to a new format. I need to identify how to
create a search for all the tables (several hundred) within the document
which have four(4) columns only. Once these tables are found I need to alter
the width of the columns to 3.67†without affecting any font styling that
currently exist.

If someone could help me out with the search criteria I'm sure I can put the
rest together.

Any help would be greatly appreciated.
Thanks
Mike
 
J

Jay Freedman

Hi Mike,

You don't have to "find" the tables. You just run a For Each loop over
all the tables, and modify only the ones that have four columns. This
should do it:

Sub ResizeTables()
Dim oTbl As Table

For Each oTbl In ActiveDocument.Tables
With oTbl
If .Columns.Count = 4 Then
.AutoFitBehavior Behavior:=wdAutoFitFixed
.Columns.PreferredWidthType = wdPreferredWidthPoints
.Columns.PreferredWidth = InchesToPoints(3.67)
End If
End With
Next oTbl
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
M

MichaelB

Jay,

Thanks for the information I truly appreciate it..! With me being new to
VBA I am trying to understand it as much as I can. Is it possible to modify
the widths of each column separately? If I wanted to make column 1 = 1â€,
column 2 = .69†and column 3 = .63†and column 4 = 3†how would I go about
doing so within the script you provided? I believe if I was able to see this
I would better understand the table objects (maybe not). I believe I am lucky
enough to have no merged or split cells therefore I’m hoping this to be
pretty straight forward (just beyond my comprehension is all).

Can you also help me understand the script by defining what the two lines
within are doing (meaning)?

..AutoFitBehavior Behavior:=wdAutoFitFixed
..Columns.PreferredWidthType = wdPreferredWidthPoints

Again, thanks for the help, I am truly grateful.

Mike
 
J

Jay Freedman

Hi Mike,

No problem!

First, the code to make the column widths different:

Sub ResizeTables()
Dim oTbl As Table

For Each oTbl In ActiveDocument.Tables
With oTbl
If .Columns.Count = 4 Then
.AutoFitBehavior Behavior:=wdAutoFitFixed
.Columns.PreferredWidthType = wdPreferredWidthPoints
.Columns(1).PreferredWidth = InchesToPoints(1#)
.Columns(2).PreferredWidth = InchesToPoints(0.69)
.Columns(3).PreferredWidth = InchesToPoints(0.63)
.Columns(4).PreferredWidth = InchesToPoints(3#)
End If
End With
Next oTbl
End Sub

Now, as to what's happening:

- The .AutoFitBehavior applies to the whole table (because of the With oTbl
statement), and it's equivalent to the menu item Table > AutoFit > Fixed
Column Width. That keeps the columns from resizing based on the contents or
the page width.

- The statement
.Columns.PreferredWidthType = wdPreferredWidthPoints
also refers to the whole table, and it says that when a column's
PreferredWidth value is set, the number should be interpreted as a
measurement in points, rather than a percentage of the width of the table.

The original macro sets a single PreferredWidth value for all the columns at
once, because it uses .Columns.PreferredWidth. The revised macro uses the
index numbers of the columns to set each one individually.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
M

MichaelB

Jay,

One more question, PLEASE..? They are killing me here; they now want me to
align all tables to right of the page. I have been able to do it individually
but I can not figure out how to loop it into the script you had created for
me. Any ideas how I can incorporate this justification into the script so
that the tables being reformated are also being re-aligned to the right of
the page?

Sorry to be a pain (in the you know what), I’m really not liking tables at
the moment. Please note I am extremely grateful for your help (I owe you
big, if you ever need a mechanic I’m your man).

Mike
 
J

Jay Freedman

Hi Mike,

The line of code you need is

.Rows.Alignment = wdAlignRowRight

If you want just the four-column tables to be aligned to the right, put the
new line between the "If .Columns.Count = 4 Then" and the ".AutoFitBehavior
Behavior:=wdAutoFitFixed" line. If you want all tables aligned to the right
regardless of how many columns there are, put the new line just before the
"If .Columns.Count = 4 Then" line.

Actually, I could have used your services last night. :) My 48-month
battery died at only 40 months. Oh well...

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top