ch00seLife
01-30-2009, 12:48 PM
I'm trying to write a piece of generated Word Document content that involves writing tables. I want all of the tables to be formatted the same way--indented 1/2 inch from the left. I also need to separate them with paragraphs, and I want to set the column widths.
However, whenever I try to do all of this, the left-indentation gets lost after either the 5th or 7th generated table, depending on how many even though I have verified stepping through the code that the table style I have added to the Styles collection of the document is the same and that this object hasn't lost its LeftIndent property value.
I'm using Word 2007. I have a Windows Vista computer, but this also happens on Windows XP Professional (so I'm assuming it would on any OS).
I have created a simplified version of the code that demonstrates this (see below). Does anybody know why this happens and what I can do to stop it from happening?
Sub Test()
' Test Macro
Dim objIndentedWithCenteredContentTableStyle As Word.Style
Set objIndentedWithCenteredContentTableStyle = ActiveDocument.Styles.Add(Name:="ch00seLife Table Style 1", Type:=wdStyleTypeTable)
objIndentedWithCenteredContentTableStyle.Table.LeftIndent = 36
Dim intCounter As Integer
intCounter = 0
While intCounter < 15
Dim rng As Range
Set rng = ActiveDocument.Content
rng.Collapse Direction:=wdDirectionEnd
Dim objTable As Word.Table
Set objTable = ActiveDocument.Tables.Add(Range:=rng, NumRows:=2, NumColumns:=3, AutoFitBehavior:=wdAutoFitFixed)
objTable.Style = "ch00seLife Table Style 1"
' If these column-width-setting lines AND the insert-paragraph line near the bottom are both present, then
' the half-inch left indent is lost after the 7th table is written. The number of tables written before
' they stop being indented changes to 5 if I comment out the code to set the third column's width.
' If they are commented out, the indentation is consistently half an inch throughout the generated content.
objTable.Columns(1).Width = InchesToPoints(1)
objTable.Columns(1).Width = InchesToPoints(0.75)
objTable.Columns(1).Width = InchesToPoints(1)
Dim i As Integer
Dim j As Integer
For i = 1 To 2
For j = 1 To 3
objTable.Cell(i, j).Range.Text = "T:" & intCounter & "; C:(" & i & "," & j & ")"
Next j
Next i
Set rng = ActiveDocument.Content
' Here is the paragraph-insertion code that, together with the column-width-setting code above, causes table indentation
' to be lost somewhere in the middle of the generated content.
rng.InsertParagraphAfter
intCounter = intCounter + 1
Wend
End Sub
However, whenever I try to do all of this, the left-indentation gets lost after either the 5th or 7th generated table, depending on how many even though I have verified stepping through the code that the table style I have added to the Styles collection of the document is the same and that this object hasn't lost its LeftIndent property value.
I'm using Word 2007. I have a Windows Vista computer, but this also happens on Windows XP Professional (so I'm assuming it would on any OS).
I have created a simplified version of the code that demonstrates this (see below). Does anybody know why this happens and what I can do to stop it from happening?
Sub Test()
' Test Macro
Dim objIndentedWithCenteredContentTableStyle As Word.Style
Set objIndentedWithCenteredContentTableStyle = ActiveDocument.Styles.Add(Name:="ch00seLife Table Style 1", Type:=wdStyleTypeTable)
objIndentedWithCenteredContentTableStyle.Table.LeftIndent = 36
Dim intCounter As Integer
intCounter = 0
While intCounter < 15
Dim rng As Range
Set rng = ActiveDocument.Content
rng.Collapse Direction:=wdDirectionEnd
Dim objTable As Word.Table
Set objTable = ActiveDocument.Tables.Add(Range:=rng, NumRows:=2, NumColumns:=3, AutoFitBehavior:=wdAutoFitFixed)
objTable.Style = "ch00seLife Table Style 1"
' If these column-width-setting lines AND the insert-paragraph line near the bottom are both present, then
' the half-inch left indent is lost after the 7th table is written. The number of tables written before
' they stop being indented changes to 5 if I comment out the code to set the third column's width.
' If they are commented out, the indentation is consistently half an inch throughout the generated content.
objTable.Columns(1).Width = InchesToPoints(1)
objTable.Columns(1).Width = InchesToPoints(0.75)
objTable.Columns(1).Width = InchesToPoints(1)
Dim i As Integer
Dim j As Integer
For i = 1 To 2
For j = 1 To 3
objTable.Cell(i, j).Range.Text = "T:" & intCounter & "; C:(" & i & "," & j & ")"
Next j
Next i
Set rng = ActiveDocument.Content
' Here is the paragraph-insertion code that, together with the column-width-setting code above, causes table indentation
' to be lost somewhere in the middle of the generated content.
rng.InsertParagraphAfter
intCounter = intCounter + 1
Wend
End Sub