Memory not freed after closing Word Documents

C

Chris Wright

Hi,
I've written a process using VBA to convert a bunch of
word documents into RTF files. I'm not using the Batch
Conversion wizard that comes with MS Word because the docs
are password protected. I ran a test run and all worked
well, except for one problem. After closing each
document, the memory doesn't seem to be freed up, and
gradually the used memory ramps up till the system
chokes. This started to happen processing 2000 documents
and I have about 100,000 documents to process. My code is
attached below, any help on how I can free up the memory
would be appreciated, failing that I'm going to try to
convert the program to full VB so I can totally close down
the Word Application to free up the memory without
interrupting the processing.

++++++++++++++++++++++++++++++++++++++++++

Private Sub btn_Convert_Click()
On Error GoTo PROC_ERR

Dim inFile
Dim outFile
Dim from_Path
Dim to_Path
Dim passwd

Application.WindowState = wdWindowStateMinimize

'get files from the selected path
'and insert them into the doc

from_Path = txt_From_Path.Text
to_Path = txt_To_Path.Text
passwd = txt_passwd.Text


Open to_Path & "convert.log" For Output As #1
Open to_Path & "filenames.dat" For Output As #2

inFile = Dir$(from_Path & "*.doc")

Print #1, "Reading *.doc from " & from_Path
Print #1, "Writing *.rtf to " & to_Path


Do While inFile <> ""

outFile = Left(inFile, Len(inFile) - 3) & "rtf"

ChangeFileOpenDirectory from_Path

Documents.Open fileName:=inFile,
ConfirmConversions:= _
False, ReadOnly:=True,
AddToRecentFiles:=False, PasswordDocument:=passwd _
, PasswordTemplate:="", Revert:=False,
WritePasswordDocument:=passwd, _
WritePasswordTemplate:="",
Format:=wdOpenFormatAuto

ChangeFileOpenDirectory to_Path

ActiveDocument.SaveAs fileName:=outFile,
FileFormat:= _
wdFormatRTF, LockComments:=False,
Password:="", AddToRecentFiles:= _
True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False,
SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False

Documents.Close
'Also tried activedocument.close

Print #1, "Time: " & Format$(Now(), "dd-mmm-yyyy
hh:mm:ss AM/PM") & " File " & inFile & " converted to " &
outFile & " successfully."
Print #2, outFile
inFile = Dir
Loop


PROC_EXIT:
On Error Resume Next
Close #1
Close #2
Unload Me

Exit Sub
PROC_ERR:
lError = Err.Number
sError = Err.Description
If lError = 5180 Then
Resume
Else
Print #1, "!!!!! File " & inFile & " LOAD
FAILED !!!!!!"
Print #1, "Error: " & lError & " - " & sError
GoTo PROC_EXIT
End If
End Sub

+++++++++++++++++++++++++++
 
J

Jezebel

I doubt that this will solve your problem, but the code would be cleaner
this way:

Dim pDoc as Word.Document

:

Set pDoc = Documents.Open( .... )
pDoc.SaveAs ....
pDoc.Close
set pDoc = Nothing
 
D

DA

Hi Chris

Not that this is going to help you much either, but I
tested your code with about 50 docs on Word'03 and memory
was freed each time the Documents.Close was called.

Could be a memory leak in older versions which has been
fixed. Best thing I can suggest is to make sure you've
got the latest service packs.

Dennis.
 
M

Malcolm Smith

For what it's worth I would have to go along with Jezebel's suggestion
here of using pointers, for example pDoc, to point to the document being
worked on.

Now, if that doesn't work then an alternative is to write this in Visual
Basic which then opens a Word application and then convert 100 documents
at time, then closes Word and then continues.

I have seen many document conversion applications when my clients have
changed from one format to another and they all have used various external
applications.

To convert from Word VBA to VB all one needs to do is to define a
Word.Application object which is only one level up the Object Hierarchy.

Hope that this helps a little
- Malc
www.dragondrop.com
 
J

Jezebel

There's inevitably some memory leakage in this sort of exercise, so working
in batches as you suggest is certainly going to be required. I would have
thought you could get away with larger batch sizes than 100; but no way
you'll get through 100,000 in one hit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top