Run a macro each time a workbook is saved

  • Thread starter Thread starter donbuz
  • Start date Start date
D

donbuz

Hi everybody,
I would like to know how to run a macro any time a workbook is bein
saved. I created my macro is saved in "Personal.xls" but I do not kno
how to trigger it without using a button associated to it but instea
when the user would click File->save or directly the save icon. I trie
to add a call to my macro in the code of the event BeforeSave o
"Personal.xls" but it does not work...

Thanks to whoever can give me a hand on this.

donbu
 
You will need to create class event to trap the event of any open workbook
being saved.
The class event can reside in your personal.zls.

A good resource to read up on this is in chip pearson web site.
url I think is www.cpearson.com

as well do a googgle search on class events.

to get you started.....
Put the following in a new class module
' use insert class module from vbe

rename it to EventClass

Option Explicit
Public WithEvents App As Application

Private Sub App_WorkbookBeforeSave(ByVal wb As Workbook, Cancel As Boolean)
' your code
end sub


In this thisworkbook module of personal.xls
Option Explicit
Dim AppClass As EventClass

Private Sub Workbook_Open()
Set AppClass = New EventClass
Set AppClass.App = Application
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set AppClass.App = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top