|
-
Feb 3rd, 2023, 09:39 AM
#1
Thread Starter
Junior Member
Word automation started giving errors - Now have to close and re-create Word object
Hi,
I have a VB6 program that performs Word automation. It processes a batch of invoices. Each invoice's details are populated into an existing Word document using Word find & replace commands. The document is then printed to a PDF and emailed. Usually the batch will be between 15 and 40 invoices.
i.e.
create word object
For each invoice
- open word document
populate word document using find & replace
print word document
email word document
close word document
Next
close word object (MSWord.quit : Set MSWord = Nothing)
My VB code to automate Word is along the lines of:
Dim MSWord as object
Set MSWord = CreateObject("word.application","localhost")
' To Open Document
With MSWord
.Documents.Open Filename:=FileToOpen, etc
End With
' To Close Doucment
With MSWord
.ActiveDocument.Close SaveChanges:=0
End With
' To Print Document
With MSWord
.ActiveDocument.PrintOut FileName:="", etc
End With
' To Find & Replace
With MSWord
.Selection.Find.ClearFormatting
.Selection.Find.Replacement.ClearFormatting
With .Selection.Find
.Text = FindText
.Replacement.Text = ReplaceText
.Forward = True
.Wrap = 1 'wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Selection.Find.Execute replace:=1 'wdReplaceOne
End With
This program has worked for years without any problems. But now when I run it, it gives me errors when trying to execute the Word commands, but only when its processing say the 12th+ invoice. If I run the program again it will fail on a random invoice but usually on the 12th or more; It could be the 18th or 20th. It can fail on the openening of the Word document, the find & replace, or the print.
Sometimes the error is 5097 which I believe is a Word memory error. But my system has 16gb and no other apps running.
Running the program for a batch of 10 or less invoices seems to work ok. So this suggest maybe it is indeed a memory problem? But I cannot fathom why this has just started when it has worked fine for so long.
For testing purposes I modified my program to not print/generate a PDF and to not email. So its just opening, find and replacing and closing, and it still gives me errors. I also modified it to not find and replace, so it just opened and closed documents. That weirdly resulted in Word putting my document into its list of disable items.
*** RESOLVED (Kind of)
I finally ended up closing the Word object after each document had been printed and emailed, and then re-creating the Word object before opening the next document in the batch. This has cured the problem but I still can't understand why the program started giving me errors after working fine for so many years.
***
Last edited by nealb; Feb 3rd, 2023 at 11:27 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|