Document-Level Events Example

With document-level events, you can customize the behavior of open documents. Follow these step-by-step instructions to customize the current spreadsheet by changing the meaning of a double-click on a spreadsheet cell. In more technical terms, this example creates an event procedure for events in the STATISTICA Spreadsheet.

1. Open a spreadsheet, e.g., the example data file Exp.sta, in STATISTICA.

2. From the View menu, select Events - View code. At this point the Document Events window is displayed where you can associate certain events with this spreadsheet.

3. In the Object box select Document, and in the Proc box select BeforeDoubleClick. The Documents Events window will now look as the following illustration:

The following code will be inserted in the Documents Events window:

Private Sub Document_BeforeDoubleClick _
(ByVal Flags As Long, _
ByVal
VarNo As Long, _
ByVal
CaseNo As Long, _
Cancel As Boolean)

End Sub

This code is a placeholder for the event code.

4. Change the code to the following:

Private Sub Document_BeforeDoubleClick _
(
ByVal Flags As Long, _
ByVal
VarNo As Long, _
ByVal
CaseNo As Long, _
Cancel
As Boolean)

MsgBox "Double-click not allowed here"

Cancel = True

End Sub

5. From the Run menu, select Run macro. At this point, the event handler is running. Hence, you have specified (via the code added in the previous step) that when you double-click on any spreadsheet cell, a message box will be displayed that says "Double-click not allowed here." To test this, follow the remaining steps in this example.

6. Activate the spreadsheet (by clicking on any cell) and then double-click on any cell in the spreadsheet. Instead of the default action associated with double-clicks (editing the cell), you will see the following message box displayed:

  

As you can see, we have now "protected" the cells of the spreadsheet against the double-click event.

7) At this point, you may want to save the event handler (code) with the spreadsheet so that it will be run any time that spreadsheet is open. To do this, click on the Document Events window and select Save from the File menu. Now, close the Document Events window and activate the spreadsheet. Select  Events - Autorun from the View menu. Finally, save the spreadsheet (option File - Save).

The next time you open the Exp.sta file, the new event handler will automatically be executed, and you will not be able to double-click on a cell in the spreadsheet. To "disconnect" the event handler, select again the View menu command Events - Autorun to clear that option.

See Graph Document-Level Events, Report Document-Level Events, Spreadsheet Document-Level Events, and Workbook Document-Level Events for listings of graph, report, spreadsheet, and workbook events, respectively.

See also, Macro Event Behavior.