Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-16-2011, 10:43 PM
PosseJohn PosseJohn is offline BeforeClose BeforePrint BeforeSave Windows XP BeforeClose BeforePrint BeforeSave Office 2007
Novice
BeforeClose BeforePrint BeforeSave
 
Join Date: Jul 2011
Posts: 20
PosseJohn is on a distinguished road
Default BeforeClose BeforePrint BeforeSave


I have utilized BeforeClose, BeforePrint, and BeforeSave functions in EXCEL, but I do not see them available for WORD VBA projects.

Is there a way of creating these using a new CLASS definition?
Reply With Quote
  #2  
Old 07-17-2011, 12:18 AM
macropod's Avatar
macropod macropod is offline BeforeClose BeforePrint BeforeSave Windows 7 64bit BeforeClose BeforePrint BeforeSave Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Word has Document_Close, Document_New and Document_Open events (amongst others), and you can add more. Using Events requires that you instantiate the app object class. To do this, create a new class module to register your events. At the top of the module put:
Code:
Public WithEvents wdApp As Word.Application
Public WithEvents wdDoc As Word.Document
Then, in a normal code module, put:
Code:
Dim wdAppClass As New ThisApplication
Public Sub AutoExec()
Set wdAppClass.wdApp = Word.Application
End Sub
This will allow you to get events from your Word document. For example, you can use the selection change event and test if the selection is in a header or footer:
Code:
Private Sub wdDoc_WindowSelectionChange(ByVal Sel As Selection)
If Selection.Information(WdInformation.wdInHeaderFooter) = True Then
  MsgBox "I'm in a header or footer!"
Else
  MsgBox "I'm NOT in a header or footer!"
End If
End Sub
or, more usefully, to ensure a document with formfields is protected before printing:
Code:
Private Sub wdApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
With Doc
  If .FormFields.Count > 0 Then _
    If .ProtectionType = wdNoProtection Then _
    .Protect Type:=wdAllowOnlyFormFields, noreset:=True
End With
End Sub
For further info, see: http://msdn.microsoft.com/en-us/libr...ffice.10).aspx
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 07-17-2011, 01:39 AM
PosseJohn PosseJohn is offline BeforeClose BeforePrint BeforeSave Windows XP BeforeClose BeforePrint BeforeSave Office 2007
Novice
BeforeClose BeforePrint BeforeSave
 
Join Date: Jul 2011
Posts: 20
PosseJohn is on a distinguished road
Default

Thank you so much. GREAT reference link.
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 07:38 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft