Solved

Excel workbook auto save/close after inactivity

Posted on 2012-03-15
8
1,438 Views
Last Modified: 2012-03-16
Hello:

I would like a macro that runs in the back ground in excel that would save the workbook and close it after a given time frame if there was no activity by the user.
0
Comment
Question by:DavidH7470
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Expert Comment

by:costaf
ID: 37725251
hi, there. if i´m not mistaken there is an option in Excel 2010 Save options that allows you to auto save  every x minutes , and it saves your workbook automaticly , even if you close the document without saving it.
0
 
LVL 20

Accepted Solution

by:
netcmh earned 500 total points
ID: 37725252
0
 
LVL 15

Expert Comment

by:jerseysam
ID: 37725261
Dim DownTime As Date


Sub SetTime()
DownTime = Now + TimeValue("00:00:20")
Application.OnTime DownTime, "ShutDown"
End Sub


Sub ShutDown()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub


Sub Disable()

On Error Resume Next
Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", _
Schedule:=False
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DavidH7470
ID: 37725352
Hi Jerseysam

That works to a point.  But it closes even as a user is entering information.  I was looking for it close it after an interval of inactivity.

Thanks
0
 
LVL 20

Expert Comment

by:netcmh
ID: 37725370
The link I provided has the inactivity close segment.

Private Sub Workbook_Open()
MsgBox "This workbook will auto-close after 20 seconds of inactivity"
Call SetTime
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Disable
End Sub


Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call Disable
Call SetTime
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target _
As Excel.Range)
Call Disable
Call SetTime
End Sub
0
 
LVL 20

Expert Comment

by:netcmh
ID: 37725372
the module would have:

Dim DownTime As Date


Sub SetTime()
DownTime = Now + TimeValue("00:00:20")
Application.OnTime DownTime, "ShutDown"
End Sub


Sub ShutDown()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub


Sub Disable()

On Error Resume Next
Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", _
Schedule:=False
End Sub
0
 
LVL 3

Expert Comment

by:DaFranker
ID: 37725455
To use Jerseysam's solution, add this code in ThisWorkbook:

Private Sub Workbook_Open()
    'Remove the single-quote at the start of the line below to give a warning to the user.
    'MsgBox "This workbook will auto-close after 20 seconds of inactivity"
    Call SetTime
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call Disable
End Sub


Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call Disable
    Call SetTime
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
    Call Disable
    Call SetTime
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target _
As Excel.Range)
    Call Disable
    Call SetTime
End Sub

Open in new window

0
 
LVL 20

Expert Comment

by:netcmh
ID: 37729061
Thank you for the grade. Good luck.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question