Access.2007.VBA.Bibl..

(John Hannent) #1
Create and open a new, blank workbook:

bks.Add

Make the workbook visible:

appExcel.Application.Visible = True

ErrorHandlerExit:
Exit Sub

ErrorHandler:
If Err = 429 Then

Excel is not running; open Excel with CreateObject:

Set appExcel = CreateObject(“Excel.Application”)
Resume Next
Else
MsgBox “Error No: “ & Err.Number & “; Description: “ & _
Err.Description
Resume ErrorHandlerExit
End If

End Sub

The procedures in this section can be run from macros; each procedure has a macro
whose name is mcrplus the procedure name.

The error handler in the preceding procedure is similar to the one used in the Word Automation
code in Chapter 6; sets a reference to the current Excel instance if Excel is running and otherwise
creates a new Excel Application instance using the CreateObjectfunction from a line in the
procedure’s error handler. If you run the CreateNewWorkbookprocedure repeatedly, each time it
runs a new workbook is created within the Excel window, named Book1, Book2, and so forth, as
shown in Figure 7.5.

NOTENOTE


Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf