• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 25. Automation: Communicating wi... > Closing an Automation Object

Closing an Automation Object

After the user clicks the CloseExcel command button, the CloseExcel subroutine is called, as shown in Listing 25.4. The CreateExcelObj routine determined whether the user was running Excel prior to launching your application. When the CloseExcel routine runs, it prompts the user to close Excel only if the public variable gbExcelRunning indicates that Excel was not running prior to your application. Otherwise, it prompts the user, warning that he or she must close Excel.

Listing 25.4. The CloseExcel routine.

Sub CloseExcel()
    On Error GoTo CloseExcel_Err
    Dim intAnswer As Integer
    Dim objWK As Object

    'Attempt to point to an active workbook
    Set objWK = gobjExcel.ActiveWorkbook
    'If Excel is Still Running and was NOT running before
    'this application executed it, prompt user to close
    If Not gbExcelRunning Then
        intAnswer = MsgBox("Do You Want to Close Excel?", vbYesNo)
        If vbYes Then
            objWK.Close False
            gobjExcel.Quit
        End If
    Else
        MsgBox "Excel Was Running Prior to This Application." & Chr(13) _
             & "Please Close Excel Yourself."
        gobjExcel.Visible = True
    End If
CloseExcel_Exit:
    Set gobjExcel = Nothing
    Set objWK = Nothing
    Exit Sub

CloseExcel_Err:
    MsgBox "Error # " & Err.Number & ": " & Err.Description
    Resume CloseExcel_Exit
End Sub

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint