PDA

View Full Version : How to give a string parameter in function app.OnTime



QueenofChaos
04-17-2009, 07:21 AM
Hi everyone,
I don't understand something in my Code.
I use the event DocumentBeforeSave and I put a timer in it to execute a procedure after the save.
The call of the procedure worked fine until I added a string parameter to it.
Now It' simply don't even call the procedure.
I don't know if I did something wrong with the parameter but hardly see where.
Please if you have any idea just tell me.


Here is the event who call the procedure with the OnTime function.
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim oldPath As String
Dim procedure As String
oldPath = ActiveDocument.FullName
'Lance un timer pour que le sous programme se lance apres la sauvegarde.
App.OnTime Now(), "'ThisDocument.AfterSaveProcName """ & oldPath & """'"
End Sub

And here is the procedure who is called
'Permet de mettre à jour l'ID après chaque sauvegarde
Sub AfterSaveProcName(ByVal oldPath As String)
Dim Path As String
Path = ActiveDocument.FullName
ActiveDocument.ID.Value = CheckID(oldPath, Path)
MsgBox "ID a été mis à jour"
End Sub

Thanks in advance

Kenneth Hobs
04-17-2009, 08:34 AM
It can be a bit tricky. See if my example helps.
http://vbaexpress.com/forum/showthread.php?t=25896

QueenofChaos
04-17-2009, 12:26 PM
Thanks for your answer but it dosn't really help me. In your exemple I'don't see a call to a procedure with a string parameter. The problem can't be anywhere else because it's work without le parameter.
If someone see a syntax error let me know, I'm a bit confuse with all this quotes.

Kenneth Hobs
04-17-2009, 01:14 PM
Make it work without a parameter first. I would not pass one though. I would just use a Public variable or a Constant as shown in my example if the OnTime routine needed to know something.

If quotes get hairy, I like to do this:
Dim q as String
q = """"
MsgBox q & "Hello World!" & q

mdmackillop
04-18-2009, 09:33 AM
Option Explicit
Sub Test()
Dim oldPath As String
Dim strText As String
Dim NextTime As Date

oldPath = ActiveWorkbook.FullName
strText = "'AfterSaveProcName " & """" & oldPath & """" & "'"
NextTime = Now + TimeValue("00:00:03")
Application.OnTime NextTime, strText
End Sub

Sub AfterSaveProcName(Data As String)
MsgBox Data
End Sub



Based on answer here

http://www.eggheadcafe.com/forumarchives/Excelprogramming/Jun2005/post23583253.asp
Tushar Mehta

QueenofChaos
04-20-2009, 01:22 PM
I've tried your code but it didn't work. I tried to call a procedure without parameters that is in the same class module as the BeforeSave event but it dosn't work. But if I put the same procedure in "ThisDocument", the call works. It's not possible to call a procedure that is in a class module?
I wondering if it's even possible to give a parameters to a procedure who is in "ThisDocument" from a class module, maybe it is the problem, do you have any idea?
This code is in my Class Module "EventClass"
Option Explicit
Public WithEvents App As Word.Application
Public beforeChange As String
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim strText As String
Dim NextTime As Date
beforeChange = ActiveDocument.FullName
App.OnTime Now(), "EventClass.test"
End Sub

Sub test()
MsgBox beforeChange
End Sub

mdmackillop
04-20-2009, 01:25 PM
You've posted in the Excel forum. Are you trying to run Word from Excel, or is this really a Word question?

QueenofChaos
04-20-2009, 01:35 PM
You're totaly right I've post in the wrong forum, I'm working with Word. I'm so use to do some VBA in Excel that I pick the wrong forum. I'm really sorry, I guess there is no way to transfer the post in the right section.

mdmackillop
04-20-2009, 03:37 PM
Moved from Excel forum

Paul_Hossler
04-21-2009, 07:25 PM
Mac, can you please tell me what I'm doing wrong here? I'm also trying to pass a parm to OnTime in Word 2007 (thanks for the link, BTW), and not getting anything

I have triple checked the single and double quotes, but still not seeing anything wrong

I have this bit of code in a standard module, nothing tricky with classes or anything, but the OnTime macro won't fire with a passed parm. I've done it in Excel, but maybe Word works differently???


Option Explicit
Sub SimpleCase()
Dim s As String


s = "TestProc "
s = s & Chr(34) & Chr(34) & "AAAA" & Chr(34) & Chr(34)
s = "'" & s & "'"

MsgBox s

Call Application.OnTime(Now + TimeValue("00:00:01"), s)
End Sub

Sub TestProc(s As String)
MsgBox s
End Sub


Thanks

Paul