Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 10-19-2016, 07:01 AM
NorthSuffolk NorthSuffolk is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Oct 2016
Posts: 5
NorthSuffolk is on a distinguished road
Default VBA to distribute all columns evenly within tables

Hi all,



I am looking for VBA code which will find all tables within a Word document and distribute the content of these columns evenly (in the same way that you can manually select a table in Word, select Layout and Distribute Columns).

Thanks for your help.
Reply With Quote
  #2  
Old 10-19-2016, 04:30 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2013
Expert
 
Join Date: Mar 2010
Location: Melbourne Australia
Posts: 551
Guessed is on a distinguished road
Default

The following code will do what you asked.
Code:
Sub Macro1()
  Dim aTbl As Table
  For Each aTbl In ActiveDocument.Tables
    'aTbl.PreferredWidthType = wdPreferredWidthPercent
    'aTbl.PreferredWidth = 100
    If aTbl.Columns.Count > 1 Then aTbl.Range.Cells.DistributeWidth
  Next aTbl
End Sub
The two disabled lines could be enabled if you want all the tables to fill the width of the page as well.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 09-21-2017, 08:01 AM
PhoenixvillePA PhoenixvillePA is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Sep 2017
Posts: 1
PhoenixvillePA is on a distinguished road
Default Odd Behaviour when adjusting column width via Word VBA

I followed the VBA sample from the above thread and it works fine on a newly created table from Word.

In my scenario, I am creating a Word document from another application. The 3rd party application creates tables and using a Word template which I supplied, it pastes HTML into the Word document. The application creates a bunch of tables in the Word document and the column widths are different. I want to make them evenly distributed.

When I run the routine noted above, it doesn't work on first try. If I then go to one of the tables and adjust the column width on any random column and then go back and run the macro again, the table resizes the columns. It only seems to work for those tables in the document where I manually resize a column.

Obviously this defeats the purpose of the macro.

Anyone have any ideas why this behaviour is occurring? Does it have something to do with the HTML that is used to create the table when the document is first created?

Appreciate any thoughts and/or corrective actions.
Reply With Quote
  #4  
Old 09-23-2017, 05:28 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2013
Expert
 
Join Date: Mar 2010
Location: Melbourne Australia
Posts: 551
Guessed is on a distinguished road
Default

Without seeing a sample document this might be hard to fix.
Can you post a sample document which includes a couple of tables that the macro doesn't work on?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
tables, vba
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA: Selecting columns from multiple tables LouterMattheus Excel Programming 1 07-22-2015 06:33 AM
Tables on a page in twomutiliple columns derklauer Word 1 05-15-2015 08:11 PM
Evenly Space Text Boxes in Word Squint Word 2 12-30-2013 09:08 AM
How do I add columns to existing tables? dpcalder Word 1 08-06-2013 09:49 AM
Columns or tables? Linda O Word 6 01-12-2011 08:11 AM


All times are GMT -7. The time now is 09:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft