Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word - How to detect close event?

Status
Not open for further replies.

bradmaunsell

Programmer
May 8, 2001
156
US
How can I detect a close event in MS Word document that is opened from MS Access?

I have an MS Access form displaying a name and address (Northwind) having a button to open a new MS Word doc and head it up with a name and address displayed on the form.

Once the doc is opened with the header info inserted, I want the user to be able type whatever they want in the Word doc. Meanwhile, Access is still open, and when the operator exits the Word doc, I want the Access code to close the word object.

I think I need to detetct the MS Word CLOSE event and run
oWord.Quit
Set oWord = Nothing

Can anybody bail me out on this one?

Thanks,
Brad


See enclosed code.


Private Sub cmdWord_Click()
Dim oWord As Word.Application
Dim oRange As Range
Const TemplatePath = "C:\WINDOWS\Profiles\Brad\My Documents\Access\Samples\Access_2000\NorthwindWordTemplate.dot"
Const SaveDocPath = "C:\WINDOWS\Profiles\Brad\My Documents\Access\Samples\Access_2000\"

Set oWord = CreateObject("Word.Application")
With oWord
.Documents.Add TemplatePath, NewTemplate:=False, DocumentType:=0
.Visible = True
.Activate
.WindowState = wdWindowStateMaximize
.WindowState = wdWindowStateNormal
End With

Set oRange = ActiveDocument.Range(Start:=0, End:=0)
oRange.ParagraphFormat.Alignment = wdAlignParagraphLeft

With oRange
.InsertParagraphAfter
.InsertAfter Me.CompanyName

.InsertParagraphAfter
.InsertAfter Me.ContactName

.Font.Name = "Arial"
.Font.Size = 12
.InsertParagraphAfter
End With
Exit Sub
EndItNow:
oWord.Quit
Set oWord = Nothing
End Sub
 
Option Explicit

Dim WithEvents moDoc As Word.Document


Private Sub moDoc_Close()

End Sub
 
Thanks for responding Justin.

I need a little more help as it compiles but is not working.

The declarations sections (Dim WithEvents moDoc As Word.Document) defines moDoc. Should I be doing something differently with (.Documents.Add) statement?


Set oWord = CreateObject("Word.Application")
With oWord
.Documents.Add
.Visible = True
.Activate
.WindowState = wdWindowStateMaximize
.WindowState = wdWindowStateNormal
End With
 

With oWord
set moDoc = .Documents.Add
.Visible = True
.Activate
.WindowState = wdWindowStateMaximize
.WindowState = wdWindowStateNormal
End With
 
Thanks again Justin.

This resolved this issue and got me to the next issue - which I have completed. Now I can generate multiple docs, with operator editing, from Access without errors on succssesive runs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top