The Task Object

A Task object represents a currently running Windows application (not just Microsoft Word). Task objects are kept in the Tasks collection.

One of the more useful properties of a Task object is the Name property. For instance, the following code prints (to the Immediate window) the names of all of the currently running tasks:

	Dim t as Task
	For Each t in Tasks
	   Debug.Print t.Name
	Next t

You might find it amusing to run this code on your system. You will probably see some rather bizarre looking tasks.

On a more useful note, the Tasks collection has a method called Exists that indicates whether a certain task is running. For instance, the following code determines whether Microsoft Excel is running. If so, the code activates Excel and maximizes it; if not, the code starts Excel using the VBA Shell function, whose purpose is to run an executable program (the path is correct for my PC):

	If Tasks.Exists("Microsoft Excel") = True Then
	    Tasks("Microsoft Excel").Activate
	    Tasks("Microsoft Excel").WindowState = wdWindowStateMaximize
	Else
	    Shell "I:\Office97\Excel\Excel.exe", vbMaximizedFocus
	End If

Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.