Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,775
- Office Version
- 2016
- Platform
- Windows
I'm enquiring about this just out of intellectual curiosity . Any thoughts ?
Regards.
Regards.
Sub AngryInch()
Application.OnTime Now + TimeValue("00:00:30"), "WakeUp"
Application.OnTime Now + TimeValue("00:00:10"), "ShutOff"
End Sub
Private Sub ShutOff()
Application.Visible = False
End Sub
Private Sub WakeUp()
Application.Visible = True
End Sub
I might consider using the open_event to open a little .vbs file to keep track of the time as well as the name of the Excel file just opened...It seems you would need to have the code running outside of excel in any case, since the app will shutdown. I'm assuming in a vbs script you could use a Timer object of some kind.
Thanks Alexander.
Yes, that's what i had in mind. I guess that will need a windows timer. Unfortunatly, VBS doesn't support API declarations.
I am trying to solve this by creating a new instance of excel and reopen the workbook from there. However, i don't want to use the VBE of the seconde instance to inject the code as that will only work depending on the Macro security settings of the user. I want to make this to be as robust as possible.
Regards.
In VBS you have access to WScript.Sleep.
If you use a 2nd instance of Excel, I don't know why you need to modify code. I must be missing something but it appears to me that you already have a workbook with code. So what's to modify?
<font face=Courier New>
With CreateObject("Excel.Application")
.Visible = True
.Workbooks.Open ("C:\Test.xls")
.Application.Run "MyMacro"
End With</FONT>
<font face=Courier New>
' ZVI:2009-07-05 http://www.mrexcel.com/forum/showthread.php?t=400499
' Scheduled reloading of itself
Sub ReloadExcel()
Dim xlFileName$, vbsFileName$, vbsText$, FileNo%, Wb
' Define reloading files
xlFileName = ThisWorkbook.FullName
vbsFileName = Replace(LCase(xlFileName), ".xls", ".vbs")
' Build the text of VB script
vbsText = "With CreateObject(""Excel.Application"")" & vbLf _
& ".Visible = True" & vbLf _
& ".Workbooks.Open (""" & xlFileName & """)" & vbLf _
& ".Application.Run ""MyMacro""" & vbLf _
& "End With"
' Create VBS file
On Error Resume Next
Kill vbsFileName
FileNo = FreeFile
Open vbsFileName For Binary Access Write As #FileNo
Put #FileNo, , vbsText
Close #FileNo
' Charge the scheduled task
Shell "AT " & Format(Now + TimeSerial(0, 1, 0), "hh:mm") & " /INTERACTIVE """ & vbsFileName & """"
' Close all workbooks and quit
'For Each Wb In Application.Workbooks: Wb.Close: Next
'Application.Quit
End Sub
' Macro for calling from VBS
' Pay attention on SYSTEM UserName used by VBS
Sub MyMacro()
MsgBox "Hi from " & Application.UserName & "!"
End Sub</FONT>