• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Word document and its Open/Closed status controlled via VB6

This is the code lines that work to open the word document.  
    Set objWordApp = CreateObject("Word.Application")
    Set objWordDoc = CreateObject("Word.Document")
    Set objWordDoc = objWordApp.Documents.Open(strMailMergeFilePath)
    objWordApp.Visible = True

After this lines I have the document opened and user can view it.  I would like to application to do some other stuff when user clicked the word document "closed' - that x - button in the right upper corner.

Is this possible to do?  Can the application see the status of the objWordDoc?

1 Solution
>>> Can the application see the status of the objWordDoc?

In your code trap errors. That should let you know if the user manually closes the word document.

Create a class CWordDocument that can handle the document events :

Option Explicit

Public WithEvents Doc As Word.Document

'You can add properties and methods as you want'

Private Sub Doc_Close()
  ' Do what you want to be done when the doc is closed'
End Sub

Open in new window

Then in your code do :

Dim wordDoc as New CWordDocument
Set wordDoc.Doc = objWordApp.Documents.Open(strMailMergeFilePath)
objWordApp.Visible = True

Open in new window

When the document is closed by the user, Doc_Close in CWordDocument is called.
AquarusAuthor Commented:
Thank you ,

I am almost done here.

Can I programmatically set up the property of the word document so when the user closes it the word application does warn: Save or No or Cancel?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now