PDA

View Full Version : when time is 22:00, the word file will activate and a mesage box will appear



uktous
03-20-2012, 08:03 AM
Hi,

I want a macro that will show a message box when the time is 22:00 automatically.

I produced the following macro.
If I run it at 22:00, the message box will appear.
However, I hope that the message box will appear automatically.

How can I do that?



If Time > #10:00:00 PM# And Time < #10:00:59 PM# then

Msg = "Time is 22:00"
Style = vbOKOnly

Response = MsgBox(Msg, Style)

End If


Thanks

fumei
03-20-2012, 03:11 PM
This can be done using the OnTime method. Check your VBA Help for examples. In your case use an AutoExec macro that executes when Word starts. It would start the OnTime method, wait until 22:00 then fire the macro listed as the parameter. THAT macro would have your message.

Of course if Word is not open, nothing will happen.

uktous
03-20-2012, 06:25 PM
This can be done using the OnTime method. Check your VBA Help for examples. In your case use an AutoExec macro that executes when Word starts. It would start the OnTime method, wait until 22:00 then fire the macro listed as the parameter. THAT macro would have your message.

Of course if Word is not open, nothing will happen.

where I should put the macro?

I think should not be in any modules.

fumei
03-20-2012, 08:51 PM
I think should not be in any modules.

Huh??? If it is not in any module NOTHING will happen. Code must be in a module for code to run.

Put your AutoExec in a module in Normal.dot. Put your macro with your messagebox in the same module.

fumei
03-20-2012, 11:05 PM
Actually, if you intend to KEEP a document open for the time until 22:00, you can put the code in a module in that document.

Also, you do not need to use an AutoExec macro if you do not need the OnTime to start working when Word is started. If you want to start the OnTime yourself, you can simply have two macros (see example below), and run the starting one when you like. But again, you MUST keep Word loaded. If you close Word, the OnTime method will - obviously - not work.
Sub StartOnTime()
Application.OnTime When:="22:00:00", Name:="It_Is_11"
End Sub

Sub It_Is_11()
MsgBox "Hey....it is 11:00 PM!"
End Sub

Frosty
03-21-2012, 10:14 AM
If you have Microsoft Word, then you probably have Microsoft Outlook.

Can't you just use a Calendar appointment? Other than that, the only way to approach it is what Fumei has already described (although you don't have to use AutoExec... you can, as Fumei has said, put the code in the ThisDocument, and simply have it run in the Document_Open event.

If you can give a little more info on what you actually want to happen (I'm assuming the message box is simply for a proof of concept), you might get a better answer for how to approach it.

The following code, in the ThisDocument module (it's the first thing) would work the same way, except that it would start when you opened the document automatically...


Private Sub Document_Open()
Application.OnTime When:="22:00:00", Name:="It_Is_11"
End Sub
Sub It_Is_ll()
Msgbox "Hey... it is 11:00 PM! Go home already!"
End Sub

Talis
03-21-2012, 09:04 PM
I must be particularly thick today, but why 'When it is 22:00:00" would you say "...it is 11:00 PM!"? Shouldn't it be 10:00 PM?

Has Lucy put a curse on you guys!?

fumei
03-21-2012, 09:22 PM
LOL. Jason...we are both busted! In my defence, in my own coding in my document I had it as 23:00 (because it was coming up to 11:00 PM my time and I wanted to test). I typed (not copied) the code going into my post. So i put it as 22:00...but forgot my own message.

Nevertheless, yup...busted. And no, no curse. It would have to take a much bigger source to do that...if I believed in curses, which I do not.

And in Jason's defense, he probably was paying too much attention to my post. I take full responsibility. LOL.

Frosty
03-22-2012, 11:18 AM
Haha... whoops. As always, I agree with Fumei. He is totally at fault here. :)