View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Closing running sub from a different one

You need to make SH a public variable so you can shut down. Then adding
set SH = Nothing in the before close routine.


Dim SH As IWshRuntimeLibrary.WshShell

Public Sub CloseMe()
Dim Res As Long

Set SH = New IWshRuntimeLibrary.WshShell
Res = SH.Popup(Text:="Are you still there? Please click Yes or this file
will close in 2 minutes", secondstowait:=2, _
Title:="Active", Type:=vbYesNo)
If Res = vbYes Then
Application.OnTime Now() + TimeValue("00:00:50"), "CloseMe"
Else
Sheets("Dept. a").Visible = xlSheetVeryHidden
Sheets("Dept. b").Visible = xlSheetVeryHidden
Sheets("Dept. c").Visible = xlSheetVeryHidden
Sheets("ALL DEPARTMENT GRAPHS").Visible = xlSheetVeryHidden
Sheets("MACROS").Select
ThisWorkbook.Save
ThisWorkbook.Close
End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Application.ScreenUpdating = False
Sheets("Dept. a").Visible = xlSheetVeryHidden
Sheets("Dept. b").Visible = xlSheetVeryHidden
Sheets("Dept. c").Visible = xlSheetVeryHidden
Sheets("ALL DEPARTMENT GRAPHS").Visible = xlSheetVeryHidden
Sheets("MACROS").Select
Application.ScreenUpdating = True
ThisWorkbook.Save
!!!!!!!!!STOP THE CLOSEME SUB!!!!!!!!
set SH = Nothing
ThisWorkbook.Close
End Sub


"jtfalk" wrote:

I have a sub that starts a timmer and checks to see if you are there or
closes the workbook. That works great. if I close the workbook the old
fashioned way ythe CloseMe sub is still running and opens the workbook back
up. I need to put something in Sub Workbook_BeforeClose to stop the Sub
CloseMe(). I posted this earlier but it was difficult to understand waht i
was saying. i hope this is easier.

Sub Auto_Open()
LogInformation ThisWorkbook.Name & " opened by " & Application.UserName &
" " & Format(Date, "yyyy-mm-dd") & " " & Format(Time, "hh:mm")
Application.OnTime Now() + TimeValue("00:00:50"), "CloseMe"
End Sub




Public Sub CloseMe()
Dim SH As IWshRuntimeLibrary.WshShell
Dim Res As Long

Set SH = New IWshRuntimeLibrary.WshShell
Res = SH.Popup(Text:="Are you still there? Please click Yes or this file
will close in 2 minutes", secondstowait:=2, _
Title:="Active", Type:=vbYesNo)
If Res = vbYes Then
Application.OnTime Now() + TimeValue("00:00:50"), "CloseMe"
Else
Sheets("Dept. a").Visible = xlSheetVeryHidden
Sheets("Dept. b").Visible = xlSheetVeryHidden
Sheets("Dept. c").Visible = xlSheetVeryHidden
Sheets("ALL DEPARTMENT GRAPHS").Visible = xlSheetVeryHidden
Sheets("MACROS").Select
ThisWorkbook.Save
ThisWorkbook.Close
End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Application.ScreenUpdating = False
Sheets("Dept. a").Visible = xlSheetVeryHidden
Sheets("Dept. b").Visible = xlSheetVeryHidden
Sheets("Dept. c").Visible = xlSheetVeryHidden
Sheets("ALL DEPARTMENT GRAPHS").Visible = xlSheetVeryHidden
Sheets("MACROS").Select
Application.ScreenUpdating = True
ThisWorkbook.Save
!!!!!!!!!STOP THE CLOSEME SUB!!!!!!!!
ThisWorkbook.Close
End Sub