PDA

View Full Version : Generated-Content Tables With Same Custom Style Lose Indentation Mid-Way Through Loop



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

ch00seLife
01-30-2009, 12:51 PM
Note: The 2nd paragraph of my post was meant to have the following text between "depending on how many" and "even though":

"columns for which I set the width"

lucas
01-30-2009, 01:09 PM
Edit your post and fix it.......edit button on the right.

I'm looking at this and can reproduce your problem but I haven't figured out why it is happening yet either.

fumei
02-03-2009, 12:37 PM
1. rng.Collapse Direction:=wdDirectionEnd

should fail the procedure (if you have Option Explicit...ahem), as wdDirectionEnd is NOT a valid defined variable. If fact, it should be the constant wdCollapseEnd.

2. Did you try stepping through the code? If you do, (just holding down the F8 key and letting it rip)...it does NOT do the change. Although for me, the lost indent comes between the 9th and the 10th.

3. objTable.Columns(1).Width = InchesToPoints(1)
objTable.Columns(1).Width = InchesToPoints(0.75)
objTable.Columns(1).Width = InchesToPoints(1)

These change the value of the same column. Is it supposed to be Columns(1), Columns(2), Columns(3)?

4. The following works, still keeping your Columns(1) as you posted. Please note the style is applied after the fact, at the end. Also all variables and object are declared at the start of the procedure - rather than at various places in the procedure. Also j and j are declared as Long, as VBA parses and converts all integers to long.Sub Test()

Dim objTable As Word.Table
Dim intCounter As Long
Dim objIndentedWithCenteredContentTableStyle As Word.Style
Dim rng As Range
Dim i As Long
Dim j As Long

Set objIndentedWithCenteredContentTableStyle = _
ActiveDocument.Styles.Add(Name:="ch00seLife Table Style 1", _
Type:=wdStyleTypeTable)

objIndentedWithCenteredContentTableStyle.Table _
.LeftIndent = 36

While intCounter < 15
Set rng = ActiveDocument.Content
rng.Collapse Direction:=0

Set objTable = ActiveDocument.Tables.Add(Range:=rng, _
NumRows:=2, NumColumns:=3, _
AutoFitBehavior:=wdAutoFitFixed)

' ????????
objTable.Columns(1).Width = InchesToPoints(1)
objTable.Columns(1).Width = InchesToPoints(0.75)
objTable.Columns(1).Width = InchesToPoints(1)

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
rng.InsertParagraphAfter
intCounter = intCounter + 1
Wend

Set objTable = Nothing
For Each objTable In ActiveDocument.Tables
objTable.Style = "ch00seLife Table Style 1"
Next
End Sub



5. Why are you doing this? I mean, creating the style? Why not just have the style?

6. If you need a space between tables - and who doesn't? - I would recommend having....no surprise here...an explicit style to do just that. Mine is called...wait for it...TableSpacer. Using a Normal style paragraph (which is exactly what is used with .InsertParagraphAfter in this case), is OK, but not a good use of Word.

Besides, using an explicit style for the space between tables means you can have different spaces for different situations.

You know that annoying thing of having two tables WITHOUT a paragraph mark? The tables merge? I have

WeeTableSpacer = 1 pt

It is so small that two tables can look like they are butt up against each other, but of course, are not.

TableSpacer (used between tables) can be any size you want.

7. Finally, I would like to point out that your style:

objIndentedWithCenteredContentTableStyle

does NOT do what its name suggests.

The contents are NOT centered.



Oh, and you use the underscore character a bit more, especially for your comments? It really stretches out things for me without it. Thanks.

fumei
02-03-2009, 12:41 PM
BTW: I believe (but am not totally sure) that it acts weird because there is conflict between AutoFit and your declared InchesToPoint.

I am particularly curious as to why stepping through the code does NOT alter things, but if you just run it, it DOES alter things.

Curious.