Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Application.Options.Pagination = False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top