Excel Application Object

The Application object represents the Excel application itself and is the root object for other objects like Workbook, Worksheet, and Range.

The Application Object

To use the Excel object model, you first need to create an Application object.

Excel VBA In Excel VBA, Application directly refers to the Application object and does not require explicit creation. In many cases, it can be omitted.

To create an Application object in other applications (e.g., Word, PowerPoint), use:

code.vba
Dim App As Object
Set xl = CreateObject("Excel.Sheet")
Set App = xl.Application

Python xlwings When using the xlwings package, the top-level function App() creates an App object:

code.python
>>> import xlwings as xw
>>> app = xw.App()
>>> app2 = xw.App()

To check the number of workbooks in app:

Excel VBA

code.vba
Application.Workbooks.Count

Or omit Application:

code.vba
Workbooks.Count

Python xlwings

code.python
>>> app.books.count

Python xlwings API

code.python
>>> app.api.Workbooks.Count

To activate app2 (making it the current application) using the Python xlwings approach:

code.python
>>> app2.activate()

The Apps object is a collection of all App objects:

code.python
>>> import xlwings as xw
>>> xw.apps

Use the add() method to create a new application (automatically activated):

code.python
>>> xw.apps.add()

Use the active attribute to return the active application:

code.python
>>> xw.apps.active

Use the count attribute to return the number of applications:

code.python
>>> xw.apps.count

Each Excel application has a unique PID (Process ID). Use the keys() method to retrieve all PIDs as a list:

code.python
>>> pid = xw.apps.keys()
>>> pid
[3672, 4056]

Reference a specific application using its PID (e.g., get the caption of the first application):

code.python
>>> xw.apps[pid[0]].api.Caption
'Workbook1 - Excel'

Note: For the Python xlwings approach, the App object lacks a Caption attribute; use the API syntax instead.

Use the kill() method to force an Excel application to exit by terminating its process:

code.python
>>> app.kill()

Use the Quit() method to exit the application without saving any workbooks:

Excel VBA

code.vba
Application.Quit

Python xlwings

code.python
>>> app.quit()

Position, Size, Caption, Visibility, and State Attributes

Each Excel application is a graphical window. The following attributes relate to the window:

Position: Left and Top define the coordinates of the window's top-left corner. Excel VBA:

code.vba
Application.Left   '30.25
Application.Top    '95.25

Python xlwings API:

code.python
>>> app.api.Left
30.25
>>> app.api.Top
95.25

Size: Width and Height define the window's width and height. Excel VBA:

code.vba
Application.Width   '635.25
Application.Height   '390.0

Python xlwings API:

code.python
>>> app.api.Width
635.25
>>> app.api.Height
390.0

Caption: The window title. Excel VBA:

code.vba
Application.Caption  'Workbook1 - Excel'

Python xlwings API:

code.python
>>> app.api.Caption
'Workbook1 - Excel'

Visibility: Visible returns or sets whether the window is visible (True = visible, False = hidden). Excel VBA:

code.vba
Application.Visible  'True

Python xlwings API:

code.python
>>> app.api.Visible
True

State: WindowState defines the window state: minimized (xlMinimized, -4140), maximized (xlMaximized, -4137), or normal (xlNormal, -4143). Excel VBA:

code.vba
Application.WindowState  '-4143

Python xlwings API:

code.python
>>> app.api.WindowState
-4143

To maximize the window:

Excel VBA

code.vba
Application.WindowState = xlMaximized

Python xlwings API

code.python
>>> app.api.WindowState = xw.constants.WindowState.xlMaximized

Other Common Attributes

1. Screen Updating

Refreshing the graphical interface (redrawing) takes time. Setting ScreenUpdating to False disables redrawing, improving script speed. Reset to True after operations.

Excel VBA

code.vba
Application.ScreenUpdating = False

Python xlwings

code.python
>>> app.screen_updating = False

2. Display Alerts

To avoid interruptions from warning dialogs during automation, set DisplayAlerts to False. Reset to True after tasks.

Excel VBA

code.vba
Application.DisplayAlerts = False

Python xlwings

code.python
>>> app.display_alerts = False

3. Calling Worksheet Functions

Excel worksheet functions are powerful. Use the WorksheetFunction property to call them. For example, use CountIf to count values >8 in range B2:F5 (Figure 1-1).

Document Image

Figure 1-1

Excel VBA

code.vba
WorksheetFunction.CountIf(Range("B2:F5"), ">8")    'Result: 7.0

Python xlwings API

code.python
>>> app.api.WorksheetFunction.CountIf(app.api.Range('B2:F5'), '>8')
7.0