Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-07-2021, 01:16 PM
Ulodesk Ulodesk is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Word 2013 Expert Cert
Need a macro adjustment
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default 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
Many thanks!
Reply With Quote
  #2  
Old 12-07-2021, 01:56 PM
Guessed's Avatar
Guessed Guessed is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 12-07-2021, 02:23 PM
Ulodesk Ulodesk is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Word 2013 Expert Cert
Need a macro adjustment
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default 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
Reply With Quote
  #4  
Old 12-07-2021, 05:37 PM
Guessed's Avatar
Guessed Guessed is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 12-08-2021, 08:49 AM
Ulodesk Ulodesk is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Word 2013 Expert Cert
Need a macro adjustment
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default 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.
Reply With Quote
  #6  
Old 12-08-2021, 09:28 AM
Ulodesk Ulodesk is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Word 2013 Expert Cert
Need a macro adjustment
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 12-08-2021, 09:30 PM
Bikram Bikram is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2007
Advanced Beginner
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 12-09-2021, 07:10 AM
Ulodesk Ulodesk is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Word 2013 Expert Cert
Need a macro adjustment
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 12-09-2021, 03:38 PM
Guessed's Avatar
Guessed Guessed is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #10  
Old 12-13-2021, 08:47 AM
Ulodesk Ulodesk is offline Need a macro adjustment Windows 10 Need a macro adjustment Office 2016
Word 2013 Expert Cert
Need a macro adjustment
 
Join Date: Sep 2009
Location: Virginia
Posts: 866
Ulodesk is on a distinguished road
Default Yes!

Andrew, thank you, with my apologies for the delay in responding. This works perfectly.

Much obliged.

Philip
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a macro adjustment 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
Need a macro adjustment Automatic adjustment date zbiku25 Project 3 02-28-2017 09:03 AM
Need a macro adjustment Table adjustment on Surface OneNote TheMax OneNote 1 01-23-2015 04:33 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:04 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft