O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required