View Single Post
 
Old 04-27-2012, 01:48 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Tony,

Using Events requires that you instantiate the app object class. To do this, create a new class module in your mailmerge main document to register the desired Word events. At the top of the module put:
Code:
Public WithEvents wdApp As Word.Application
Public WithEvents wdDoc As Word.Document
Below that, add the following code to intercept the save event:
Code:
Private Sub wdApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
If Doc.MailMerge.MainDocumentType <> wdNotAMergeDocument Then
  MsgBox "This is the mailmerge main document," & vbCr & _
  "not the output document." & vbCr & vbCr & _
  "Do not send this document to the client.", vbExclamation
End If
End Sub
 
Private Sub wdApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim Rslt As Variant
If Doc.MailMerge.MainDocumentType <> wdNotAMergeDocument Then
  Rslt = MsgBox("This is the mailmerge main document," & vbCr & _
  "not the output document." & vbCr & vbCr & _
  "Do not send this document to the client." & vbCr & vbCr & _
  "Continue Saving?", vbOKCancel)
End If
If Rslt = vbCancel Then Cancel = True
End Sub
The above code should give the messages you desire.

Then, in a normal code module, put:
Code:
Dim wdAppClass As New ThisApplication
Public Sub AutoExec()
Set wdAppClass.wdApp = Word.Application
End Sub
This code will allow the events in your Word document to be called.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote