View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Run an Excel code every 15 minutes of the clock

Indeed the Now function is "Invalid Outside Procedure", you need to put it
inside a procedure. Eg

Sub abc()
dim dt as Date
dt = Now
Msgbox dt
End sub

For your future reference, The top of a module is the "Declarations" area.
This is where API functions are declared, together with module or global
level variables and constants. Everything else belongs in a procedure (Sub,
Function, etc).

It's normally best to store the time the Onime macro scheduled to run in a
module level variable is so it can be cancelled if/as necessary, which you
can do by calling the OnTime as normal but with the argument Schedule:=False

It's not clear when you do / don't want to run the OnTime, gaps (not
lunchtime?) the first time and when to cancel it altogether. Try and
describe.

Regards,
Peter T






"thomas" wrote in message
...
Peter,

The "Now" got flagged for "Compile Error: Invalid Outside Procedure".

But how do I incorporate your code into mine as follows:

Option Explicit
Private Declare Function GetProcessVersion Lib "kernel32" ( _
ByVal ProcessID As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As
Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal
dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As
Long) As Long
Private Declare Function EnumProcesses Lib "PSAPI.DLL" (ByRef lpidProcess
As
Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal
hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal
nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal
hProcess
As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As
Long)
As Long
Public RunWhen As Double
Public Const cRunIntervalSeconds = 900
Public Const cRunWhat = "Make_SGX_Txt" ' the name of the procedure to run
Dim FirstTime As Boolean
Sub StartTimer()

If FirstTime Then
RunWhen = Date + TimeSerial(8, 45, 0)
Else
If Time TimeSerial(8, 45, 0) And Time <= TimeSerial(12, 30, 0) Or
Time
TimeSerial(13, 59, 0) And Time < TimeSerial(17, 10, 0) Then

RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
End If
End If

Application.OnTime EarliestTime:=RunWhen, _
Procedu=cRunWhat, Schedule:=True

End Sub

Sub Make_SGX_Txt()

Dim ProcID As Long
Dim Version As Long
Dim LastErr As Long
Dim ThisProcID As Long
ProcID = 1092

{ what I want to do }



Thomas