I'm debugging an application at the moment where a colleague of mine created a method that iterates through all record sets and turns them into tables in a .docx document.
Something goes horribly wrong when creating a document for a large dataset, though.
Here's the code:
Code:
With objWord.Selection
.Find.Execute
While .Find.Found
'Set the size and font for the entire selection
.Font.Name = "Calibri"
.Font.Size = 10
rsPDA.MoveFirst
While Not rsPDA.EOF
'Header above a series of tables
If sLeefdomein <> rsPDA.value("Leefdomein") Then
sLeefdomein = rsPDA.value("Leefdomein")
.Font.Size = 12
.Font.Bold = True
.TypeText Text:=vbNewLine + sLeefdomein
.Font.Size = 10
.Font.Bold = False
End If
'Create new table
Set t = objWord.ActiveDocument.Tables.Add(Range:=objWord.Selection.Range, _
NumRows:=5, _
NumColumns:=6, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)
'Define columns
t.Cell(1, 2).Merge MergeTo:=t.Cell(1, 6)
t.Cell(2, 2).Merge MergeTo:=t.Cell(2, 6)
t.Cell(3, 2).Merge MergeTo:=t.Cell(3, 6)
t.Cell(4, 2).Merge MergeTo:=t.Cell(4, 6)
'Insert data
t.Cell(1, 1).Range.Text = "Probleem": t.Cell(1, 1).Range.Font.Italic = True
t.Cell(1, 2).Range.Text = rsPDA.value("Probleem")
t.Cell(2, 1).Range.Text = "Doel": t.Cell(2, 1).Range.Font.Italic = True
t.Cell(2, 2).Range.Text = rsPDA.value("Doel")
t.Cell(3, 1).Range.Text = "Actie": t.Cell(3, 1).Range.Font.Italic = True
t.Cell(3, 2).Range.Text = rsPDA.value("Actie")
t.Cell(4, 1).Range.Text = "Uitvoering": t.Cell(4, 1).Range.Font.Italic = True
t.Cell(4, 2).Range.Text = rsPDA.value("Uitvoering")
t.Cell(5, 1).Range.Text = "Prob. eval. ": t.Cell(5, 1).Range.Font.Italic = True
t.Cell(5, 2).Range.Text = rsPDA.value("DatumProbleemEvaluatie")
t.Cell(5, 3).Range.Text = "Actie evaluatie ": t.Cell(5, 3).Range.Font.Italic = True
t.Cell(5, 4).Range.Text = rsPDA.value("DatumAktieEvaluatie")
t.Cell(5, 5).Range.Text = "Tijd ": t.Cell(5, 5).Range.Font.Italic = True
t.Cell(5, 6).Range.Text = rsPDA.value("Tijd")
Set t = Nothing 'Destroy table reference.
'Set cursor for next while loop
.MoveDown Unit:=wdLine, Count:=5
.TypeText Text:=vbNewLine
Sleep 100 'Even 1000 doesn't change anything.
rsPDA.MoveNext
Wend
.Find.Execute
Wend
The generated document looks like this:
All goes well until page 7 or 8 (no matter what's in the dataset).
I was thinking maybe the WordObjects Tables.Add method is working async behind the scenes and
this code pumps the next table into it's rendering engine too quickly? But I'm not sure because a sleep on 100, 250 and 1000 didn't event change the position where the tables started to nest.
Any help or tips would be appreciated.