msfz751

MOpenClose

May 19th, 2014
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' MOpenClose
  2. ' Description:  This module contains the application startup and shutdown code.
  3. '               It must not contain any code that won't compile in old versions,
  4. '               nor anything that calls external object models that might not be
  5. '               installed on the user's computer
  6. '
  7. ' Authors:      Rob Bovey, www.appspro.com
  8. '               Stephen Bullen, www.oaltd.co.uk
  9. '
  10. ' Chapter Change Overview
  11. ' Ch#   Comment
  12. ' --------------------------------------------------------------
  13. ' 05    Initial version
  14. '
  15. Option Explicit
  16. Option Private Module
  17.  
  18. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  19. ' Comments: This routine is run every time the application is
  20. '           opened. It initializes the application.
  21. '
  22. ' Arguments:    None
  23. '
  24. ' Date          Developer       Chap    Action
  25. ' --------------------------------------------------------------
  26. ' 06/01/08      Rob Bovey       Ch05    Initial version
  27. '
  28. Public Sub Auto_Open()
  29.  
  30.     Dim wkbBook As Workbook
  31.  
  32.     ' The very first thing your application should do upon
  33.    ' startup is attempt to delete any copies of its
  34.    ' command bars that may have been left hanging around
  35.    ' by an Excel crash or other incomplete exit.
  36.    On Error Resume Next
  37.         Application.CommandBars(gsBAR_TOOLBAR).Delete
  38.     On Error GoTo 0
  39.    
  40.     ' Initialize global variables.
  41.    InitGlobals
  42.    
  43.     ' Make sure we can locate our time entry workbook before we
  44.    ' do anything else.
  45.    If Len(Dir$(gsAppDir & gsFILE_TIME_ENTRY)) > 0 Then
  46.        
  47.         Application.ScreenUpdating = False
  48.         Application.StatusBar = gsSTATUS_LOADING_APP
  49.        
  50.         ' Build the command bars.
  51.        BuildCommandBars
  52.        
  53.         ' Determine if the time entry workbook is already open.
  54.        ' If not, open it. If so, activate it.
  55.        On Error Resume Next
  56.         Set wkbBook = Application.Workbooks(gsFILE_TIME_ENTRY)
  57.         On Error GoTo 0
  58.        
  59.         If wkbBook Is Nothing Then
  60.             Set wkbBook = Application.Workbooks.Open( _
  61.                             gsAppDir & gsFILE_TIME_ENTRY)
  62.         Else
  63.             wkbBook.Activate
  64.         End If
  65.        
  66.         ' Make the worksheet settings for the time entry
  67.        ' workbook
  68.        MakeWorksheetSettings wkbBook
  69.        
  70.         ' Reset critical application properties.
  71.        ResetAppProperties
  72.    
  73.     Else
  74.         MsgBox gsERR_FILE_NOT_FOUND, vbCritical, gsAPP_NAME
  75.         ShutdownApplication
  76.     End If
  77.  
  78. End Sub
  79.  
  80.  
  81. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  82. ' Comments: This routine runs automatically every time the
  83. '           application workbook is closed. If the application
  84. '           shutdown code is not already running (as the result
  85. '           of a call from the Exit button), this procedure
  86. '           calls the application shutdown procedure.
  87. '
  88. ' Arguments:    None
  89. '
  90. ' Date          Developer       Chap    Action
  91. ' --------------------------------------------------------------
  92. ' 06/01/08      Rob Bovey       Ch05    Initial version
  93. '
  94. Public Sub Auto_Close()
  95.     ' Call standard shutdown code if it isn't already running.
  96.    If Not gbShutdownInProgress Then ShutdownApplication
  97. End Sub
  98.  
  99.  
  100. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  101. ' Comments: This routine shuts down the application.
  102. '
  103. ' Arguments:    None
  104. '
  105. ' Date          Developer       Chap    Action
  106. ' --------------------------------------------------------------
  107. ' 06/01/08      Rob Bovey       Ch05    Initial version
  108. '
  109. Public Sub ShutdownApplication()
  110.  
  111.     ' Ignore any errors on application shutdown.
  112.    On Error Resume Next
  113.    
  114.     ' This flag prevents this procedure from being called a
  115.    ' second time by Auto_Close if it has already been called
  116.    ' by the ExitApplication procedure.
  117.    gbShutdownInProgress = True
  118.  
  119.     ' Delete command bar.
  120.    Application.CommandBars(gsBAR_TOOLBAR).Delete
  121.    
  122.     ' Close the time entry workbook, allowing the user to
  123.    ' save changes.
  124.    Application.Workbooks(gsFILE_TIME_ENTRY).Close
  125.    
  126.     ' If there are no workbooks left open, quit Excel
  127.    ' Otherwise just close this workbook.
  128.    If lCountVisibleWorkbooks() = 0 Then
  129.         ThisWorkbook.Saved = True
  130.         Application.Quit
  131.     Else
  132.         ThisWorkbook.Close False
  133.     End If
  134.    
  135. End Sub
Advertisement
Add Comment
Please, Sign In to add comment