0

I have the following VBA code

Private Sub CreateQuery_Click()

  Dim doc As Document
  Dim i As Integer

  Set doc = ActiveDocument
  i = doc.Paragraphs.Count

  doc.Paragraphs(i).Range.InsertParagraphAfter
  i = i + 1

  For j = 0 To 1000
      doc.Paragraphs(i).Range.InsertParagraphAfter
      i = i + 1
      doc.Paragraphs(i).Range.InsertParagraphAfter
      i = i + 1
      With doc.Paragraphs(i)
          .Range.Font.Italic = True
          .Range.ListFormat.ApplyBulletDefault
          .Indent
          .Indent
          .TabStops.Add Position:=CentimetersToPoints(3.14)
          .TabStops.Add Position:=CentimetersToPoints(10)
          .TabStops.Add Position:=CentimetersToPoints(11)
      End With
      For k = 0 To 10
          With doc.Paragraphs(i)
              .Range.InsertAfter "testState" & vbTab & CStr(doc.Paragraphs(i).Range.ListFormat.CountNumberedItems) & vbTab & CStr(doc.Paragraphs.Count)
              .Range.InsertParagraphAfter
          End With
          i = i + 1
      Next
      i = doc.Paragraphs.Count
      With doc.Paragraphs(i)
          .Range.ListFormat.ApplyBulletDefault
          .TabStops.ClearAll
          .Outdent
          .Outdent
      End With
  Next

  i = doc.Paragraphs.Count

  doc.Paragraphs(i).Range.InsertParagraphAfter
  i = i + 1

End Sub

Basically this code just prints n numbers of lines with the specific format.

  • Bullet list
  • Indented
  • and TabStops

alt text
(source: lans-msp.de)

The Code works perfectly for an arbitrary number of lines, but then at some point Word just stops applying the TabStops.

I know that if I wouldn't reset the format every 10 lines, the code would work seemingly forever (really?!?). But the every 10 line brake is a must.

The exact line number where everything breaks down is dependent on the amount of RAM. On my work computer with 1GB it only works until about line 800(as you can see). My computer at home with 4GB didn't show this behaviour. But I'm sure it would have shown it as well if I had it let run long enough, because in my production code(which is a bit more complex) my home computer shows the problem as well.

Is this some kind of memory leak or something? What did I do wrong? Is maybe, god-forbid, VBA itself the culprit here?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
eric
  • 1,857
  • 5
  • 23
  • 33

2 Answers2

3

Try to apply the formatting using a defined style. See if that makes a difference.

codeape
  • 97,830
  • 24
  • 159
  • 188
  • I'm also wondering why on earth anyone would want to format *every single line* directly when their format does not differ at all. – Tomalak Jul 22 '09 at 09:08
  • It is not every line. I just reapply the format every block of 10 lines. In the real script way more stuff is going on between the blocks. – eric Jul 22 '09 at 09:15
  • This solved the problem for me. I can see that this is the cleaner way, so thats great. But I still think Word has some kind of memory leak here. Maybe this 'fix' just pushed the boundaries up a bit... Thanks a lot anyway, my problem is gone and thats the most important thing! – eric Jul 28 '09 at 09:42
0

You might try turning automatic pagination off while adding the lines, to see if that helps.

Application.Options.Pagination = False
Gary McGill
  • 26,400
  • 25
  • 118
  • 202
  • No unfortunately this doesn't work, but I do get one extra correctly formatted block. After that it still breaks – eric Jul 22 '09 at 09:10