extremecorvette
New Member
- Joined
- Jun 2, 2011
- Messages
- 20
Question: Macro to save and close workbook
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o> </o>
This is what I have come up with, I have found three issues with it.
Running Excel 2003 with Windows 7
<o> </o>
<o> </o>
Private Sub Workbook_Open()
<o> </o>
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 600
If TimeInMinutes > 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
Start = Timer
Do While Timer < Start + TotalTimeInMinutes
DoEvents
<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-comffice:smarttags" /><st1lace w:st="on">Loop</st1lace>
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "This file has been open for " & TotalTime / 60 & " minutes. You have 5 minutes to save before Excel closes."
End If
<o> </o>
Start = Timer
Do While Timer < Start + (5 * 60)
DoEvents
<st1lace w:st="on">Loop</st1lace>
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "Excel will now close."
Application.Quit
ActiveWorkbook.Close True
' closes the active workbook and saves any changes
End Sub
<o> </o>
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o> </o>
This is what I have come up with, I have found three issues with it.
- One it closes all the workbooks I have open and only saves the one that the macro is in.
- Two if I put this macro in more than one workbook I can’t have both open at the same time.
- Three I got the missage to come up You have 5 minutes to save before Excel closes. But if no one clicks that message the workbook stays open.
Running Excel 2003 with Windows 7
<o> </o>
<o> </o>
Private Sub Workbook_Open()
<o> </o>
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 600
If TimeInMinutes > 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
Start = Timer
Do While Timer < Start + TotalTimeInMinutes
DoEvents
<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-comffice:smarttags" /><st1lace w:st="on">Loop</st1lace>
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "This file has been open for " & TotalTime / 60 & " minutes. You have 5 minutes to save before Excel closes."
End If
<o> </o>
Start = Timer
Do While Timer < Start + (5 * 60)
DoEvents
<st1lace w:st="on">Loop</st1lace>
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "Excel will now close."
Application.Quit
ActiveWorkbook.Close True
' closes the active workbook and saves any changes
End Sub
<o> </o>