|
-
Sep 10th, 2009, 02:07 AM
#1
Thread Starter
Junior Member
Create A New Event in Excel VBA
Is it possible at all to create entirely new events in Excel VBA, not just attaching to an existing one? What I've been trying to do and not succeeding so far is to create an event which lets me know before a window is closed: a "Window_BeforeClose()" event basically. Why do I want to do this? I have an Excel VBA project with two windows that have to stay open. One of my users managed to crash the project because he accidentally closed a window. So I want to know when someone tries to close a window and then override this action.
-
Sep 10th, 2009, 01:59 PM
#2
Re: Create A New Event in Excel VBA
Thread moved from the 'CodeBank' forum (which is for you to post your code examples, not questions) to the 'Office Development' forum
-
Sep 11th, 2009, 07:02 AM
#3
Re: Create A New Event in Excel VBA
Are you using Excel UserForms?
-
Sep 11th, 2009, 09:45 AM
#4
Thread Starter
Junior Member
Re: Create A New Event in Excel VBA
No, I'm not using an Excel user form. The windows are standard workbook windows but opened as two windows so that instead of having "Book1" as the only opened window I have two windows named "Book1:1" and "Book1:2" or whatever the filename is by Excel.
I need both windows to be opened for use and they are addressed in code as windows(n).property where n = 1 or 2 in this case. If one of the windows gets close all windows(n).property codes result in errors being raised. I've worked round this for now by trapping these errors and re-opening the closed window. It would however be nicer and tidier to be able to stop the window being closed in the first place.
Last edited by abbey-abbey; Sep 11th, 2009 at 09:51 AM.
-
Sep 11th, 2009, 10:10 AM
#5
Re: Create A New Event in Excel VBA
AFAIK, you cannot create new events for an Excel object. The events are "hard coded" into the object, such as a Worksheet, Workbook, Window and may not be changed. You can, however, create custom events for your own classes.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 11th, 2009, 01:26 PM
#6
Thread Starter
Junior Member
Re: Create A New Event in Excel VBA
Thanks koolsid. It explains why I've been banging my head against the wall.
-
Sep 14th, 2009, 01:43 AM
#7
Thread Starter
Junior Member
Re: Create A New Event in Excel VBA
 Originally Posted by koolsid
AFAIK, you cannot create new events for an Excel object. The events are "hard coded" into the object, such as a Worksheet, Workbook, Window and may not be changed. You can, however, create custom events for your own classes.
How about this:
If I create a new class, can I represent the open window as a new object in this class and then create my own unique event to this new object?
Perhaps this way I can have a Window_BeforeClose() event?
-
Sep 14th, 2009, 04:01 AM
#8
Addicted Member
Re: Create A New Event in Excel VBA
Hi abbey,
a simple solution would be this small procedure in the Workbook_WindowActivate sub.
Advantage: From opening the workbook till closing - always 2 windows of the same file are open.
Disadvantage: File can only be closed by menu File->close
Flipping of the window if you want to close one
But a thing this one can be achieved.
best regards
TheOnly
Code:
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
With ThisWorkbook
If InStr(1, Wn.Caption, "xls:") = 0 Then
.NewWindow
.Windows.Arrange xlArrangeStyleHorizontal
End If
End With
End Sub
-
Sep 14th, 2009, 05:52 AM
#9
Re: Create A New Event in Excel VBA
@Abbey: I did go through your post 1 but could you explain more in detail about this "Window" and what exactly do you want to do?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 14th, 2009, 07:12 AM
#10
Thread Starter
Junior Member
Re: Create A New Event in Excel VBA
I provided this detail to Hack earlier. My response is reproduced below:
"No, I'm not using an Excel user form. The windows are standard workbook windows but opened as two windows so that instead of having "Book1" as the only opened window I have two windows named "Book1:1" and "Book1:2", or whatever the filename is, by Excel.
I need both windows to be opened for use and they are addressed in code as windows(n).property where n = 1 or 2 in this case. If one of the windows gets close all windows(n).property codes result in errors being raised. I've worked round this for now by trapping these errors and re-opening the closed window. It would however be nicer and tidier to be able to stop the window being closed in the first place."
-
Sep 14th, 2009, 07:21 AM
#11
Thread Starter
Junior Member
Re: Create A New Event in Excel VBA
Thanks TheOnly. I already used a similar method to get round the problem. As you rightly point out, you get window flipping when the closed window is re-opened. This is also complicated because window(1) is always the active window regardless of whether or not its caption index is 1, 2 or whatever. So I had to sort this out in code and make sure the correct tabs are activated in the correct window and ...
That's why I'm so keen on a WindowBeforeClose event because it will make life so easy then.
-
Sep 14th, 2009, 12:02 PM
#12
Re: Create A New Event in Excel VBA
There is one way that I can think of ...
Creating this
vb Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) End Sub
during runtime in the workbooks that you open. because when you close the Window, the Workbook_BeforeClose event is fired... probably that might help you with your query..
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 15th, 2009, 02:01 AM
#13
Thread Starter
Junior Member
Re: Create A New Event in Excel VBA
Unfortunately this will not work because Workbook_BeforeClose event only gets fired when you try to close the workbook or the last window in a multi-window workbook but not when you close a 1 of n window.
-
Sep 15th, 2009, 03:51 AM
#14
Addicted Member
Re: Create A New Event in Excel VBA
Hi abbey,
Have found the solution hat window 1 is always on top regardless which of the 2 windows do you close.
The class thing would be wonderfull but I think this is only managable with API stuff and thats not so easy.
best regards
theOnly
Code:
With ThisWorkbook
'look if we found a more than one windows workbook
If InStr(1, Wn.Caption, "xls:") = 0 Then
' no - create a new window
.NewWindow
'imediate change to the Window number 1
'so that win 1 will be always on top of the screen
.Windows(ThisWorkbook.Name & ":1").Activate
'now we can arrange the workbook
.Windows.Arrange xlArrangeStyleHorizontal
End If
End With
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|