I am trying to create a macro that will auto close and save an Excel workbook if it has been open for a certain period of time. Modifying some code I found on Microsoft’s website, I have:
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
StartTimer
End Sub
Const idleTime = 30
Dim Start as Long
Sub StartTimer()
Start = Timer
Do While Timer < Start + idleTime
DoEvents
Loop
Application.DisplayAlerts = False
ActiveWorkbook.Close True
Application.DisplayAlerts = True
End Sub
However, this throws an error when the loop ends (after 30 seconds have elapsed). At the line with Application.DisplayAlerts = False it gives me an error BASIC runtime error Object variable not set If I comment out the Application line, it gives me the same error with the ActiveWorkbook line. I then tried to make a macro that was just a call to Msgbox ActiveWorkbook.name and this gave me the same error. I have never used VBA before, so I get the feeling this is a stupidly obvious bug, but SO assistance would be appreciated.
It was in fact a simple bug, a bit more googling resolved for me.
I am testing in OpenOffice, which uses a different API than Microsoft starting with OO 3.3. 3.2, which I was using ref docs for, supported ActiveWorkbook, while 3.3 requires ThisComponent instead of ActiveWorkbook