PDA

View Full Version : problem using Application.Ontime in an addin



jzz1981
12-06-2012, 08:44 AM
Hi all,

I am encountering a problem when using an Application.Ontime in a Global template. It just doesn't work. Seems that the ontime method only works in a document that's currently open.

I'm trying to display information retrieved from a website and refresh this every hour. This should be available in Word, whatever document is used at the moment. So I made a global template with an 'AuteExec' macro, which fires sub to retreive the info and set a timer to do this every hour.

The AutoExec is doing its work, the info is retreived and displayed as soon as Word starts. But it is never updated. I've made some tests with simpler subroutines, and my conclusion is that the timer simply won't work in a global template (the whole timer and retreive routine works flawlessly if I use an open document). By the way, normal.dot does the same. Nothing that is.

Anyone recognize this problem? Any thoughts about possible solution / workaround?

This is for work, where Office 2007 is used, at home, Office XP, same result.

Greetings,

Jzz

fumei
12-06-2012, 11:36 AM
Please post all relevant code.

jzz1981
12-06-2012, 11:58 AM
The following setup is what I want, but what does not work. I typed this from the top of my head on a tablet, as I'm not behind my computer right now, so sorry for any typos.


Sub AutoExec()

Call testsub

End sub

Sub testsub()
Dim bar as Commandbar
Dim cnt as CommandbarControl

Set bar = Commandbars("testbar")
For each cnt in bar.controls
cnt.delete
Next cnt

Set cnt = bar.controls.add msoCommandButton
cnt.Caption = Now

Application.Ontime Now + Timeserial(0, 0, 5), "testsub"

End sub



When I start word, the button is created with the right caption, but the sub is never called again.

gmaxey
12-06-2012, 12:22 PM
Are you certain the code is not running? I used the following in a global template and Word beeps every 5 seconds:

Sub AutoExec()
Call testsub
End Sub
Sub testsub()
Beep
Application.OnTime Now + TimeSerial(0, 0, 5), "testsub"
End Sub

jzz1981
12-06-2012, 12:28 PM
As I think of it, no, I'm not sure. Could be some problem with the Commandbar either.

I'll try with the Beep, eliminating the Commandbar. Maybe needs redraw or something like that... I'll give another update.

jzz1981
12-06-2012, 12:50 PM
Good one to try! I does work. Then there must be something with the commandbar. Is there some redraw function to be called?

Or are there other solutions I have not thought about? I like to display information. Only a small text, probably 10 chars at the most. This info must be available to the users of Word (actually the computer, that is, but I'm limited to VBA and I can rely on Word always being open).

Greetings, and thanks for the help so far already.

Edit:

I can get it to work in an open document, so that pretty much eliminates the redraw question. Any other thoughts?

gmaxey
12-06-2012, 01:05 PM
How about:

Option Explicit
Sub AutoExec()
Call testsub
End Sub
Sub testsub()
Beep
Application.StatusBar = "The time is now: " & Now & " do you know where your children are?"
Application.OnTime Now + TimeSerial(0, 0, 5), "testsub"
End Sub

jzz1981
12-06-2012, 01:31 PM
That's what I've been trying. Without the question where my children are, that is. This is my home computer and here it now seems to work, although not very consistently every 5 seconds. Probably something with priorities I geuss. Anyways, not important.

I'll try again at work if I have time (will be there at the weekend) and post back results. But I did try test simular to this. Although that was during a Nightshift, so it could be my mistake after all (between keyboard and chair).

Thanks so far Greg!

jzz1981
12-08-2012, 12:36 AM
Hmmm...

At work now, but here no go. I have office 2003.

Beep is disabled somehow, probably in Windows settings (which I can't reach due to security), but I can play 'Chimes.wav' (standard Windows notification sound).

When I make a test document, with:


Sub AutoExec()

call Test

End Sub

Sub Test()

playsound "C:\Windows\Media\Chimes.wav"
Application.OnTime Now + TimeSerial(0, 0, 5), "Test"

End Sub



This works. Every 5 seconds I hear Chimes.

But if I save this document as 'test.dot' in the Word startup folder and restart Word, I hear Chimes, I hear Chimes again and then... silence... So the AutoExec works, the sound works, the timer works... But only once.

:banghead:

Any thoughts?

greetings, Jzz

gmaxey
12-08-2012, 05:49 AM
It works here. Out of ideas. Sorry.

gmaxey
12-08-2012, 08:22 AM
You might try:

Option Explicit
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
Private lngTHdl As Long
Const lngInterval_Timer As Long = 5000 ' in ms
Sub AutoExec()
StartTimer
End Sub
Sub StartTimer()
On Error GoTo StartTimer_Error
StartApiTimer lngInterval_Timer
On Error GoTo 0
Exit Sub
StartTimer_Error:
StopApiTimer
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure StartTimer"
End Sub
Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal CurTHdl&, ByVal CurSystemTime&)
Dim dTime As Date
On Error GoTo TimerProc_Error
Beep
Application.StatusBar = "The time is " & Now & ", do you know where your children are?"

Exit Sub
TimerProc_Error:
StopApiTimer
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure TimerProc"
End Sub
Sub StartApiTimer(ByVal lngMS As Long)
lngTHdl = SetTimer(0&, 0&, lngMS, AddressOf TimerProc)
End Sub
Sub StopApiTimer()
On Error GoTo StopApiTimer_Error
KillTimer 0&, lngTHdl: lngTHdl = 0
On Error GoTo 0
Exit Sub
StopApiTimer_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure StopApiTimer"
End Sub



Hmmm...

At work now, but here no go. I have office 2003.

Beep is disabled somehow, probably in Windows settings (which I can't reach due to security), but I can play 'Chimes.wav' (standard Windows notification sound).

When I make a test document, with:


Sub AutoExec()

call Test

End Sub

Sub Test()

playsound "C:\Windows\Media\Chimes.wav"
Application.OnTime Now + TimeSerial(0, 0, 5), "Test"

End Sub



This works. Every 5 seconds I hear Chimes.

But if I save this document as 'test.dot' in the Word startup folder and restart Word, I hear Chimes, I hear Chimes again and then... silence... So the AutoExec works, the sound works, the timer works... But only once.

:banghead:

Any thoughts?

greetings, Jzz

Frosty
12-10-2012, 11:13 AM
Couple of thoughts:
1. Beep works off of the way you have sounds configured on your computer. If you don't have the Alert (if I recall correctly) set up to play a sound, then the Beep will trigger, but no sound will be played. I don't remember the exact option, but you need to look at control panel > Sounds

2. Office 2007 deals with commandbars quite a bit differently. You don't have options to dynamically modify command bars the same way you do in Office 2003.

3. Office 2007 is terribly buggy. Office 2010 solves a lot of problems that Office 2007 introduced.. Rather than try to solve some esoteric problem which relates to a bug, can you describe what you actually want to have happen (why you're interested in this Application.OnTime functionality)? Because if it is really related to command bar controls in Office 2007, I think you're ultimately going to be barking up the wrong tree-- you will need to start investigating the ribbon interface and ribbon callback functions (and Greg has a lot of great tutorials on his website to give you guidance).

Bottom line: if you are working on a code template which needs to be compatible with both Word 2003 and Word 2007 -- you are going to need to separate out your user interface (commandbars in 2003, ribbon in 2007/2010) into a separate template, pull out or dynamically delete the commandbars when you're in Office 2007/2010, and connect your user interface differently, depending on what application you desire to run the code in.

Other than that-- I think Greg has given you every direction I can think of.