Word 2003 - Setting Column Width

A

Alistair F

I have used VBA to manipulate Word tables for some time and have always used
the SetWidth method to do so. One limitation of this method is that I can't
set the width to a value less than 12 points. The PreferredWidth method
allows smaller values but I can't get method to work consistently. I have
code that sets some columns to a width of 3 points (these are 'spacer'
columns) and this works the first time the code is called but subsequent
calls do not work properly - columns do not resize at all. When I step
through the code using F8 the code works but when it runs freely it doesn't.

One thing I've noticed is that the PreferredWidth of the table is initially
not set but at some point when the code is running it is set and this value
seems to override individual column widths.

I'd appreciate advice on what to try next.

Thanks in anticipation.
 
K

Klaus Linke

Hi Alistair,

You can't set the width to a value less than the sum of .LeftPadding and
..RightPadding -- Maybe that's the problem?

Regards,
Klaus
 
A

Alistair F

Both are set to 1 point so that's not the problem. When I attempt to use, for
example:

columns(7).setwidth 6,wdadjustnone

I get an error 4608, Value out of range.

columns(7).preferredwidth=6

does work, but not consistently from code.
 
K

Klaus Linke

Alistair F said:
Both are set to 1 point so that's not the problem.

You sure that's one point, and not one mm? And is that the setting in "Table
properties > Table > Options..." or in the cell options?
The latter (although they should override the table settings), strangely
don't seem to matter.

Perhaps it's worth a try to set the left and right paddings to zero or a
small value, in "Table properties > Table tab, Options...", and see if it
works then.

Then I attempt to use, for example:

columns(7).setwidth 6,wdadjustnone

I get an error 4608, Value out of range.

That's the same error I get when the width is smaller than the sum of the
paddings.

I thought the border widths might interfere too, or some whithespace in the
cells, but a test seems to show they are ignored for determining the minimum
width you can set: They just cause the setting ("preferred width") to not
become the "real" column width.

One thing I'd make sure is to set .AllowAutoFit to false for the table, or
it may play tricks with you... although I don't suppose it has to do with
your trouble.

Regards,
Klaus
 
D

Doug Robbins - Word MVP on news.microsoft.com

Hi Klause,

Even after using code to set the left and right paddings to 0, trying to
subsequently set the width to a value like 6 produces the out of range error
for me whether I run the macro or use F8 to step through it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

Alistair F

The SetWidth method always generates an error with a width of less than 12
points but PreferredWidth does not. It's PreferredWidth that behaves
differently from code and using F8.

I guess I'll need to stick with 12 points and redesign the tables.

Cheers


Alistair
 
K

Klaus Linke

Doug Robbins - Word MVP on news.microsoft.com said:
Even after using code to set the left and right paddings to 0, trying to
subsequently set the width to a value like 6 produces the out of range
error for me whether I run the macro or use F8 to step through it.


I tried with several values for the cell paddings (Word 2003).
The code I used:

With Selection.Tables(1)
.LeftPadding = 1
.RightPadding = 1
End With
Selection.Columns(1).SetWidth _
ColumnWidth:=3, _
RulerStyle:=wdAdjustNone

As I said, the relevant settings seem to be the paddings defined in the
table, not the individual cells (... bit of a bug if you ask me).

No idea why it seems to work differently on your machine (or in your
version).

Regards,
Klaus
 
D

Doug Robbins - Word MVP on news.microsoft.com

Hi Klaus,

Using your code, I get the Out of Range error in both Word 2007 and Word
2003. I am not sure if the error in 2003 is due to both versions being
installed on the one machine. Do you have 2007 installed on the machine on
which you ran the code under 2003?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
K

Klaus Linke

Doug said:
Using your code, I get the Out of Range error in both Word 2007 and Word
2003. I am not sure if the error in 2003 is due to both versions being
installed on the one machine. Do you have 2007 installed on the machine
on which you ran the code under 2003?

No, just Word 2003 SP3. Could also be caused by some other setting that's
different on our machines, and it may remain a mystery.
Your getting an error message seems a bug though... Besides the padding, I
don't see what might cause the error here.

Klaus
 
K

Klaus Linke

BTW, if you want to mail me the table you ran the code on, I would check if
it's something in the document/table that makes the difference?

Greetings,
Klaus
 
D

Doug Robbins - Word MVP on news.microsoft.com

Hi Klaus,

It was just a simple 3 x 3 table created by using the Insert table icon on
the toolbar. There was no text in any of the cells.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
K

Klaus Linke

Doug said:
It was just a simple 3 x 3 table created by using the Insert table icon on
the toolbar. There was no text in any of the cells.

Well, that's what I assumed and what my test table looked like too, pretty
much.
My thought was that a lot of settings are stored in the document (coming
from the template), such as defaults for new tables, options settings, and
so on, and the difference might be there.

Klaus
 
D

Doug Robbins - Word MVP on news.microsoft.com

Hi Klaus,

I have sent a document to (e-mail address removed)

The Word 2003 installation on this machine however is just plain vanilla and
the only thing that it has been used for is to create toolbars as you can no
longer do that in Word 2007.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
K

Klaus Linke

Hi Doug,

Thanks... I get the error too with your doc, and can replicate it now with
new tables if I play around with AutoFit settings, paddings ..., but can't
find any pattern.
Sometimes I seem to get the error, and later (say after closing and
re-opening the doc) it works with the same(?) settings, and vice versa.
A while ago I looked at how tables are stored internally, and there's a lot
of messy stuff in there, say for HTML compatibility, that's not even
accessible from the user interface or VBA.
Maybe it'll remain a mystery... In case I find out anything, I'll post back.

Greetings,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top