在许多列表段落上循环一个Word宏导致内存问题 [英] Looping a Word macro over many list paragraphs causes memory issue

查看:187
本文介绍了在许多列表段落上循环一个Word宏导致内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个相当简单的微软Word的vba宏问题,它旨在解决当我们从Word文档创建PDF版本时出现的列表缩进问题。

该宏基本上循环遍历文档中的每个列表,并且对于与列表关联的每个列表段落,它将设置列表模板的项目符号和文本位置以匹配所应用的在段落级别(代码需要与Word 2000一起使用,所以不使用列表样式)。

当处理大型文档(60多个列表,大约350个列表段落) ,宏第一次运行正常,但第二次中途死亡,这个方法或属性不可用,因为有内存或磁盘问题。

我已经放弃了在循环中取消设置任何对象引用的通常路径,所以我看不到可能保留在内存中的东西。



代码很简单,包含一个单独的过程,目前存储在ThisDocument中:

  Option Explicit 

Sub test2()
Dim i As Integer,n As Integer
Dim curList As List,curPar As Paragraph ,templ As ListTemplate
Dim gapSize As Double,level As Integer
$ b $ Application.ScreenUpdating = False
Application.Options.Pagination = False

对于我= 1 To Lists.Count
Set curList = Lists(i)

For n = 1 To curList.ListParagraphs.Count
Set curPar = curList.ListParagraphs(n)

Set templ = curPar.Range.ListFormat.ListTemplate
level = curPar.Range.ListFormat.ListLevelNumber
gapSize = templ.ListLevels(level).TextPosition - templ.ListLevels(level) .NumberPosition

templ.ListLevels(level).NumberPosition = curPar.LeftIndent - gapSize
templ.ListLevels(level).TextPosition = curPar.LeftIndent
templ.ListLevels(level) .TabPosition = curPar.TabStops.After(curPar.LeftIndent - gapSize).position

Set templ = Nothing
Set curPar = Nothing
Next n

UndoClear
Set CurList = Nothing

Next i

Application.ScreenUpdating = True
Application.Options.Pagination = True

结束Sub


解决方案

解决方案可以解决这个问题,但确实是一个非常糟糕的解决方案。基本上,一旦我们完成了一个宏的运行,关闭并保存文件,并立即重新打开。这样就可以立即或在任何阶段重新运行宏,因为关闭文档似乎最终会正确刷新内存。显然这只能在用户很喜欢保存宏的一部分时使用,但在我的情况下是

I'm hitting problem with a reasonably straightforward vba macro for Microsoft Word which is designed to get around some issues we're seeing with list indentation when we create PDFs versions from the Word doc.

The macro basically loops through each list in the document, and for each list paragraph associated with the list, it is setting the list template's bullet and text position to match what's applied at the paragraph level (code needs to be used with Word 2000 so not using list styles).

When dealing with large documents (60+ lists, ~350 list paragraphs), the macro runs through fine first time, but second time dies half way through with a "This method or property is not available because there is a memory or disk problem".

I've gone down the usual route of unsetting any object references used during the loop, so I can't see what might be holding on to the memory.

The code is quite simple and consists of a single procedure, currently stored in ThisDocument:

Option Explicit

Sub test2()
    Dim i As Integer, n As Integer
    Dim curList As List, curPar As Paragraph, templ As ListTemplate
    Dim gapSize As Double, level As Integer

    Application.ScreenUpdating = False
    Application.Options.Pagination = False

    For i = 1 To Lists.Count
        Set curList = Lists(i)

        For n = 1 To curList.ListParagraphs.Count
            Set curPar = curList.ListParagraphs(n)

            Set templ = curPar.Range.ListFormat.ListTemplate
            level = curPar.Range.ListFormat.ListLevelNumber
            gapSize = templ.ListLevels(level).TextPosition - templ.ListLevels(level).NumberPosition

            templ.ListLevels(level).NumberPosition = curPar.LeftIndent - gapSize
            templ.ListLevels(level).TextPosition = curPar.LeftIndent
            templ.ListLevels(level).TabPosition = curPar.TabStops.After(curPar.LeftIndent - gapSize).position

            Set templ = Nothing
            Set curPar = Nothing
        Next n

        UndoClear
        Set curList = Nothing

    Next i

    Application.ScreenUpdating = True
    Application.Options.Pagination = True

End Sub

解决方案

I've found a nasty, dirty solution that gets around the issue somewhat but is really a very poor fix. Basically, once we have gotten through one complete run of the macro, close and save the document and immediately reopen. This then allows the macro to be rerun immediately or at any stage because closing the document seems to eventually flush the memory properly. Obviously this can only be used if the user is happy to save as part of running the macro, but in my case it is

这篇关于在许多列表段落上循环一个Word宏导致内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆