Tutorial 15 Automation and Error Handling For Automation please refer to the notes in the Concept lesson for this tutorial. Error Handling In this tutorial selected errors that would usually cause an application to halt are taken care of through code within the procedures/functions. Tutorial 14 introduces the CreateObject method that will start another application from an existing application (starting Access while in an Excel procedure). This process could result in several instances of Access being started if the procedure is called several times. Using GetObject, instead of CreateObject, will allow the existing Access application to be reused rather than starting a new one. The problem is that GetObject will return an error and halt the program if there is not another Access application running. Is there another instance of the Server (second) application already running? Rather than just creating an object, we can call GetObject(Class := …..) This function will return the address of an existing instance of the Server application. Unfortunately if the second application is not already running the function returns an Error, and the procedure is halted. Intercepting the Error returned by GetObject caused when an existing instance of the class is not found. A run-time error occurs when the procedure is running. We have seen our procedures halted many times by syntax errors. The type of error is held in an object called: Err object. We can trap this error before it halts the procedure. In this case, when GetObject returns an error we can trap the error, and call CreateObject to create a new object. When an error occurs, if it is trappable, the value retuned to Err object will allow the code to recover from the error, rather than having the error halt the procedure. In the case of GetObject used when the application is already running, the number 429 is returned to Err.Number, and Err.Description = "ActiveX component can't create object" are both set. Error Handling The statement On Error placed in the code prior to GetObject will turn on the error handling feature. This will make it possible to trap the error caused by the Server application already having been created when GetObject was called. Here is the syntax: On Error Goto <line> , where <line> is a label of a line in the procedure The Resume [ next | line ] statement is used to return to the procedure after the error has been handled. Here is the entire code that will call GetObject. If there is an error it will call CreateObject, then continue processing: 'turn on error trapping On Error GoTo NewWorkbookErrHandler 'declare object variable Dim appExcel as Excel.Application 'assign Automation object's address Set appExcel = GetObject(Class := "Excel.Application") 'add a new workbook appExcel.Workbook.Add 'make if visible Here: appExcel.Visible = True 'release memory Set appExcel = Nothing Exit Sub 'Exit the subroutine NewWorkbookErrHandler 'Err.Number will hold the number of the error generated Select Case Err.Number Case 429 'Automation does not exist, Create one Set appExcel = _ CreateObject(Class:="Excel.Application") Resume Next 'continue on line after GetObject Case Else ' Unforeseen Error MsgBox Prompt := "Please record number and info. " & _ Err.Description & " " & Err.Number, _ buttons:=vbOKOnly + vbInformation Resume Here ' go to line labeled Here End Select End Sub