Skip to main content
Skip table of contents

Desktop Client: How do I create a user defined function using VBA?

From the Model menu, you may choose "Functions...". 

In the 'Define Function' dialog box that displays, once you have added your own function, there is a Visual Basic checkbox option that is displayed. 

This Visual Basic checkbox option allows you to link your function behavior to VBA code. The process to create a function using VBA and connect to a User Defined function in iGrafx is to handle the FunctionValue() event on the Document object in VBA. The convention in VBA is that when you handle an event the event handler is named in the following format: Object_EventName. In this case, the object where the event is fired is a "Document", and the event name is FunctionValue. Therefore the event handler function is named "Document_FunctionValue". 

To create the function do the following:

  1. Go to the VBA editor in the tool (Alt F11, or use the Tools->Visual Basic->Visual Basic Editor).
  2. Double-click on the "ThisDocument" icon in the window at the upper left (Project Window). An edit window will appear on the right with the default headers (combo boxes) named "(General)" and "(Declarations)".
  3. Pull down "(General)" and select the "Document" object. You'll notice that the second combo defaults to "Activate" and a default function stub appears (Document_Activate()). Ignore that for now and we'll delete it later.
  4. Pull down "Activate" in the combo box on the right and choose "FunctionValue". This will cause the "Document_FunctionValue()" stub to be inserted in the edit window. You can now delete the Document_Activate() function stub from the previous step.
  5. Below is some sample code for an example User Defined function called "HalfTheArgument":

---------

Private Sub Document_FunctionValue(ByVal FunctionName As String, ByVal DoubleArgument As Double, DoubleResult As Double)
If FunctionName = "HalfTheArgument" Then DoubleResult = DoubleArgument/2 End If
End Sub

-------------- 

By handling the event, you will process all "Visual Basic" User Defined functions from iGrafx. Therefore you must check the string to see which function you are about to process. This string is contained in the argument "FunctionName". The function simply takes the argument "DoubleArgument" passed (based on your choice, argument or no argument, when defining the User Defined Function in iGrafx), and divides it by 2, and returns the result by setting the "DoubleResult" argument value. 

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.