运行宏时"this method or property is not available because there is a memory or disk error"错误 4605



这是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次的内存消耗。不过,我还有足够多的公羊。我在Windows 8.1 X86_64上运行Authort Word 2013。为什么会发生这种情况,我该如何解决错误?

在这种情况下,显然是撤消堆栈堵塞。

简单修复:修改这样的代码:

        '...
        '...
        Next
        If bDel = True Then .Delete
        Doc.UndoClear
    End If
End With

从文件/选项/自定义ribon启用开发人员菜单。

然后是PRES ALT-F11,并将此代码粘贴在其中。按F5。

Sub Removedenter code hereNonDefaultStyles()
Dim CurrentStyleInLoop As Style
   For Each CurrentStyleInLoop In ActiveDocument.Styles
If Not CurrentStyleInLoop.BuiltIn Then
     CurrentStyleInLoop.Delete
End If
Next CurrentStyleInLoop
End Sub

相关内容