Class Task (Word VBA)

The class Task represents a single task running on the system. The Task object is a member of the Tasks collection. The Tasks collection includes all the applications that are currently running on the system.

The main procedures of class Task are Activate and Close

Set

To use a Task class variable it first needs to be instantiated, for example

Dim tsk as Task
Set tsk = Tasks(Index:=1)

The following procedures can be used to set variables of type Task: Tasks.Item and Application.Tasks

For Each

Here is an example of processing the Task items in a collection.

For Each tsk In Tasks 
 If InStr(tsk.Name, "Microsoft Excel") > 0 Then 
     tsk.Activate 
     tsk.Close 
 End If 
Next tsk

Methods

These are the main methods of the Task class

Activate - Activates the Task object.

Tasks(1).Activate

Close - Closes the specified task.

Tasks(1).Close

Other Methods

Move - Positions a task window.

Dim lngLeft As Long: lngLeft = 
Dim lngTop As Long: lngTop =  
Tasks(1).Move Left:=lngLeft, Top:=lngTop

Resize - Sizes the specified task window.

Dim lngWidth As Long: lngWidth = 
Dim lngHeight As Long: lngHeight =  
Tasks(1).Resize Width:=lngWidth, Height:=lngHeight

SendWindowMessage - Sends a Windows message and its associated parameters to the specified task.

Dim lngMessage As Long: lngMessage = 
Dim lngwParam As Long: lngwParam = 
Dim lnglParam As Long: lnglParam =  
Tasks(1).SendWindowMessage Message:=lngMessage, wParam:=lngwParam, lParam:=lnglParam

Properties

Height returns or sets the height of the specified task window.

Tasks(1).Height =

Left returns or sets a Long that represents the horizontal position of the specified task, measured in points.

Tasks(1).Left =

Name returns the name of the specified object.

Dim strName As String
strName = Tasks(1).Name

Parent returns an object that represents the parent object of the specified Task object.

Dim objParent As Object
Set objParent = Tasks(1).Parent

Top returns or sets the vertical position, in points, of the specified window.

Tasks(1).Top =

Visible true if the specified object is visible.

Tasks(1).Visible = True

Width returns or sets the width of the specified Task object, in points.

Tasks(1).Width =

WindowState returns or sets the state of the specified document window or task window.

Tasks(1).WindowState = wdWindowStateMaximize

Tasks.Count returns a Long that represents the number of tasks in the collection.

Dim lngCount As Long
lngCount = Tasks.Count

Tasks.Parent returns an object that represents the parent object of the specified Tasks object.

Dim objParent As Object
Set objParent = Tasks.Parent