#1
|
|||
|
|||
Need a macro adjustment
Hi. I have this macro from previous assistance. I am working on a document now with tons of tables that is set up differently. I need the tables -- even better would two specific table styles, which are N-Table1 and N-Table2 -- to be:
left-aligned, indented 0.3 inches, and 7.2" wide Code:
Sub TablesResizeAll() ' ' Resizes all tables in document to 6.5" wide, centered ' Dim oTbl As Table For Each oTbl In ActiveDocument.Tables oTbl.AutoFitBehavior wdAutoFitFixed With ActiveDocument.PageSetup oTbl.PreferredWidth = .PageWidth - .LeftMargin - .RightMargin oTbl.Rows.Alignment = wdAlignRowCenter End With Next oTbl End Sub |
#2
|
||||
|
||||
The preferred way to apply the left indent is to apply a table style since this is part of the table style and shouldn't be locally set. The second macro shows how to change the relevant attributes of your chosen table style if they weren't already correctly set.
Code:
Sub TablesResizeAll() Dim oTbl As Table For Each oTbl In ActiveDocument.Tables oTbl.AutoFitBehavior wdAutoFitFixed oTbl.PreferredWidthType = wdPreferredWidthPoints oTbl.PreferredWidth = InchesToPoints(7.2) oTbl.Style = "Table Grid" 'also turns header row, 1st column & banded rows on Next oTbl End Sub Sub SetupTableStyle() With ActiveDocument.Styles("Table Grid").Table .Alignment = wdAlignRowLeft .LeftIndent = InchesToPoints(0.3) End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Ooops
Andrew, I am very grateful for your prompt help. Unfortunately, I don't know enough about macros to have told you whatever was needed to prevent your macro from stripping all the other formatting from the tables -- shading, border colors, etc. I quickly closed the document before it saved and am back to where I was.
The may be no way to deal with this wholesale. I'll revisit the several table styles I am working with, add the indent, and specify the width. Cheers, Philip |
#4
|
||||
|
||||
OK, if standardising your tables more comprehensively is not what you want then you can just set the alignment without restyling.
Code:
Sub TablesResizeAll() Dim oTbl As Table For Each oTbl In ActiveDocument.Tables oTbl.AutoFitBehavior wdAutoFitFixed oTbl.PreferredWidthType = wdPreferredWidthPoints oTbl.PreferredWidth = InchesToPoints(7.2) oTbl.Rows.Alignment = wdAlignRowLeft oTbl.Rows.LeftIndent = InchesToPoints(0.3) Next oTbl End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Revised
Thankyou very much, sir. This will be a great tool to have. It seems strange that the table width is grayed-out in the Table Properties dialogue in Modify Table Style. But I have read that Word's table coding is "legacy", arcane, and extremely complex.
|
#6
|
|||
|
|||
Hello again. I just tried out the second one -- excellent! One further question, if I may.
Is there a line or lines that can be inserted in the code to have the macro ignore tables that have text wrap-around, rather than being inline? In a number of proposals, we put wrap-around graphics in tables and, come to think of it, we also have text callout boxes that are tables. |
#7
|
|||
|
|||
Dear ULOdesk, You can use the following code to target tables that are wrapped around text.
Code:
Sub Formattingtables() Dim i As Integer Dim ii As Integer Dim doc As Document Set doc = ActiveDocument ii = doc.Tables.Count For i = 1 To ii If doc.Tables(i).Rows.WrapAroundText = True Then 'Ignore the wrapped Tables Else 'Apply formatting to Non - wrapped Tables With doc.Tables(i) .AutoFitBehavior wdAutoFitFixed .PreferredWidthType = wdPreferredWidthPoints .PreferredWidth = InchesToPoints(7.2) .Rows.Alignment = wdAlignRowLeft .Rows.LeftIndent = InchesToPoints(0.3) End With End If Next End Sub |
#8
|
|||
|
|||
Bikram, thank you very much for your expertise. This does indeed ignore the wrap-around tables, which is great.
I am finding an anomaly in some non-wrap tables, however, and perhaps you can help me understand. It seems that I have once again failed to include every necessary variable in my presentation of the problem, for which I apologize; I am out of my league when it comes to macros. In the test document I tried on which I tried out your macro, there were some tables that had a width set, and although they indented properly, the width remained as it was. I started with a fresh copy (made from a template that had the tables in it as samples). The template is standard 8.5x11-inch paper with 1-inch margins, hence the tables had been specified at 6.5 inches wide, left-aligned. The column widths had been dragged in when originally designed, and hence they had specified measurements as well. In the fresh copy, I changed the document margins to a half-inch, selected one such table, opened properties, and then unchecked the table width box and deleted each column width value. When I ran the macro, the 6.5-inch width remained, and when I opened the table properties dialogue, width box was checked and the value was back in the table width field. I can only guess that perhaps this is related to the "Automatically fit size to contents" box in Options being unchecked, which is always my preference in the proposal work we do, for various reasons. I don't know if there is an inherent conflict here or if, perhaps, there is code that could be inserted that would allow the macro to override and allow the width for such tables to be set as otherwise specified by the macro. Although a majority of our documents work with our templates, we do get outliers like the one that prompted this thread, in which we are obliged to work in the client's document, and which is already more than 150 pages with about 80 non-wrapped tables so far, and the need to indent 0.3 and change the width to 7.2. This macro is potentially an enormous boon in such circumstances, and even I can see how to adjust the indent and width values. I would be most grateful for any further attention you can give this. |
#9
|
||||
|
||||
Does this one solve the allow autofit issue?
Code:
Sub TablesResizeAll() Dim oTbl As Table For Each oTbl In ActiveDocument.Tables If Not oTbl.Rows.WrapAroundText Then oTbl.AllowAutoFit = False oTbl.AutoFitBehavior wdAutoFitFixed oTbl.PreferredWidthType = wdPreferredWidthPoints oTbl.PreferredWidth = InchesToPoints(7.2) oTbl.Rows.Alignment = wdAlignRowLeft oTbl.Rows.LeftIndent = InchesToPoints(0.3) End If Next oTbl End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
Yes!
Andrew, thank you, with my apologies for the delay in responding. This works perfectly.
Much obliged. Philip |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro adjustment needed | Ulodesk | Word VBA | 2 | 02-06-2020 06:49 AM |
IF Statement Adjustment | shawn.low@cox.net | Mail Merge | 0 | 09-23-2019 10:08 AM |
Can I disable auto adjustment of sizes in Smart art Hierarchy charts? | frankgoldman | PowerPoint | 1 | 05-04-2019 04:37 AM |
Automatic adjustment date | zbiku25 | Project | 3 | 02-28-2017 09:03 AM |
Table adjustment on Surface OneNote | TheMax | OneNote | 1 | 01-23-2015 04:33 PM |