-
Access VBA to RESET Code
I'm using Access to open a Word Document using:
HTML Code:
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?
-
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.
-
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.
-
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.
-
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.
-
You can open them manually first, but I am not sure if that is what you mean.
-
I found the following:
HTML Code:
Documents.Open FileName:="C:\MyFiles\MyDoc.doc", ReadOnly:=True
Which seems to be what I was looking for.
-
:thumb
Does it prevent the error?
-
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?
-
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.
-
That is a good idea. Didn't think of that - I'll give it a shot.