PDA

View Full Version : Solved: Table width (and column width) in VBA



view
09-02-2009, 06:23 AM
Hello again all,

In this I'm trying to;

1) Insert a 3x4 table
2) Apply a style
3) Set the widths of the columns (to 20%, 40% and 40%)
4) Add the text for the headers.

I've managed to get 1, 2 and 4 working as I want. But I'm having some real problems with 3.

If I don't include any code todo with the widths, then the table is inserted fine, spans the width of the document (to the margins) and has all 3 columns the same width.

However, when I try to manipulate the width of the columns, it seems to be going horribly wrong.

This is the code I have so far;

With ActiveDocument
'...
Set myRange = .Bookmarks("Body").Range
.Tables.Add Range:=myRange, NumRows:=4, NumColumns:=3
.Tables(1).Style = "StyleTHD"
' --------------------------
.Tables(1).PreferredWidthType = wdPreferredWidthPercent
.Tables(1).PreferredWidth = 100
.Tables(1).Columns(1).Width = 20
.Tables(1).Columns(2).Width = 40
.Tables(1).Columns(3).Width = 40
' --------------------------
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
Selection.TypeText ("Date")
Selection.MoveRight Unit:=wdCell
Selection.TypeText ("Aim")
Selection.MoveRight Unit:=wdCell
Selection.TypeText ("Activity")
Selection.MoveRight Unit:=wdCell
With the above code, it seems that the table width is set to 20% (of the document), then the columns are set as percentages of the table, resulting in a really small table squished to the left margin.

What I'm trying to achieve is the table to be full width of the document (not in the margins though), then the first column to be 20%, second and third both 40% of the full table width.

Any thoughts on how to solve this? Hope I've explained what I'm trying to do properly and shown what I'm doing so far.

Any help is greatly appreciated.

lucas
09-02-2009, 09:39 AM
Maybe this will help:

http://www.eggheadcafe.com/software/aspnet/34854287/setting-table-column-widt.aspx

view
09-08-2009, 03:06 AM
Sorry for the incredibly delayed reply - was changing ISP so been stuck offline for a while!

Thank you for the link Lucas - it gave me some new things to try, but unfortunately the problem is still not resolved.

I have tried setting preferred width type for the columns, using different arguments for autofit, dropping width to 0 and then back up to 100 (percent) etc the table always ends the same!

If I don't include the column width lines of code then the table spans the full width of the document as it should. Any attempt to modify the column widths seems to shrink the table towards the left margin.

Any thoughts? Or is the answer in the link you posted and I'm just not getting it right.

lucas
09-08-2009, 10:13 AM
I'm not very good with word and tables specifically but this seems to be something that might work:

With ActiveDocument
'...
Set myRange = .Bookmarks("Body").Range
.Tables.Add Range:=myRange, NumRows:=4, NumColumns:=3
' .Tables(1).Style = "StyleTHD"
' --------------------------
With ActiveDocument.Tables(1)
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).PreferredWidth = 20
.Columns(2).PreferredWidth = 40
.Columns(3).PreferredWidth = 40
End With
' --------------------------
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
Selection.TypeText ("Date")
Selection.MoveRight Unit:=wdCell
Selection.TypeText ("Aim")
Selection.MoveRight Unit:=wdCell
Selection.TypeText ("Activity")
Selection.MoveRight Unit:=wdCell
End With

fumei
09-08-2009, 01:47 PM
Notice that Steve has commented out the instruction for applying the table style.

' .Tables(1).Style = "StyleTHD"


Unfortunately, while an excellent idea, table styles in Word can be..ummmm, to be charitable...flaky.

Personally, I like both the idea, and in certain situations, the application of table styles. However, once applied, changing the attributes can take things off into la-la land.

This is what you seem to be doing. You apply the StyleTHD style, then alter it. Or so it seems to me.

Perhaps if you altered the style to be what you want it to be first?

Or, just alter the table as you want without the style (which is what Steve does).

In any case, it may also be better if you used a table object. I am unclear as to where - exactly - your text is going in the table, as I never use Selection.Move with tables, but here is the code for the procedure in the attached file. You can execute it by clicking "Make Table" on the top toolbar.

Option Explicit

Sub MakeTable()
Dim oTable As Table

ActiveDocument.Bookmarks("here").Select
With Selection
.Collapse 0
.MoveRight Unit:=wdCharacter, Count:=1
End With
Set oTable = ActiveDocument.Tables.Add _
(Range:=Selection.Range, NumRows:=4, NumColumns:=3)
With oTable
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).Width = 20
.Columns(2).Width = 40
.Columns(3).Width = 40
.Cell(1, 1).Range.Text = ("Date")
.Cell(1, 2).Range.Text = ("Aim")
.Cell(1, 3).Range.Text = ("Activity")
.AutoFitBehavior (wdAutoFitWindow)
End With
End Sub

It uses a bookmark as a locator to place the table. You use a bookmark range itself to place your table. I am not sure why you are doing that.

view
09-09-2009, 01:31 AM
Ah thank you both very much for your help with this.

Fumei you're right that I apply the style and then modify it - it honestly never occured to me that that would not be a good idea.

The style applied changes some of the aesthetics of the table - didn't think the column widths were involved in that. Will have to investigate.

The code block you produced works great thanks.

Used the range as I'm still struggling with the syntax of VBA and many of the examples I've read recently have used the range - didn't know about the Select bit.

Thanks again both - all working perfectly now.

fumei
09-10-2009, 10:47 AM
Excellent. Keep at it. It is all a learning experience, and we ALL had to go through it.

Basically, if you can avoid Selecting things, then do so. With VBA, in most cases, there actually is no need to select anything. This applies to Excel as well.