Closing Word Document and Window, Leave Other Word Docs Open

andytaylor

New member
Local time
Today, 04:44
Joined
May 3, 2010
Messages
3
SHORT VERSION OF MY QUESTION:

I have a document, titled "DocumentThatOpensThenCloses.doc", that does exactly what I want it to do. It opens and then closes itself, but leaves all other Word documents open. The code is as follows:

Private Sub Document_Open()
ActiveDocument.Close
End Sub

Private Sub Document_Close()
ActiveWindow.Close
End Sub

However, if I create a command button on a form in Access that opens the document, it will open the document, and then close the document, and leave any other Word documents open that were already open. The problem is that it leaves a separate instance of Word open. Unfortunately, Application.Quit is not an option for me because, when I eventually implement this, I will have to have other Word documents open.

The code in Access that I'm using to open my document is as follows:

Private Sub Command4_Click()
On Error GoTo Err_Command4_Click

Dim oApp As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Exit_Command4_Click:
Exit Sub

Err_Command4_Click:
MsgBox Err.Description
Resume Exit_Command4_Click

(Incidentally, I didn't write this code, it's just the code that was created from the Command Button wizard). Under properties for this command button, I set "DocumentThatOpensThenCloses.doc" as the hyperlink.

A LITTLE MORE EXPLANATION:

I have a series of documents that I am mail-merging from access. Basically, I have set up several Word documents as mail merge documents, and then I have command buttons in Access that open the documents. Inside those documents, I have a macro that executes the mail merge, and creates a new document with the data in it. My goal is to then close the original mail merge document, which I am able to do, but the problem is that every time I create a new mail merge, I am left with another empty Word window with no document in it.

I am using Access 2003 SP3 and Word 2003 SP3.
 
Sounds like your problem is that you have some code which does not tie back to oApp and therefore Access opens another instance of Word to handle the code. This is a common mistake with people who do Excel programming from Access as well.

What is the entire code that does your merge? Perhaps we can spot the location which needs to be tied to oApp.
 
Thanks for your reply. I was a comp. sci. undergrad, but haven't programmed in awhile, so if there's a common mistake to be made, I will definitely make it.

I have a document where I'm playing around with creating a merge, but I'm having this problem even with this test document, which isn't a merge document. The command button in Access is just a command to open the document, and the document is just a plain document without even any text, just the lines of VBA code that close the document on open, and then close the active window on close. For the test document, the only code I have is what I've posted.

I think you're probably on to something. I guess the problem is, I don't know how (or if it's even possible) to close oApp from within the Word document. My knowledge of VBA is definitely still rusty.
 
If you are going to open it from Access but have Word close itself then you do NOT want to instantiate an object. Just open it from a hyperlink:

FollowHyperlink strPathAndFileNameHere
 

Users who are viewing this thread

Back
Top Bottom