Runtime Error 462 - VBA Not Releasing Word (1 Viewer)

abbaddon223

Registered User.
Local time
Today, 03:24
Joined
Mar 13, 2010
Messages
162
Hi,

I'm hoping someone can help me please?

I have a word doc (2010) which is connected to an access DB (2010) for a mail merge. The mail merge has already been done from word to access, then I'm just using a vba call from access to open the word doc.

When I run the command the first time round, it opens, prints and closes just fine.

If I run it again, I get the following error: "Run time error 462. The remote machine does not exist or is unavailable" and it debugs at the following section of the code: "Set myMerge = ActiveDocument.MailMerge"

From looking about apparently the below code is not "Releasing" the word document properly, so when it goes to run again, it errors. That said, if that is the issue, I don't know how to modify the below. Can anyone help me please? It would be really appreciated!!


Dim LWordDoc As String
Dim oApp As Object

'Path to the word document
LWordDoc = "D:\Remote Applications\AIA Invoice Management\Mail_Merges\Bulk Merges\8 - 14 Days Chase Letter.docx"

If Dir(LWordDoc) = "" Then
MsgBox "Document not found."

Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True

'Open the Document
oApp.Documents.Open FileName:=LWordDoc

Set myMerge = ActiveDocument.MailMerge
If myMerge.State = wdMainAndSourceAndHeader Or _
myMerge.State = wdMainAndDataSource Then
With myMerge.DataSource
.FirstRecord = 1
.LastRecord = 100000
End With
End If

With myMerge
.Destination = wdSendToPrinter
.Execute
End With

oApp.ActiveDocument.Close
oApp.Quit

End If
 

pwbrown

Registered User.
Local time
Today, 11:24
Joined
Oct 1, 2012
Messages
170
Try adding oApp = nothing after oApp.Quit.
If that doesn't work try :
Code:
oApp.Application.Quit
oApp = nothing
instead of
oApp.ActiveDocument.Close
oApp.Quit
 

abbaddon223

Registered User.
Local time
Today, 03:24
Joined
Mar 13, 2010
Messages
162
Hi,

Thanks for your help. The word doc now asks me if I want to save changes (which I'd need to stop as there are a few merges to run, one after another).

When I manually click "Dont Save" - I get an access error: "Runtime Error 5861 'Name' is Read Only Property"

This was for both lines of code you very kindly supplied.

Thank you for trying to help me!!!
 

pr2-eugin

Super Moderator
Local time
Today, 11:24
Joined
Nov 30, 2011
Messages
8,494
Hello abbaddon223, Try setting the objects to nothing towards the end of the Sub, and see if it releases the object.. Like..
Code:
Set oApp = Nothing
Set myMerge = Nothing
Also, I could not see the declaration of myMerge object.. So Dim the object as well.. See what happens..

EDIT: A bit slow there.. :D
 

abbaddon223

Registered User.
Local time
Today, 03:24
Joined
Mar 13, 2010
Messages
162
Hi pr2-eugin - thanks for the help. Yes that lets me cycle through the mail merges without the error. I do have a new promblem though (scream!!)

As I'm no longer closing the docs after printing (it errors if I try) - the second batch of code (same as the first but pointing to a different word doc) prints the first one again. I assume it's because I'm not closing them after the last line of code?
 

pr2-eugin

Super Moderator
Local time
Today, 11:24
Joined
Nov 30, 2011
Messages
8,494
A manual close/a programmatic close has to be performed.. I normally have a manual close because it gives them a chance to review docs before sending it to the printer.. So do not delete the lines of code for closing the word app.. so it should be..
Code:
oApp.ActiveDocument.Close
oApp.Quit
Set oApp = Nothing
Set myMerge = Nothing
 

abbaddon223

Registered User.
Local time
Today, 03:24
Joined
Mar 13, 2010
Messages
162
Hi,

Quitting to doc as suggested put's us back in the "Machine Not Available" error code.

Really doesn't want to release this doc :banghead:

thanks again
 

Users who are viewing this thread

Top Bottom