Automatically close idle document using

Hello,

We have a word document at work that gets edited/updated by many ppl, problem is sometimes ppl forget it opened and others cant edit it

I needed to put a vba code in this particular document to save & close document if selection does not change for 5 minutes even if user is using the computer for other purposes.

Thanks in advance
Answer
Answer
Install this code in a module in the document, and save the document as a "Microsoft Word Macro-Enabled Document (*.docm)". See http://www.gmayor.com/installing_macro.htm if needed.

Const interval As Double = 5 ' minutes
Dim selStart As Long
Dim selEnd As Long

Sub AutoOpen()
    selStart = 0
    selEnd = 0
    Application.OnTime When:=DateAdd("n", interval, Now), Name:="CheckStatus", Tolerance:=120
End Sub

Sub CheckStatus()
    If Selection.Start <> selStart Or Selection.End <> selEnd Then
        selStart = Selection.Start
        selEnd = Selection.End
        Application.OnTime When:=DateAdd("n", interval, Now), Name:="CheckStatus", Tolerance:=120
    Else
        ThisDocument.Close SaveChanges:=wdSaveChanges
    End If
End Sub

When the document is first opened, it starts a timer. After 5 minutes (or whatever number of minutes you assign to the constant named 'interval'), the CheckStatus macro will run. At that time, if the selection has changed since the document was opened (or since the previous time the CheckStatus macro ran), the selection's location is updated and the timer is restarted. If the selection is still in the same place, the document will be saved and closed without notice.

In practice, there could be a couple of 'unintended consequences'. One is that this code doesn't care whether the selection has moved away from its previous location and then back to the same spot (most likely the beginning of the document, start = 0 and end = 0), so it could close the document prematurely. Another is that the same careless person might notice immediately that the document has disappeared, and reopen it before anyone else gets a chance. I don't think much can be done about either of these possibilities.

If you can replace Word 2007 with Word 2013, it allows simultaneous editing of a document by multiple people. Each user's edits lock only the paragraph(s) that they're editing, and those locks are released when that user saves (and everyone else is notified to refresh).
_____________________________
https://jay-freedman.info

2 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer
I can't think of any common situation that wouldn't also give you some sort of message before removing the module from the file or otherwise blocking macros. For example:
  • If you save a macro-containing document as a .docx, first you get a message telling you that the macros will be discarded, and offering a Cancel button.
  • If you save it as a .docm in an untrusted location, the macros will be there but will be disabled, and one of several types of messages will tell you so.
  • If your macro security options are set to disable unsigned macros, again you'll get a message (or, at the High setting, they'll just fail to run), but the macros will be visible in the editor.

Along the lines of "is it plugged in?", I have to ask whether you're certain that the file you opened is in the same location as the one you saved. Try again, and check the time stamp on the file.


The only other thing I can think of, though I've never seen it, is if your network at work has some really aggressive antivirus software that watches for macros and strips them out. Are you saving the file to your local drive, or to a network drive or SharePoint server?

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated May 9, 2024 Views 5,309 Applies to: