Macro lines Skipped for Table Format

G

Glenn Sasscer

I have a macro creating a table and formating the column widths in Word 2000,
where it works perfectly. In Word 2003 or 2007, it skips the width
formating.

Code:

ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(1.1)
ActiveDocument.Tables(1).Cell(1, 2).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(4.2)
ActiveDocument.Tables(1).Cell(1, 3).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(2.25)

In Word 2003 or 2007, if I stop the macro just before these lines adn F8
through each line, the widths work. If I stop it just before these lines and
just after these lines, it widths do not work.

Any suggestions?

- Glenn
 
M

macropod

Hi Glenn,

Try:
Sub Test()
With ActiveDocument.Tables(1)
.Columns(1).PreferredWidth = InchesToPoints(1.1)
.Columns(2).PreferredWidth = InchesToPoints(4.2)
.Columns(3).PreferredWidth = InchesToPoints(2.25)
End With
End Sub
 
D

Doug Robbins - Word MVP on news.microsoft.com

There was a response posted to your identical post in the
microsoft.public.word.vba.customization newsgroup on 3/21/2009. Please do
not post the same question separately to multiple newsgroups

--
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
 
G

Glenn Sasscer

Thanks Macropod. I tried it and it still did not work. Skips right over the
code as if it were not there, but if I F8 through it, it works fine.

Any other ideas?

macropod said:
Hi Glenn,

Try:
Sub Test()
With ActiveDocument.Tables(1)
.Columns(1).PreferredWidth = InchesToPoints(1.1)
.Columns(2).PreferredWidth = InchesToPoints(4.2)
.Columns(3).PreferredWidth = InchesToPoints(2.25)
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Glenn Sasscer said:
I have a macro creating a table and formating the column widths in Word 2000,
where it works perfectly. In Word 2003 or 2007, it skips the width
formating.

Code:

ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(1.1)
ActiveDocument.Tables(1).Cell(1, 2).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(4.2)
ActiveDocument.Tables(1).Cell(1, 3).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(2.25)

In Word 2003 or 2007, if I stop the macro just before these lines adn F8
through each line, the widths work. If I stop it just before these lines and
just after these lines, it widths do not work.

Any suggestions?

- Glenn
 
M

macropod

Hi Glenn,

On its own, the code I posted works correctly with both Word 2000 and Word 2007. Perhaps there's something else about your
implementation of the code or in the document itself that's causing you not to get the results you're after.

--
Cheers
macropod
[MVP - Microsoft Word]


Glenn Sasscer said:
Thanks Macropod. I tried it and it still did not work. Skips right over the
code as if it were not there, but if I F8 through it, it works fine.

Any other ideas?

macropod said:
Hi Glenn,

Try:
Sub Test()
With ActiveDocument.Tables(1)
.Columns(1).PreferredWidth = InchesToPoints(1.1)
.Columns(2).PreferredWidth = InchesToPoints(4.2)
.Columns(3).PreferredWidth = InchesToPoints(2.25)
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Glenn Sasscer said:
I have a macro creating a table and formating the column widths in Word 2000,
where it works perfectly. In Word 2003 or 2007, it skips the width
formating.

Code:

ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(1.1)
ActiveDocument.Tables(1).Cell(1, 2).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(4.2)
ActiveDocument.Tables(1).Cell(1, 3).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(2.25)

In Word 2003 or 2007, if I stop the macro just before these lines adn F8
through each line, the widths work. If I stop it just before these lines and
just after these lines, it widths do not work.

Any suggestions?

- Glenn
 
T

Tony Jollans

I can't say for sure what the problem is but, perhaps, can offer some
avenues to try. There are, potentially, two unrelated issues that may come
into play here.

Firstly, PreferredWidth is not an absolute setting. The width is 'preferred'
but if there are other settings on your Table, that conflict in any way, the
preference will not be honoured. So the fact that one person sees something
with one table, and another sees something else with another table means
nothing without a lot of further detail. It would be worth trying to use
Width rather than PreferredWidth.

Secondly, although less common, single stepping a macro does sometimes cause
it to work differently. When this happens it is often worth putting DoEvents
statements in the code, as this often triggers, or allows, things to happen
that would otherwise be queued up - in other words it can change the order
of events, particularly with respect to UI activity that is tied to the use
of Selection.
 
M

macropod

Hi Glenn,

You might need to set:
..AllowAutoFit = False

Otherwise, Word will try to balance the column widths to take account of any other columns that you aren't processing in the tables.

--
Cheers
macropod
[MVP - Microsoft Word]


Glenn Sasscer said:
Thanks Macropod. I tried it and it still did not work. Skips right over the
code as if it were not there, but if I F8 through it, it works fine.

Any other ideas?

macropod said:
Hi Glenn,

Try:
Sub Test()
With ActiveDocument.Tables(1)
.Columns(1).PreferredWidth = InchesToPoints(1.1)
.Columns(2).PreferredWidth = InchesToPoints(4.2)
.Columns(3).PreferredWidth = InchesToPoints(2.25)
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Glenn Sasscer said:
I have a macro creating a table and formating the column widths in Word 2000,
where it works perfectly. In Word 2003 or 2007, it skips the width
formating.

Code:

ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(1.1)
ActiveDocument.Tables(1).Cell(1, 2).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(4.2)
ActiveDocument.Tables(1).Cell(1, 3).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(2.25)

In Word 2003 or 2007, if I stop the macro just before these lines adn F8
through each line, the widths work. If I stop it just before these lines and
just after these lines, it widths do not work.

Any suggestions?

- Glenn
 

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