ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Running code before any file is opened/saved (https://www.excelbanter.com/excel-programming/358899-running-code-before-any-file-opened-saved.html)

Bill Schanks

Running code before any file is opened/saved
 
I found the below code posted around Nov 2005:

In a new Class module name clsXLEvents
Option Explicit
Private WithEvents xlApp As Excel.Application


Private Sub Class_Initialize()
Set xlApp = Excel.Application
End Sub


Private Sub xlApp_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal
SaveAsUI As
Boolean, Cancel As Boolean)
MsgBox "Tada"
End Sub


In a standard module named whatever
Option Explicit


Public xlApp As clsXLEvents


And in ThisWorkbook
Option Explicit


Private Sub Workbook_AddinInstall()
Set xlApp = New clsXLEvents
End Sub


Private Sub Workbook_AddinUninstall()
Set xlApp = Nothing
End Sub


Private Sub Workbook_Open()
Set xlApp = New clsXLEvents
End Sub

---

My question is is there a way to make a global Workbook_Open code?

I tried adding:

Private Sub xlApp_Workbook_Open(ByVal Wb As Workbook)

MsgBox "Open"

End Sub

In 'clsXLSEvents', but to no avail.


Bob Phillips[_6_]

Running code before any file is opened/saved
 
Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
MsgBox Wb.Name & " opened"
End Sub

In the class module, in the dropdown boxes at the top of the module, if you
select xlApp from the left dropdown, the right dropdown shows you what
events are available.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Bill Schanks" wrote in message
oups.com...
I found the below code posted around Nov 2005:

In a new Class module name clsXLEvents
Option Explicit
Private WithEvents xlApp As Excel.Application


Private Sub Class_Initialize()
Set xlApp = Excel.Application
End Sub


Private Sub xlApp_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal
SaveAsUI As
Boolean, Cancel As Boolean)
MsgBox "Tada"
End Sub


In a standard module named whatever
Option Explicit


Public xlApp As clsXLEvents


And in ThisWorkbook
Option Explicit


Private Sub Workbook_AddinInstall()
Set xlApp = New clsXLEvents
End Sub


Private Sub Workbook_AddinUninstall()
Set xlApp = Nothing
End Sub


Private Sub Workbook_Open()
Set xlApp = New clsXLEvents
End Sub

---

My question is is there a way to make a global Workbook_Open code?

I tried adding:

Private Sub xlApp_Workbook_Open(ByVal Wb As Workbook)

MsgBox "Open"

End Sub

In 'clsXLSEvents', but to no avail.




Bill Schanks

Running code before any file is opened/saved
 
Thanks ... that works great!



All times are GMT +1. The time now is 10:12 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com