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

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

Re: Memory not freed after closing Word Documents by Jezebel

Jezebel
Sun Aug 29 21:58:57 CDT 2004

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







"Chris Wright" <anonymous@discussions.microsoft.com> wrote in message
news:039401c48e3a$57a64ba0$a401280a@phx.gbl...
> 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
>
> +++++++++++++++++++++++++++



Memory not freed after closing Word Documents by DA

DA
Mon Aug 30 00:51:21 CDT 2004

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.

>-----Original Message-----
>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
>
>+++++++++++++++++++++++++++
>.
>