Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.tables
|
|||
|
|||
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
Posted to microsoft.public.word.tables
|
|||
|
|||
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
Posted to microsoft.public.word.tables
|
|||
|
|||
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
Posted to microsoft.public.word.tables
|
|||
|
|||
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
Posted to microsoft.public.word.tables
|
|||
|
|||
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
Posted to microsoft.public.word.tables
|
|||
|
|||
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
Posted to microsoft.public.word.tables
|
|||
|
|||
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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Mail merge macro does not find database query | Mailmerge | |||
How can I create a macro to find and replace text attributes? | Microsoft Word Help | |||
Mega macro with find and replace (HELP!) | Microsoft Word Help | |||
How do I find my macro files for word so I can import them to my . | Microsoft Word Help | |||
Should be simple! Macro to find italics won't run | Microsoft Word Help |