错误4605“此方法或属性不可用,因为存在内存或磁盘错误”运行宏

问题描述 投票:0回答:1

这是MS-Word宏。它会产生以下错误:

错误4605“此方法或属性不可用,因为存在内存或磁盘问题

' Word script to remove all unused styles in the document
Sub DeleteUnusedStyles()
Dim Doc As Document, bDel As Boolean
Dim Rng As Range, StlNm As String, i As Long
Application.ScreenUpdating = False
Set Doc = ActiveDocument
With Doc
  For i = .Styles.Count To 1 Step -1
    With .Styles(i)
      If .BuiltIn = False Then
        bDel = True: StlNm = .NameLocal
        For Each Rng In Doc.StoryRanges
          With Rng
            With .Find
              .ClearFormatting
              .Format = True
              .Style = StlNm
              .Execute
            End With
            If .Find.Found = True Then
              bDel = False
              Exit For
            End If
          End With
        Next
        If bDel = True Then .Delete
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub

它应该从word文档中删除所有未使用的样式。我在互联网上找到并测试了其他3个应该做同样事情的宏,但最后会出现同样的错误。 Word文档暂停约5分钟(这是100页文档格式严重),然后它会吐出这个错误。在错误出现之前,我可以看到单词进程的内存消耗增加了近5倍。我仍然有足够的RAM。我在Windows 8.1 x86_64上运行正版Word 2013。为什么会发生这种情况?如何解决错误?

vba ms-word ms-office word-2013
1个回答
2
投票

在这种情况下,显然Undo堆栈会堵塞。

简单修复:修改代码如下:

        '...
        '...
        Next
        If bDel = True Then .Delete
        Doc.UndoClear
    End If
End With
© www.soinside.com 2019 - 2024. All rights reserved.