PDA

View Full Version : Access VBA to RESET Code



heedaf
05-24-2019, 05:49 PM
I'm using Access to open a Word Document using:

Set WdDoc = AppWd.Documents.open(....

to open a word document which works fine on the first time it is opened but if the document is closed and the code is re-ran it gives the following error:

"Run-time error '462'
"The remote server machine does not exist or is unavailable"

I believe the problem is that Access still thinks Word is open because the object still exists.

I've tried wdDoc.Close and Set AppWd to nothing but that causes problems trying to run it again.

What does work is to catch the error and when the debug window pops up click "end" or "reset" the code run. Any ideas on how to fix this problem?

OBP
05-25-2019, 01:40 AM
This is the code that I use to finish the VBA after sending the data to word,

m_objDoc.SaveAs Filename:=newname
m_objDoc.Close
m_objWord.Quit
' clean up
Set m_objDoc = Nothing
Set m_objWord = Nothing

Note it closes the word doc and quits word before setting the variables to nothing, you could try that for your variables.

heedaf
05-28-2019, 03:42 PM
Thank you for the reply. Is there away of ending the script and keeping the Word document open? The user most likely will need to use the database while referring to the document.

OBP
05-29-2019, 06:35 AM
Well it would appear that it is keeping the word doc open that is causing your problem, so I am not sure how to resolve that.
I suppose that you could close it all down and then try re-opening just for viewing purposes.
I would try the closing it all down first to see if it resolves your original problem.

heedaf
05-29-2019, 07:50 AM
I guess what I really want to do is open a Word document without creating an object. Or to be able to close the object without closing the document.

OBP
05-29-2019, 09:22 AM
You can open them manually first, but I am not sure if that is what you mean.

heedaf
05-29-2019, 10:06 AM
I found the following:


Documents.Open FileName:="C:\MyFiles\MyDoc.doc", ReadOnly:=True

Which seems to be what I was looking for.

OBP
05-29-2019, 11:48 AM
:thumb
Does it prevent the error?

heedaf
05-29-2019, 03:33 PM
It does. Since I'm not using an object it isn't an issue. You are right about using .quit which it might be the only way to prevent the error.

This error has been a pain for me for a very long time. Users sometimes will close Word (from Word) which doesn't give the Access code the chance to kill the object. Is there a way that I could have code running in the background and if it detects that Word was closed that it will then kill the object that was created?

OBP
05-29-2019, 03:38 PM
You could have a form open in invisible mode and set it's timer to test if there is a doc open and if it is not close & quit the object.

heedaf
05-29-2019, 03:42 PM
That is a good idea. Didn't think of that - I'll give it a shot.