Hi, I've written a VBA program to open and close a large number of word documents, saving as RTF and removing the password. The problem is that when a word document is closed, the memory doesn't seem to get freed up, so after processing about 2000 documents, I start getting warning messages about system/virtual memory.
Does anyone know how to free the memory after closing the word document?
Here's the code, any help is gratefully received. :-
=============================
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 source and destination dir. Get Password.
from_Path = txt_From_Path.Text
to_Path = txt_To_Path.Text
passwd = txt_passwd.Text
' Open log files.
Open to_Path & "convert.log" For Output As #1
Open to_Path & "filenames.dat" For Output As #2
' Get list of files ot process.
inFile = Dir$(from_Path & "*.doc")
'Write messae to log.
Print #1, "Reading *.doc from " & from_Path
Print #1, "Writing *.rtf to " & to_Path
'loop through documents.
Do While inFile <> ""
outFile = Left(inFile, Len(inFile) - 3) & "rtf"
ChangeFileOpenDirectory from_Path
' Open file
Documents.Open fileName:=inFile, ConfirmConversions:= _
False, ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:=passwd _
, PasswordTemplate:="", Revert:=False, WritePasswordDocument:=passwd, _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
'move to destination directory
ChangeFileOpenDirectory to_Path
'Save file as rtf.
ActiveDocument.SaveAs fileName:=outFile, FileFormat:= _
wdFormatRTF, LockComments:=False, Password:="", AddToRecentFiles:= _
False, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False
'close document.
ActiveDocument.Close
'Documents.Close
'Log conversion.
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
=============================
Does anyone know how to free the memory after closing the word document?
Here's the code, any help is gratefully received. :-
=============================
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 source and destination dir. Get Password.
from_Path = txt_From_Path.Text
to_Path = txt_To_Path.Text
passwd = txt_passwd.Text
' Open log files.
Open to_Path & "convert.log" For Output As #1
Open to_Path & "filenames.dat" For Output As #2
' Get list of files ot process.
inFile = Dir$(from_Path & "*.doc")
'Write messae to log.
Print #1, "Reading *.doc from " & from_Path
Print #1, "Writing *.rtf to " & to_Path
'loop through documents.
Do While inFile <> ""
outFile = Left(inFile, Len(inFile) - 3) & "rtf"
ChangeFileOpenDirectory from_Path
' Open file
Documents.Open fileName:=inFile, ConfirmConversions:= _
False, ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:=passwd _
, PasswordTemplate:="", Revert:=False, WritePasswordDocument:=passwd, _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
'move to destination directory
ChangeFileOpenDirectory to_Path
'Save file as rtf.
ActiveDocument.SaveAs fileName:=outFile, FileFormat:= _
wdFormatRTF, LockComments:=False, Password:="", AddToRecentFiles:= _
False, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False
'close document.
ActiveDocument.Close
'Documents.Close
'Log conversion.
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
=============================