### Accessing Event Subroutines

The easiest way to access an object’s event subroutines is by following this procedure.

1. Access a readily available and robust Visual BASIC editor (such as one that ships with Microsoft’s Word or Excel products).

2. Select (General) from the Object list in the code window. This allows you to make global variable declarations.

3. 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.

4. Select your declared variable from the Object list in the code window.
   This enables you to select specific event subroutines from the Procedure list.

5. From the code window, select an event subroutine from the Procedure list.
   The new subroutine appears in the code window.

6. 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.

7. 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 Handing

This example script 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.

' 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 part program.", 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 part program.

Set PartProg = PCDApp.ActivePartProgram

Application.Visible = True

MsgBox "Part Program " & 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 part program.

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 part program.

MsgBox "ENDING EXECUTION OF " & PartProg.Name & ". Click OK to continue."

End Sub