M
Martin Müller
Hello,
I'm just about to implement an application that creates a table in a Word
2003 document. Doing so, I came across an issue I really can't explain.
The following code (VBA) creates a new document, inserts some paragraphs and
then inserts a table and sets column widths:
Sub TableCraziness1()
Dim doc As Document
Set doc = Documents.Add
Dim i As Integer
For i = 1 To 80 ' make this number smaller to make it work or larger to
make it not work
doc.Content.InsertParagraphAfter
Next i
Dim r As Range
Set r = doc.Content
r.Collapse WdCollapseDirection.wdCollapseEnd
Dim t As Table
Set t = r.Tables.Add(r, 3, 4)
t.Columns.PreferredWidthType = wdPreferredWidthPercent
t.Columns(1).PreferredWidth = 10
t.Columns(2).PreferredWidth = 20
t.Columns(3).PreferredWidth = 30
t.Columns(4).PreferredWidth = 40
End Sub
Nothing special. But if the number of paragraphs inserted is too large
(about 40), then the table will not stretch from the left to the right across
the page, but the table will be much smaller after the column widths have
been set. Please note that Word behaves differently when the number of
paragraphs is small enough. I really don't know what a number of paragraphs
before a table has to do with how it is created.
The number of paragraphs needed so that this will happen sometimes changes
when I restart Word (maybe it depends on the temperature??). So if you like
to try it, please feel free to make the number large enough, e.g. 100.
However, this can easily be fixed with these lines:
t.PreferredWidth = 100
t.PreferredWidthType = wdPreferredWidthPercent
Ok. But when merging any cells within the table, the next strange thing
happens. With too many paragraphs before the table, merging any of the cells
will reset all column widths:
Sub TableCraziness2()
Dim doc As Document
Set doc = Documents.Add
Dim i As Integer
For i = 1 To 80 ' make this number smaller to make it work or larger to
make it not work
doc.Content.InsertParagraphAfter
Next i
Dim r As Range
Set r = doc.Content
r.Collapse WdCollapseDirection.wdCollapseEnd
Dim t As Table
Set t = r.Tables.Add(r, 3, 4)
t.PreferredWidth = 100
t.PreferredWidthType = wdPreferredWidthPercent
t.Columns.PreferredWidthType = wdPreferredWidthPercent
t.Columns(1).PreferredWidth = 10
t.Columns(2).PreferredWidth = 20
t.Columns(3).PreferredWidth = 30
t.Columns(4).PreferredWidth = 40
r.Document.Range(t.Rows(2).Cells(2).Range.Start,
t.Rows(2).Cells(3).Range.End).Cells.Merge
End Sub
But when inserting a wait loop after the line Set t = r.Tables.Add(...)
For i = 1 To 1000
DoEvents
Sleep 1
Next i
which requires a declaration
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
and using the time the loop waits to scroll the document down with the
mouse, the merging of the cells will not reset the column widths (at least
most of the time). (Please note that the wait alone will not change anything.)
I already tried different things which did NOT influence this issue:
- Application.Visible
- Application.ScreenUpdating
- starting Word with "winword /a" to supress the loading of add ins
- using the Select method at different places
- a different installation of Office 2003
My computer is running Windows XP with SP2, Office 2003 has SP3 installed.
Currently, I do not have any ideas left how to make this work properly. Is
there anything badly wrong with my code?
I'd appreciate any comments on this. Thank you in advance.
Martin
I'm just about to implement an application that creates a table in a Word
2003 document. Doing so, I came across an issue I really can't explain.
The following code (VBA) creates a new document, inserts some paragraphs and
then inserts a table and sets column widths:
Sub TableCraziness1()
Dim doc As Document
Set doc = Documents.Add
Dim i As Integer
For i = 1 To 80 ' make this number smaller to make it work or larger to
make it not work
doc.Content.InsertParagraphAfter
Next i
Dim r As Range
Set r = doc.Content
r.Collapse WdCollapseDirection.wdCollapseEnd
Dim t As Table
Set t = r.Tables.Add(r, 3, 4)
t.Columns.PreferredWidthType = wdPreferredWidthPercent
t.Columns(1).PreferredWidth = 10
t.Columns(2).PreferredWidth = 20
t.Columns(3).PreferredWidth = 30
t.Columns(4).PreferredWidth = 40
End Sub
Nothing special. But if the number of paragraphs inserted is too large
(about 40), then the table will not stretch from the left to the right across
the page, but the table will be much smaller after the column widths have
been set. Please note that Word behaves differently when the number of
paragraphs is small enough. I really don't know what a number of paragraphs
before a table has to do with how it is created.
The number of paragraphs needed so that this will happen sometimes changes
when I restart Word (maybe it depends on the temperature??). So if you like
to try it, please feel free to make the number large enough, e.g. 100.
However, this can easily be fixed with these lines:
t.PreferredWidth = 100
t.PreferredWidthType = wdPreferredWidthPercent
Ok. But when merging any cells within the table, the next strange thing
happens. With too many paragraphs before the table, merging any of the cells
will reset all column widths:
Sub TableCraziness2()
Dim doc As Document
Set doc = Documents.Add
Dim i As Integer
For i = 1 To 80 ' make this number smaller to make it work or larger to
make it not work
doc.Content.InsertParagraphAfter
Next i
Dim r As Range
Set r = doc.Content
r.Collapse WdCollapseDirection.wdCollapseEnd
Dim t As Table
Set t = r.Tables.Add(r, 3, 4)
t.PreferredWidth = 100
t.PreferredWidthType = wdPreferredWidthPercent
t.Columns.PreferredWidthType = wdPreferredWidthPercent
t.Columns(1).PreferredWidth = 10
t.Columns(2).PreferredWidth = 20
t.Columns(3).PreferredWidth = 30
t.Columns(4).PreferredWidth = 40
r.Document.Range(t.Rows(2).Cells(2).Range.Start,
t.Rows(2).Cells(3).Range.End).Cells.Merge
End Sub
But when inserting a wait loop after the line Set t = r.Tables.Add(...)
For i = 1 To 1000
DoEvents
Sleep 1
Next i
which requires a declaration
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
and using the time the loop waits to scroll the document down with the
mouse, the merging of the cells will not reset the column widths (at least
most of the time). (Please note that the wait alone will not change anything.)
I already tried different things which did NOT influence this issue:
- Application.Visible
- Application.ScreenUpdating
- starting Word with "winword /a" to supress the loading of add ins
- using the Select method at different places
- a different installation of Office 2003
My computer is running Windows XP with SP2, Office 2003 has SP3 installed.
Currently, I do not have any ideas left how to make this work properly. Is
there anything badly wrong with my code?
I'd appreciate any comments on this. Thank you in advance.
Martin