Capturing mouse actions

Capturing mouse actions

You can capture the position of a mouse click by using the Document.GetUserClick function. You can capture the position of a mouse drag by using the Document.GetUserArea function.

Capturing mouse clicks

To get from the user the position of a single mouse click, use the GetUserClick member function of the Document object. This function pauses the macro for a specified period of time, or until the user clicks

somewhere in the document or presses Escape. Here is an example that uses the GetUserClick function: Dim doc As Document, retval As Long

Dim x As Double, y As Double, shift As Long Set doc = ActiveDocument doc.Unit = cdrCentimeter retval = doc.GetUserClick(x, y, shift, 10, True, cdrCursorPick)

The following parameters for the GetUserClick function are coded into this example: • In the variables x and y , the position of the mouse click is returned. • In the parameter shift , the combination of the Shift, Ctrl, and Alt keys that the user holds down when

clicking the mouse is returned. The Shift, Ctrl, and Alt keys are assigned values of 1 , 2 , and 4 (respectively); these values are added together before being returned. • 10 seconds is given to the user to click somewhere in the document. • The SnapToObjects parameter is enabled (that is, set to True ). • The Pick tool’s icon is used for the cursor icon. (You cannot use a custom icon.)

The returned value is 0 , 1 , or 2 , depending on whether the user successfully completes the click, the user cancels by pressing Escape, or the operation times out (respectively).

The returned coordinates of the click are relative to the origin of the page and are in the document’s VBA units, so it is often necessary to explicitly set the units in which you want the value to be returned.

To get the shapes that are underneath the returned click point, use the function SelectShapesAtPoint , which is a member of Page :

doc.ActivePage.SelectShapesAtPoint x, y, True The value indicates whether to select unfilled objects ( True ), or not ( False ).

Capturing mouse drags

To get from the user the position of a mouse drag, or an area or rectangle, use the GetUserArea member function of the Document object. This function pauses the macro for a specified period of time or until the user clicks, drags, and releases somewhere in the document, or until the user presses Escape. Here is an example that uses the GetUserArea function:

Dim doc As Document, retval As Long, shift As Long Dim x1 As Double, y1 As Double, x2 As Double, y2 As Double Set doc = ActiveDocument doc.Unit = cdrCentimeter retval = doc.GetUserArea(x1, y1, x2, y2, shift, 10, True, cdrCursorExtPick) ActivePage.SelectShapesFromRectangle x1, y1, x2, y2, False

The following parameters for the GetUserArea function are coded into this example: • In the variables x1 , y1 , x2 , and y2 , the position of the area is returned as two opposite corners of a rectangle.

• In the parameter shift , the combination of the Shift, Ctrl, and Alt keys that the user holds down when clicking the mouse is returned. The Shift, Ctrl, and Alt keys are assigned values of 1 , 2 , and 4 (respectively);

these values are added together before being returned. • 10 seconds is given to the user to click somewhere in the document. • The SnapToObjects parameter is enabled (that is, set to True ). • The cursor icon to be used is defined by the last parameter.

In this example, the code ends by selecting the shapes that lie completely within the area by using the Page object’s SelectShapesFromRectangle method.

The returned value is 0 , 1 , or 2 , depending on whether the user successfully completes the area selection, the user cancels by pressing Escape, or the operation times out (respectively).

The returned coordinates of the area are relative to the origin of the page and are in the document’s VBA units, so it is often necessary to explicitly set the units in which you want the value to be returned.

This method returns two points that are interpreted as the corners of a rectangle. However, the two points can also be used as the start and end points of a mouse drag.