Automatically close document.

R

Richhall

Hi, we have a control document which a number of users update on
Fridays. The problem is sometimes people stay in the document
preventing other people updating it. Is there a way I could create a
script, that once a person opens a document they have 15 minutes
within the document or it will automatically close and save, allowing
someone else to then use the document. In doing this, I would also
want a message box that says a message when the document is open, and
then with 1 minute to go also can display a message.

Cheers

Rich
 
G

Greg Maxey

Rich,

Just a stab. I didn't check to see what happens if the user is away
and doesnt' acknowledge the message box:

Sub Auto_Open()
Application.OnTime When:=Now + TimeValue("00:14:00"), Name:="MsgText"
End Sub
Sub MsgText()
MsgBox "This document will save and close in 60 seconds."
Application.OnTime When:=Now + TimeValue("00:01:00"),
Name:="myFileSave"
End Sub
Sub myFileSave()
ActiveDocument.Close wdSaveChanges
End Sub
 
G

Greg Maxey

Rich,

That won't work. The code stops when the message box is diplayed and
doesn't continue until the user clicks OK. Also the Sub Auto_Open
should be Sub AutoOpen

I toy around and see if I can come up with something that works.
 
R

Richhall

Rich,

That won't work. The code stops when the message box is diplayed and
doesn't continue until the user clicks OK. Also the Sub Auto_Open
should be Sub AutoOpen

I toy around and see if I can come up with something that works.







- Show quoted text -

Cheers everyone! Even a message box without an OK, is fine. Just a
notice really.
 
G

Greg Maxey

Ok, you can use a UserForm timed to close as the warning message.

Put this code in the document project module:

Sub AutoOpen()
Application.OnTime When:=Now + TimeValue("00:14:00"), Name:="MsgText"
End Sub
Sub MsgText()
UserForm1.Show
Application.OnTime When:=Now + TimeValue("00:01:00"),
Name:="myFileSave"
End Sub
Sub myFileSave()
ActiveDocument.Close wdSaveChanges
End Sub

Add a userform with a caption and label to suit your needs and add
this code to the Userform:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As
Long)
Private Sub UserForm_Activate()
DoEvents
'Set the display time in milliseconds
Sleep 1000
Unload Me
End Sub
 
R

Richhall

Thanks, Sorry not a VB expert or anything. So I go to
Tools>Macros>VB Editor and under projects put the first bit in the
This Document panel and save, What do I need to do with the second
bit please?
 
G

Greg Maxey

Rich,

You will need to open the VB Editor (Alt+F11). If not already
displayed, display the Project Explorer and Properties windows using
the View menu.

Click on the Project(your document name) tree then use the Insert menu
to insert a module

Paste the first bit of code in the module

Use the Insert menu to insert a Userform.

Use the toolbox to place a lable on the userform and then use the
properties window to give the userform and the label a "Caption"

Right click the Userform and select view code. Paste the second bit
of code in.
 
R

Richhall

Rich,

You will need to open the VB Editor (Alt+F11). If not already
displayed, display the Project Explorer and Properties windows using
the View menu.

Click on the Project(your document name) tree then use the Insert menu
to insert a module

Paste the first bit of code in the module

Use the Insert menu to insert a Userform.

Use the toolbox to place a lable on the userform and then use the
properties window to give the userform and the label a "Caption"

Right click the Userform and select view code. Paste the second bit
of code in.



- Show quoted text -

Cheers Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top