+ Reply to Thread
Results 1 to 3 of 3

Close & save a file at set time only IF the file is open

Hybrid View

  1. #1
    Registered User
    Join Date
    03-17-2006
    Posts
    47

    Close & save a file at set time only IF the file is open

    I'm trying to have a file auto-Close (with a Save) at a set time every day IF it's open at that time.
    To do this, I've put the following code in 'ThisWorkbook':

    Private Sub Workbook_Open()
    Application.OnTime TimeValue("17:15:00"), "TimeMacro"
    End Sub


    And I've put this in Module 1:

    Sub TimeMacro()
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    End Sub


    That works fine in that if the file is open, the file is automatically saved and closed. However if the file is NOT open at the set time (17:15) and Excel is running, Excel tries to re-open the file (gives me the Enable macro message) to run the macro, and I want to avoid this. I understand why the macro is doing what it's doing, but how can I get Excel to not run TimeMacro when the file is closed.
    Many thanks,
    Clive

  2. #2
    Tom Ogilvy
    Guest

    RE: Close & save a file at set time only IF the file is open

    You would need to cancel the scheduled event in the BeforeClose event.
    However, the user could possibly cancel when prompted to save and the
    workbook is never closed.

    If this is for you own use, you could put the macro in the personal.xls and
    run it from there. Then have the macro check for the workbook being open

    http://www.cpearson.com/excel/ontime.htm

    for example of cancelling an ontime event.

    http://www.cpearson.com/excel/events.htm for an overview of events.

    --
    Regards,
    Tom Ogilvy


    "Clivey_UK" wrote:

    >
    > I'm trying to have a file auto-Close (with a Save) at a set time every
    > day IF it's open at that time.
    > To do this, I've put the following code in 'ThisWorkbook':
    >
    > Private Sub Workbook_Open()
    > Application.OnTime TimeValue("17:15:00"), "TimeMacro"
    > End Sub
    >
    > And I've put this in Module 1:
    >
    > Sub TimeMacro()
    > Application.DisplayAlerts = False
    > ActiveWorkbook.Close
    > End Sub
    >
    > That works fine in that if the file is open, the file is automatically
    > saved and closed. However if the file is NOT open at the set time
    > (17:15) and Excel is running, Excel tries to re-open the file (gives me
    > the Enable macro message) to run the macro, and I want to avoid this. I
    > understand why the macro is doing what it's doing, but how can I get
    > Excel to not run TimeMacro when the file is closed.
    > Many thanks,
    > Clive
    >
    >
    > --
    > Clivey_UK
    > ------------------------------------------------------------------------
    > Clivey_UK's Profile: http://www.excelforum.com/member.php...o&userid=32569
    > View this thread: http://www.excelforum.com/showthread...hreadid=537788
    >
    >


  3. #3
    Registered User
    Join Date
    03-17-2006
    Posts
    47

    ONTime Event

    Tom,
    Many thanks for that. Looks like just what I need. I should have thought of using BeforeClose.
    Clive

    Quote Originally Posted by Tom Ogilvy
    You would need to cancel the scheduled event in the BeforeClose event.
    However, the user could possibly cancel when prompted to save and the
    workbook is never closed.

+ 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