当 27 页文档上的 运行 宏时,Word 内存不足
Word runs out of memory when running macro on 27 page document
这个 post 在 Microsoft Answers forum 上交叉post。
我下面的宏用于保护有问题的word文档。当它在较小的文档(4-10 页)上为 运行 时,它工作正常,但我们有一个更大的 27 页文档(6000 字)似乎占用了所有内存!我 运行 它和单词最终冻结在我身上。
这是一个 link 到 OneDrive 的可重现示例:https://1drv.ms/w/s!AgPO3BotYSt7iHvafHts2HyF2OjB?e=HSOI57
不确定通过 OneDrive 访问时格式是否保持不变,但复选框在单击时显示 X。
'Description of how you will meet the recommendation' 后跟一个文本字段,以便用户可以输入文本。 'Responsible team' 和 'Reasoning for why you disagree:'
处理相同
在宏 运行s 之后,除了红色文本以及上述格式外,整个文档都应该被锁定。
有没有办法调整宏以节省一些内存,使其在用于较大文件时 运行?
这是宏:
Sub Lock_Teammate_DraftReports_mp()
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Font.ColorIndex = wdRed
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, MatchWildcards:=False, Wrap:=wdFindStop) = True
Selection.Editors.Add wdEditorEveryone
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Protect Password:="example123", NoReset:=False, Type:=wdAllowOnlyReading,
UseIRM:=False, EnforceStyleLock:=False
End Sub
您的问题很可能与您的代码所做的所有选择有关,这既低效又容易导致大量滚动和屏幕闪烁。尝试:
Sub Lock_Teammate_DraftReports_mp()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.Font.ColorIndex = wdRed
.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
End With
Do While .Find.Execute = True
i = i + 1
If .Information(wdWithInTable) = True Then
If .Rows(1).Range.Font.ColorIndex = wdRed Then .End = .Rows(1).Range.End
If .End = .Cells(1).Range.End - 1 Then .End = .Cells(1).Range.End
If .Information(wdAtEndOfRowMarker) = True Then .End = .End + 1
End If
.Editors.Add wdEditorEveryone
If .End = ActiveDocument.Range.End Then Exit Do
.Collapse wdCollapseEnd
If i Mod 100 = 0 Then DoEvents
Loop
End With
.Protect Password:="example123", NoReset:=False, Type:=wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
End With
Application.ScreenUpdating = True
End Sub
这个 post 在 Microsoft Answers forum 上交叉post。
我下面的宏用于保护有问题的word文档。当它在较小的文档(4-10 页)上为 运行 时,它工作正常,但我们有一个更大的 27 页文档(6000 字)似乎占用了所有内存!我 运行 它和单词最终冻结在我身上。
这是一个 link 到 OneDrive 的可重现示例:https://1drv.ms/w/s!AgPO3BotYSt7iHvafHts2HyF2OjB?e=HSOI57
不确定通过 OneDrive 访问时格式是否保持不变,但复选框在单击时显示 X。
'Description of how you will meet the recommendation' 后跟一个文本字段,以便用户可以输入文本。 'Responsible team' 和 'Reasoning for why you disagree:'
处理相同在宏 运行s 之后,除了红色文本以及上述格式外,整个文档都应该被锁定。
有没有办法调整宏以节省一些内存,使其在用于较大文件时 运行?
这是宏:
Sub Lock_Teammate_DraftReports_mp()
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Font.ColorIndex = wdRed
With Selection.Find
Do While .Execute(FindText:="", Forward:=True, MatchWildcards:=False, Wrap:=wdFindStop) = True
Selection.Editors.Add wdEditorEveryone
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Protect Password:="example123", NoReset:=False, Type:=wdAllowOnlyReading,
UseIRM:=False, EnforceStyleLock:=False
End Sub
您的问题很可能与您的代码所做的所有选择有关,这既低效又容易导致大量滚动和屏幕闪烁。尝试:
Sub Lock_Teammate_DraftReports_mp()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.Font.ColorIndex = wdRed
.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
End With
Do While .Find.Execute = True
i = i + 1
If .Information(wdWithInTable) = True Then
If .Rows(1).Range.Font.ColorIndex = wdRed Then .End = .Rows(1).Range.End
If .End = .Cells(1).Range.End - 1 Then .End = .Cells(1).Range.End
If .Information(wdAtEndOfRowMarker) = True Then .End = .End + 1
End If
.Editors.Add wdEditorEveryone
If .End = ActiveDocument.Range.End Then Exit Do
.Collapse wdCollapseEnd
If i Mod 100 = 0 Then DoEvents
Loop
End With
.Protect Password:="example123", NoReset:=False, Type:=wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
End With
Application.ScreenUpdating = True
End Sub