Max Wanadoo
max.wanadoo at gmail.com
Thu Feb 7 17:26:55 CST 2008
John,
I have had a play with this but I cannot get the WithEvents part of the
Class module to kick-in.
If you go directly to the command "objword.appWord.ActiveDocument.Close"
then the WithEvents kicks in ok, but i cannot make it happen when the user
uses the BIG X on the document.
You know more about Classes than I. You may be able to get it working.
Max
HERE IS THE CLASS MODULE (clsWord)
Option Compare Database
Option Explicit
' Set reference to Microsoft Word 11.0 object library
Public WithEvents appWord As Word.Application
Private Sub appWord_DocumentBeforeClose(ByVal Doc As Word.Document, Cancel
As Boolean)
MsgBox "Please close the document using the button on the Data Form"
Cancel = True
End Sub
Private Sub Class_Initialize()
On Error Resume Next
Set appWord = New Word.Application
If Err.Number <> 0 Then
Err.Clear
End If
End Sub
Private Sub Class_Terminate()
Set appWord = Nothing
End Sub
HERE IS THE MODULE CODE WHICH USES THE CLASS (modWord)
Option Compare Database
Option Explicit
Private objword As New appWord
Private Function fTestWordObject()
Dim strLetter As String, strSalutation As String, strID As String, strAddr
As String
strLetter = "c:\Test.dot": strSalutation = "Dear Sirs,": strID = "123"
strAddr = "123 High St" & vbCrLf & "Derby" & vbCrLf & "UK"
' Dim objword As New appWord
objword.appWord.Documents.Add Template:=strLetter, newtemplate:=False
objword.appWord.Visible = True
With objword.appWord.ActiveDocument.Bookmarks
.Item("ID").Range.Text = strID
.Item("Date").Range.Text = CStr(Format(Date, "long date"))
.Item("Address").Range.Text = strAddr
.Item("Salutation").Range.Text = strSalutation
End With
exithere:
Set objword = Nothing
Exit Function
End Function
' assume this is the button code on the form to close the document
Private Sub ButCloseWord_Click()
Dim intResponse As Integer
intResponse = MsgBox("Do you want to close the document?", vbYesNo)
If intResponse = vbYes Then
objword.appWord.ActiveDocument.Close True
End If
End Sub