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
+++++++++++++++++++++++++++
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
+++++++++++++++++++++++++++