Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a macro in Word before save?

Tags:

ms-word

vba

How to make Microsoft Word to run a VBA macro every time before any document is saved? Could it be done without adding macros into the document itself?

like image 661
Dark Daskin Avatar asked Jul 11 '14 16:07

Dark Daskin


1 Answers

You can subscribe to application events in Document_Open by using WithEvents variable and conventional method names (VariableName_EventName). Works in templates as well.

You can put this code into ThisDocument object, or make a separate class module as described here.

Private WithEvents App As Word.Application

Private Sub Document_Open()
Set App = Word.Application
End Sub

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
MsgBox("BeforeSave")
End Sub

List of all application events.

like image 182
Dark Daskin Avatar answered Oct 06 '22 16:10

Dark Daskin