【问题标题】:Word VBA Tabstop wrong behaviourWord VBA Tabstop 错误行为
【发布时间】:2019-03-31 10:00:28
【问题描述】:

我有以下 VBA 代码

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

基本上,这段代码只打印 n 行特定格式的行。

  • 项目符号列表
  • 缩进
  • 和 TabStops


(来源:lans-msp.de

该代码适用于任意数量的行,但在某些时候 Word 只是停止应用 TabStops。

我知道,如果我不每 10 行重新设置一次格式,代码似乎会永远工作(真的吗?!?)。但是每 10 线刹车是必须的。

所有内容都发生故障的确切行号取决于 RAM 的数量。在我的 1GB 工作计算机上,它只能工作到大约 800 行(如您所见)。我家 4GB 的电脑没有出现这种行为。但我敢肯定,如果我让它运行足够长的时间,它也会显示出来,因为在我的生产代码(这有点复杂)中,我的家用电脑也显示了这个问题。

这是某种内存泄漏还是什么?我做错了什么?也许,上帝保佑,VBA 本身就是这里的罪魁祸首吗?

【问题讨论】:

    标签: vba memory ms-word tabstop


    【解决方案1】:

    尝试使用定义的样式应用格式。看看会不会有什么不同。

    【讨论】:

    • 我还想知道为什么在格式完全没有区别的情况下,究竟为什么会有人想要直接格式化每一行
    • 不是每一行。我只是在每 10 行的块中重新应用格式。在真正的脚本方式中,块之间会发生更多的事情。
    • 这解决了我的问题。我可以看到这是更清洁的方式,所以这很棒。但我仍然认为 Word 在这里存在某种内存泄漏。也许这个“修复”只是将界限推高了一点......无论如何,非常感谢,我的问题已经解决了,这是最重要的!
    【解决方案2】:

    您可以尝试在添加行时关闭自动分页,看看是否有帮助。

    Application.Options.Pagination = False
    

    【讨论】:

    • 不,不幸的是,这不起作用,但我确实得到了一个额外的格式正确的块。之后它仍然会破裂
    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多