Hey all,
This post is in reference to the following code which was created in this discussion http://www.mrexcel.com/board2/viewtopic.php?t=234943&start=10
I was now wondering if it would be possible to make it so in the active sheet i could set 2 cells for "scheduling" the times that the program opens or closes - as it is the code opens the program at 12midnight then closes it at 4pm. (between 4pm and midnight the program will be shut if opened)
By allowing these times to be entered into cells it would mean i could set anytime for instance open at midnight close at 1am or open at 6pm close at 3am etc etc
Any help would be great,
In advance thanks a heap for your time,
Titus
ps if this isnt explained very well just let me know and i will do my best to re explain
This post is in reference to the following code which was created in this discussion http://www.mrexcel.com/board2/viewtopic.php?t=234943&start=10
Code:
Option Explicit
'Change MyProcess to the programme you want to start and stop
'I used the calc.exe programme for my test
Public Const MyProcess As String = "Azureus.exe"
Public Const FilePath As String = "C:\Program Files\Azureus\"
'This is a log file to track instances of starting and stopping the process
'Remove the references to the log file, here and within the code, if you don't want this
Public Const MyFile As String = "c:\MyLog.txt"
Public Sec As Integer
Public When As Variant
Sub MyTimer()
When = Now + Sec / 60 / 60 / 24
Application.OnTime When, "MyTimer"
If Hour(Now) < 16 Then
StartMyProcess (MyProcess)
Else
StopMyProcess (MyProcess)
End If
'Used for testing purposes only (after setting Sec to 20 in the Workboook_Open module)
'If Minute(Now) Mod 2 = 0 Then
' StopMyProcess (MyProcess)
'Else
' StartMyProcess (MyProcess)
'End If
End Sub
Private Sub StopMyTimer()
Application.OnTime EarliestTime:=When, Procedure:="MyTimer", schedule:=False
End Sub
Private Sub StartMyProcess(strStartThis As String)
On Error Resume Next
Dim objWMIcimv2 As Object
Dim objList As Object
Dim procID As Long
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strStartThis & "'")
If objList.Count = 0 Then
'The process isn't running
procID = Shell(FilePath & strStartThis, vbHide)
Open MyFile For Append As #1
Write #1, Now & " : Process started ok."
Close #1
Else
'The process has already been started
DoEvents
Open MyFile For Append As #1
Write #1, Now & " : Process already started."
Close #1
End If
Set objWMIcimv2 = Nothing
Set objList = Nothing
End Sub
Private Sub StopMyProcess(strTerminateThis As String)
On Error Resume Next
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strTerminateThis & "'")
If objList.Count = 0 Then
'The process isn't running
Open MyFile For Append As #1
Write #1, Now & " : Process already stopped."
Close #1
Else
'The process is running
Open MyFile For Append As #1
For Each objProcess In objList
'Terminate the process and all of its threads.
intError = objProcess.Terminate
If intError <> 0 Then
'Return value is 0 for success. Any other number is an error.
Write #1, Now & " : Unable to terminate process. intError = " & intError
Else
Write #1, Now & " : Process terminated ok."
End If
Next
Close #1
Set objProcess = Nothing
End If
Set objWMIcimv2 = Nothing
Set objList = Nothing
End Sub
I was now wondering if it would be possible to make it so in the active sheet i could set 2 cells for "scheduling" the times that the program opens or closes - as it is the code opens the program at 12midnight then closes it at 4pm. (between 4pm and midnight the program will be shut if opened)
By allowing these times to be entered into cells it would mean i could set anytime for instance open at midnight close at 1am or open at 6pm close at 3am etc etc
Any help would be great,
In advance thanks a heap for your time,
Titus
ps if this isnt explained very well just let me know and i will do my best to re explain