Run Word macro from Access

W

Wes

First post, please be kind.

Have just dived into the world of VBA, quite facinating I must say. Have
found this board VERY useful. I have searched for answers to my problems and
have always found the answers, but not for this one, even though there are
numerous post concerning this very subject.

So here we go...

Have written a Macro in Word, it works just fine.

Have written VBA code in Access, it works just fine.

Would like to call/run the Word Macro from Access and then continue on with
my Access code, my head hurts from banging it against a brick wall trying to
figure this out.

Here is what I have so far in Access to call/run the Word Macro.



Sub Word_Macro()

Dim myWD As Object
Dim myFile As Object
Set myWD = CreateObject("Word.Application")
Set myFile = myWD.Documents.Open("c:\mypath\config sfs macro.doc")
myWD.Run "SFs_ConfigHeader"
myFile.Close
myWD.Quit
Set myFile = Nothing
Set myWD = Nothing

End Sub



All works well until I get to - myFile.Close-, I am then treated to a very
nasty error box that states "Run-Time error '462': The remote Server Machine
does not exist or is unavailable".

Have gone into Access and made Word objects available ( I think, pretty sure
I did, yeah, I did, I think). Just so yall know.

Some help would be appreciated.

wes
 
J

John Nurick

At a guess the problem is arising because
myfile.Close
is telling Word to close the document, and Word is popping up a
messagebox to ask whether or not you want to save the changes [it
thinks] your macro made. But because the instance of Word is not visible
you aren't seeing and can't respond to the messagebox.

Check in Word VBA help for the Document's Close method: IIRC you use
myFile.Close True
or
myFile.Close False
according to whether you want to save the changes.

If your Word macro uses the Selection object, you may well find that it
doesn't work properly if the document window is not visible. When
automating Word and Excel, it's best to avoid using Selection wherever
possible: instead, work with the specific objects exposed by the object
model (e.g. Ranges and Shapes).

Also, if the macro opens or creates any other documents, they need to be
closed (with or without saving) before you do
myWD.Quit
or you'll get the same trouble.
 
W

Wes via AccessMonster.com

Found it! It was in the Word macro. To be more specific, while trying to get
it running I had added "Application.Quit" to the Word Macro (I had three
instances of Word, according to task manager, at one point). Had to learn how
to open "objects" then close them.

I really need to buy a book or go to a class on VB.

Anyway, I took that line out of the Word Macro, now all works well.

This is a great board, have really learned alot just reading and searching.

Thanks to all those that have posted!

Thanks to John for your reply!

wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top