Creating an Application Event

countingapples

Board Regular
Joined
Feb 20, 2003
Messages
243
I would like to change a workbook_open event to one that automatically runs when excel is first started. Everything works as I have it now, I just can't figure out how to do the modifications. Code as I have it follows.

ThisWorkbook:
Code:
 Option Explicit

Dim AppClass As New EventClass

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

EventClass:
Code:
Public WithEvents App As Application

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
   Call ShowQuote
End Sub

Practice1 (regular module):
Code:
Dim dateStartTime As Date, dateEndTime As Date
Sub ShowQuote()
    dateStartTime = #2:52:00 PM#
    dateEndTime = #2:53:00 PM#
       If Time >= dateStartTime Then
            Do Until Time >= dateEndTime
                UserForm1.Show
            Loop
        End If
        If Time < dateStartTime Then
            'UserForm1.Hide
            MsgBox "Can I stay here and train with you Mr. PoPo?"
        End If
        If Time > dateEndTime Then
            MsgBox "         It works!!"
        End If
    End Sub

UserForm1:
Code:
Private Sub OKButton_Click()
'Unload Me
End Sub
Private Sub UserForm_Activate()
Dim Quote As String
Quote = "   Abandon hope all ye who enter here"
lblNow.Caption = Quote
End Sub

The purpose of this exercise is a practical joke on our departmental joker. Everyone is in on it, and I'm the one creating this.
Ideally, we would like to have this erase itself after running. Where the specific time to run is located, I want to change that to run for 3 minutes upon excel starting. I think I need a DateAdd function but am uncertain what to put for the starting date portion of the syntax.

Any help would be greatly appreciated.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
http://office.microsoft.com/assista...ID=HA010346281033&CTT=6&Origin=EC010553071033

Quote:
Using either method [workbook open or auto_open] to create the macro requires you to save it in a particular workbook. As a result, if you want the macro to run whenever Excel itself is started, you must make sure that the workbook that contains the macro is automatically opened when Excel starts. To do that, you must save the workbook that contains the start-up macro (or a shortcut that points to that workbook) in the XLStart folder. The location of the XLStart folder varies depending on the version of Microsoft Windows® you are using and how it is configured. .... (More)
 
Upvote 0
Thanks for the link on the XL Start folder Swamp Thing.

  • Does anyone have any suggestions on the self-erasing or date parts?

    In DateAdd, is there a way to specify a particular day and time?
For both the start and end times I want to specify the exact day to go with the times. I can't get them to work when I try to add a specific date to the times. I have read the entire VBA help file, my VBA book, and the MSN website that I linked to from the above. They were as clear as mud. :oops:
 
Upvote 0
Is there a way have code in the This Workbook object erase itself after it completes its mission?
In reading another post, the idea was to put in code so it runs on only the inital opening of excel, this will work quite well for me. Erasing the code will make this even sweeter if possible to do.
The target has problems all the time with his computer anyway. May as well take advantage of the situation. :devilish:
 
Upvote 0

Forum statistics

Threads
1,216,063
Messages
6,128,559
Members
449,458
Latest member
gillmit

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top