This section lists some tips to keep in mind when writing VBA scripts within DeltaV Operate.
Use the following script to create a shape through a VBA script:
Dim Pic As Object Dim Shape As Object Set Pic = Application.ActiveDocument.Page Shape.HorizontalPosition = 10 Shape.VerticalPosition = 10 Shape.Width = 30 Shape.Height = 30 Shape.ForegroundColor = RGB( 255, 0, 0 )
The BuildObject method creates the object. Once the object is created, you can perform one of these options:
Use a BaseCount of 1 when using DeltaV Operate collections such as SelectedShapes, Procedures, ContainedSelections, ContainedObjects, and Documents. See the VBA Help file for more information on collections.
To connect an animation object and a data source, use the SetSource method to set the Animation object's source property:
AnimationObj.SetSource "FIX32.NODE.TAG.FIELD", False, _ ExpressionEditor1.RefreshRate, ExpressionEditor1.DeadBand, _ ExpressionEditor1.Tolerance
The SetSource method allows you to set the data source's refresh rate, deadband, and tolerance. The second parameter lets you set an undefined object as the data source. (True indicates a UseAnyway condition.)
You can develop scripts directly in the main VBA project; however, this approach makes reusing the scripts more difficult. Instead, you should store subroutines in a separate module and then call these subroutines from the main project when you want to reuse the scripts. This allows you to export and then import the scripts into a new project with minimal modification, and also provides a more modular, component-based design.
You can cut (or copy) and paste an object from one project to another by dragging and dropping that object. Although this operation directly copies the VB code within the object to the new project, it does not automatically copy the event entries (a Click event, for example). Make sure you copy the content of the event entry into a subroutine (by selecting Edit Script from the object's pop-up menu) before pasting the code into the new project.
VBA allows you to add an object library or type library reference to your project, which makes another application's objects available in your code. These additions to your VBA project are called references. You can view, add, and delete references by selecting the References command from the Tools menu in the Visual Basic Editor (VBE).
Whenever you add a control into a picture, the control's type library is referenced by the picture within VBA. When you delete a control from a picture, the reference to the control is automatically removed to increase performance. However, you should never manually remove the references to "Project_FactoryGlobals" or "Project_User".
Whenever you reference objects, controls, or mechanisms in VBA, follow the guidelines in the following sections. To learn more about references in VBA, refer to the VBA Help file.
Any object that is referenced by name in a script cannot be deleted. For example, in the following sample script the code in Rect2_Click will execute, but the pen will not be deleted:
Rect1_Click()
Pen1.Source = "dvsys.AI_30.F_CV"
End Sub
Rect2_Click()
Chart1.DeletePen 1
End Sub
If you wanted to access the object in this example without referencing it by name, you could use the following code in Rect1_Click ():
Rect1_Click()
Dim o as object
set o = Chart1.Pens.Item(1)
o.Source = "dvsys.AI_30.F_CV"
End Sub
When an object (2Dshape, FixDynamics object, ActiveX control) is deleted from a picture and no object of that type are left in the picture, the reference to that object's type library in the VBA project is removed. To continue to use this object's type in scripts, you must manually add a reference to the type library in the VBE by selecting References from the Tools menu and selecting the type library.
You should be aware of the following behavior when dragging and dropping a Dynamo into a picture, cutting and pasting a Dynamo, or dragging and dropping a toolbar button from a category into a toolbar:
VBA copies all forms, scripts, events, and sub-forms associated with the toolbar button or Dynamo.
VBA does not copy any VBA modules or class modules associated with the toolbar button or Dynamo. Code that you put in these modules will not run if you drag the Dynamo or the toolbar button to another picture or toolbar.
VBA does not copy references to other objects such as controls or DLLs that you create for toolbar buttons or Dynamos. For example, if you include a third-party OCX as a control on a form for a toolbar button, VBA does not copy the reference when you drag the toolbar button to a toolbar. The script will not run until you open the Visual Basic Editor and create a reference to the OCX for the toolbar project.
Every global subroutine includes an optional parameter called intErrorMode. The intErrorMode parameter allows users to trap errors and to send them to Alarm Services. There are three options for the intErrorMode.
Enter this option... |
To... |
|---|---|
0 |
Use the default error handling. Allows subroutines to provide the error messages. If no entry is made for the intErrorMode parameter, the default is used. |
1 |
Allow the user to handle the error messages. Errors in the subroutines are passed back to the calling routine for handling. |
2 |
Write errors to all Alarm Services. No error messages display. Instead, the errors are written to all DeltaV Operate Alarm Services, including the Alarm History window. |
For example, if you use the intErrorMode parameter with the OpenDigitalPoint subroutine, the command would look like:
OpenDigitalPoint [DigitalPoint], [intErrorMode]
For the OpenPicture subroutine, you get the standard error message if you enter 0 for the intErrorMode, as shown in the following example:
OpenPicture "BadPic", , , , 0
When you use 0 for the intErrorMode, if you try to open a picture that does not exist, a message box appears whose title is the name of the picture that made the erroneous call and whose contents are the error number and error description.
If you enter a 1 for intErrorMode, the error is raised for you to handle:
OpenPicture "BadPic", , , , 1
Your error handling code would have to look something like this:
On Error Goto Errorhandler
OpenPicture "BadPic", , , , 1
End Sub
Errorhandler:
Msgbox "my error message" + Chr(13) + Cstr(Err.Number) +
Chr(13) + Err.Description, , Err.Source
If you enter a 2 for intErrorMode, the error is sent to all Alarm Services, including the Alarm History window using the SendOperatorMessage method:
OpenPicture "BadPic", , , , 2
When you use 2 for the intErrorMode, you provide for silent error tracking.
Plug and Solve and expert globals can affect existing scripts that loop through the documents collection. If you have scripts that loop through the documents collection, and you want to filter out all global pages to look at your documents only, look at the Document. Type property using the following sample code:
If docobj.Type = "FixGlobalsServer.FixGlobalsServer.1" then 'this is a global page