Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
MichaelB
 
Posts: n/a
Default Macro to find specific tables

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

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

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

  #3   Report Post  
Posted to microsoft.public.word.tables
MichaelB
 
Posts: n/a
Default Macro to find specific tables

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 Im 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


  #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



  #5   Report Post  
Posted to microsoft.public.word.tables
MichaelB
 
Posts: n/a
Default Macro to find specific tables

Jay,

Thanks again, this is more help then you will ever know€¦!

Mike


"Jay Freedman" wrote:

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






  #6   Report Post  
Posted to microsoft.public.word.tables
MichaelB
 
Posts: n/a
Default Macro to find specific tables

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), Im 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 Im your man).

Mike


"MichaelB" wrote:

Jay,

Thanks again, this is more help then you will ever know€¦!

Mike


"Jay Freedman" wrote:

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




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

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

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


"MichaelB" wrote:

Jay,

Thanks again, this is more help then you will ever know.!

Mike


"Jay Freedman" wrote:

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



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail merge macro does not find database query katescotland Mailmerge 0 September 9th 05 09:17 AM
How can I create a macro to find and replace text attributes? pubgal23 Microsoft Word Help 3 September 8th 05 06:09 AM
Mega macro with find and replace (HELP!) DKM Microsoft Word Help 1 May 10th 05 06:15 PM
How do I find my macro files for word so I can import them to my . hikingbarbiedoll Microsoft Word Help 1 March 2nd 05 10:50 PM
Should be simple! Macro to find italics won't run LizW Microsoft Word Help 2 November 30th 04 06:32 PM


All times are GMT +1. The time now is 04:06 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"