Share via


Understanding the Excel Application Object

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The Microsoft® 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 Microsoft® Visual Basic® for Applications (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, then 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

See Also

Working with Microsoft Excel Objects | Shortcuts to Active Objects | Understanding the Workbook Object | Understanding the Worksheet Object | Understanding the Range Object