Microsoft Office 2000/Visual Basic Programmer's Guide |
The Excel Application object is the top-level object in Excel's object model. You use the Application object to determine or specify application-level properties or execute application-level methods. The Application object is also the entry point into the rest of the Excel object model.
When you work with properties and methods of the Application object by using VBA from within Excel, the Application object is available to you by default. This is known as an implicit reference to the object. If you work with Excel objects from another Office application, you must create an object variable representing the Excel Application object. This is known as an explicit reference to the object. For example, the following two procedures return the name of the currently active Worksheet object. The ShowNameFromInsideXL procedure is designed to work from within Excel and uses an implicit reference to the Application object. In other words, it references the ActiveSheet property of the Application object without explicitly referencing the Application object itself. The ShowNameFromOutsideXL procedure is designed to be run from outside Excel and so must use an explicit reference to the Application object.
Sub ShowNameFromInsideXL()
MsgBox "'" & ActiveSheet.Name & "' is the currently active worksheet."
End Sub
Sub ShowNameFromOutsideXL()
Dim xlApp As Excel.Application
Const XL_NOTRUNNING As Long = 429
On Error GoTo ShowName_Err
Set xlApp = GetObject(, "Excel.Application")
MsgBox "'" & ActiveSheet.Name & "' is the currently active worksheet."
xlApp.Quit
Set xlApp = Nothing
ShowName_End:
Exit Sub
ShowName_Err:
If Err = XL_NOTRUNNING Then
' Excel is not currently running.
Set xlApp = New Excel.Application
xlApp.Workbooks.Add
Resume Next
Else
MsgBox Err.Number & " - " & Err.Description
End If
Resume ShowName_End
End Sub
The ShowNameFromInsideXL and ShowNameFromOutsideXL procedures are available in the modExcelSamples module in ExcelSamples.xls in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.