Microsoft KB Archive/94901

{|
 * width="100%"|

WD: Out-of-Memory Error with Macro That Edits Document

 * }

Q94901

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, version 7.0
 * Microsoft Word for the Macintosh, versions 6.0, 6.0.1

-

SYMPTOMS
In Microsoft Word for Windows, it is possible to receive an out of memory error while running a macro that performs a large number of edits to a document.

CAUSE
This out of memory error may be an indication that there are too many edits to the document, rather than an insufficient amount of memory.

RESOLUTION
To eliminate this error, add a FileSave command to your macro so that the document is saved periodically.

When using a macro to search and replace text in a document, the FileSave command is often needed after a number of replacements have been made. The FileSave command clears the Word edit buffer so that the macro can continue making edits.

Below is a sample macro that replaces all semicolons with tabs in a document. The If statement and count variable are used to save the document after every 30 replacements.

Word 6.0
  Sub MAIN ToolsOptionsSave .FastSaves = 0 StartOfDocument count = 0 EditFindClearFormatting EditFind .Find = &quot;;&quot;, .Direction = 0 While EditFindFound count = count + 1 EditClear Insert Chr$(9) If count = 30 Then FileSave count = 0 End If        RepeatFind Wend End Sub

Word 2.x
 Sub MAIN ToolsOptionsSave .FastSaves = 0 StartOfDocument count = 0 EditFindClearFormatting EditFind .Find = &quot;;&quot;, .Direction = 2 While EditFindFound count = count + 1 EditClear Insert Chr$(9) If count = 30 Then FileSave count = 0 End If     EditFind .Find = &quot;;&quot;,  .Direction = 2 Wend End Sub WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.