Accessing Event Subroutines
The easiest way to access an object’s event subroutines is by following this procedure.
- Access a readily available and robust Visual BASIC editor (such as one that ships with Microsoft’s Word or Excel products).
- Select (General) from the Object list in the code window.
This allows you to make global variable declarations.
- Declare a global variable for your PC-DMIS application as well as your PC-DMIS events. For events, you should use the WithEvents keyword and specify an object that has events. For example these two lines are necessary to define as global variables:
Dim PCDApp As PCDLRN.Application Dim WithEvents AppEvents As PCDLRN.ApplicationObjectEvents
This code would enable the AppEvents variable in the Object list.
- Select your declared variable from the Object list in the code window.
This enables you to select specific event subroutines from the Procedure list.
- From the code window, select an event subroutine from the Procedure list.
The new subroutine appears in the code window.
- Make modifications to the event’s subroutine code as needed. When PC-DMIS meets the specified condition, the event subroutine gets ran along with any code you added.
- In your script, be sure to "set" the AppEvents object variable sometime after you "set" the Pcdlrn.Application object. For example, you will need to have these two lines of code somewhere in your script:
Set PCDApp = CreateObject("PCDLRN.Application") Set AppEvents = PCDApp.ApplicationEvents
Example Script Showing Event Handling
The example script below contains code that calls subroutines when certain PC-DMIS events are executed. Inside a blank Excel worksheet, access the Visual Basic Editor and type in the following code. When finished, save the program, then place the cursor inside the Start() subroutine and press F8 to step through the code line by line to see what's happening. Notice that the code within a specific event subroutine is executed when Excel detects that event occurring within PC-DMIS.
Event Handling Example |
Copy Code |
---|---|
' These are global variable declarations Dim PCDApp As PCDLRN.Application Dim WithEvents AppEvents As PCDLRN.ApplicationObjectEvents Sub Start() ' This is the subroutine to run to start the script HideExcel End Sub Private Sub HideExcel() Dim intAnswer As Integer intAnswer = MsgBox("Do you want to make Excel invisible? For this test, you should click Yes. It will become visible when you open a measurement routine.", vbYesNo, "Hide Excel?") If intAnswer = vbYes Then Application.Visible = False Else Application.Visible = True End If LaunchPCDMIS End Sub Sub LaunchPCDMIS() Set PCDApp = CreateObject("PCDLRN.Application") Set AppEvents = PCDApp.ApplicationEvents PCDApp.Visible = True End Sub Private Sub AppEvents_OnOpenPartProgram(ByVal PartProg As PCDLRN.IPartProgram) ' Event subroutine. This activates when you OPEN a measurement routine. Set PartProg = PCDApp.ActivePartProgram Application.Visible = True MsgBox "Measurement routine " & PartProg.Name & " opened. Excel should also be visible." End Sub Private Sub AppEvents_OnStartExecution(ByVal PartProg As PCDLRN.IPartProgram) ' Event subroutine. This activates when you START EXECUTION of the measurement routine. MsgBox "STARTING EXECUTION OF " & PartProg.Name & ". Click OK to continue." End Sub Private Sub AppEvents_OnEndExecution(ByVal PartProg As PCDLRN.IPartProgram, ByVal TerminationType As Long) ' Event subroutine. This activates when you END EXECUTION of the measurement routine. MsgBox "ENDING EXECUTION OF " & PartProg.Name & ". Click OK to continue." End Sub |