Form Script Sample Code
The following example shows code that collects the name of the picture the user wants to display when the event is triggered, generates the script, and creates the event. The Procedures object is a collection of scripts. Before you generate the script, you need to get the index number for the next script using the GetEventHandlerIndex method. Once you generate the string that will become the script, use the AddEventHandler method to add the event.
Private CurrentObject As Object
Private EventName As String
Public Sub GetCurrentObjectValues(obj, Str)
Set CurrentObject = obj
EventName = Str
End Sub
Private Sub CommandButton1_Click()
'get file name entered by user
Dim FileName As String
FileName = TextBox1.Value
Dim WorkingObj As Object
Set WorkingObj = CurrentObject.Procedures
'Get the index for the procedure
Dim Index As Long
Dim Found As Long
WorkingObj.GetEventHandlerIndex EventName, Index, Found
'Generate code
Dim StringCode As String
StringCode = "Dim doc As Object" & Chr(13) _
& "Dim PicturePath As String" & Chr(13) _
& "PicturePath = ""c:\fix32\pic\"" & FileName" & _
Chr(13) & "Set doc = _
Application.Documents.Open(PicturePath)" & Chr(13)
'Add the event
WorkingObj.AddEventHandler EventName, StringCode, Index
'Close the form
End
End Sub