View Single Post
  #4   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman
 
Posts: n/a
Default Macro to find specific tables

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

MichaelB wrote:
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



"Jay Freedman" wrote:

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Mon, 13 Mar 2006 14:35:01 -0800, MichaelB
wrote:

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