+ Reply to Thread
Results 1 to 4 of 4

My workbook keeps opening by itself!

Hybrid View

  1. #1
    Registered User
    Join Date
    04-30-2013
    Location
    Cranfield
    MS-Off Ver
    Excel 2007
    Posts
    68

    My workbook keeps opening by itself!

    Hi all,

    I have just added some code to make my workbook close after it has been inactive for 5 minutes...this works fine except it opens itself up again after 5 minutes.
    If I select 'Enable Macros' it closes immediately and if not, it will stay open.

    Here is all of the code I put in for the auto shut down:
    ThisWorkbook:
    Private Sub Workbook_Open()
        dTime = Time
        On Error Resume Next
        Application.OnTime dTime + TimeValue("00:05:00"), "CloseMe"
        On Error GoTo 0
    End Sub
    
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        On Error Resume Next
        Application.OnTime dTime + TimeValue("00:05:00"), "CloseMe", , False
        dTime = Time
        Application.OnTime dTime + TimeValue("00:05:00"), "CloseMe"
        On Error GoTo 0
    End Sub
    Module1:
    Public dTime As Date
    
    Sub CloseMe()
        Application.StatusBar = "Inactive File Closed: " & ThisWorkbook.Name
        ThisWorkbook.Close SaveChanges:=True
    End Sub
    
    Sub Message_Close()
    Response = MsgBox("This file will automatically save and close after 5 minutes of inactivity", vbInformation, "This is a message")
    End Sub
    Any ideas what is going on?

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,467

    Re: My workbook keeps opening by itself!

    It has to do with "Application.OnTime". That command starts the Windows timer so your macro knows when the five minutes has been reached. What is needed is to stop the timer with syntax resembling ...

    "Application.OnTime EarliestTime:=Now + TimeValue("00:05:00"), Procedure:=AutoSave, Schedule:=False"
    The "Schedule:=False" being the key.

    However, I am experiencing the same issue with my workbook and have been unable to get the above to actually work. A search of the Forum here will realize most folks are using : See http://www.cpearson.com/excel/ontime.aspx for understanding how to stop the timer. Except I am one of those folks who can't wrap my mind around the syntax to get it working in on my workbook.

    If you manage to get your project to function as desired, please give my post a go :
    http://www.excelforum.com/excel-prog...e-display.html

    Thanks !

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: My workbook keeps opening by itself!

    James

    You seem to be using OnTime quite a lot in that code.

    The only thing you don't do is disable it when you close the workbook.

    Doing that should stop the workbook re-opening.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    04-30-2013
    Location
    Cranfield
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: My workbook keeps opening by itself!

    Thanks for your advice.

    I added this macro to stop it opening and closing...seems to work so far!
    Sub End_Timer_Data()
    
    Application.OnTime EarliestTime:=dTime + TimeValue("00:05:00"), _
    Procedure:="CloseMe", Schedule:=False
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1