The examples in this section illustrate how to use VBA scripts to create and use tags. For more information on tag groups, refer to Using Tag Groups in Pictures.
You can create or modify a tag group file using the TagGroupDefinitionInterface. You can access the interface by creating a tag group file object. The following script creates a tag group file object.
Dim TGD as Object
set TGD = _
CreateObject("TagGroupDefinitionInterfaceDll.TagGroupDefinition
Interface")
Once you create the tag group file object, you must retrieve the data in the file before you can modify it. To retrieve the data, use the following script.
'TokenList is an array of tag group symbols
Dim sTokenList() as String, TokenList as Variant
'ReplacementList is an array of tag group substititions
Dim sReplacementList() as String, ReplacementList as Variant
'DescriptionList is an array of tag group descriptions
Dim sDescriptionList() as String, DescriptionList as Variant
'Create the tag group file object
Dim TGD As Object
Set TGD = _
CreateObject("TagGroupDefinitionInterfaceDll._
TagGroupDefinitionInterface")
'In order for the following method to execute without error,
'you need to have a Tag Group file named test or you
replace 'the "
test"
parameter with the name of your tag group file.
TGD.RetrieveDefinition "Test", 2, TokenList, ReplacementList, _
DescriptionList
After the tag group data is retrieved, your script can modify the data. For example, you can change an element in the sReplacementList array and then save to the tag group file with the UpdateDefinition method. The following script shows how to change the substitition for elements 2 and 3 and save them to the tag group file.
Dim sTokenList(4) as String, TokenList as Variant
Dim sReplacementList(4) as String, ReplacementList as Variant
Dim sDescriptionList(4) as String, DescriptionList as Variant
Dim TGD As Object
'Retrieve Tag Group data from tag group file
Set TGD = CreateObject("TagGroupDefinitionInterfaceDll._
TagGroupDefinitionInterface")
'In order for the following method to execute without error,
'you need to have a tag group file named
"test1" or you will
'replace the "
Test1" parameter with the name of your tag group
'file.
TGD.RetrieveDefinition "Test1", 4, TokenList, ReplacementList, _
DescriptionList
'Modify tag group data
TokenList(2) = "Tag3"
TokenList(3) = "Tag4"
ReplacementList (2) = "FIX32.NODE2.AI1.F_CV"
ReplacementList (3) = "FIX32.NODE2.AI2.F_CV"
DescriptionList (2) = "Temperature for Node 2"
DescriptionList (3) = "Pressure for Node 2"
'Create the tag group file object and save modified tag group file
Set TGD = _
CreateObject("TagGroupDefinitionInterfaceDll._
TagGroupDefinitionInterface")
TGD.UpdateDefinition "Test", 4, TokenList, ReplacementList, _
DescriptionList
Set TGD = Nothing
As a final example, we provide the following Command button script. This script iterates through all the tag group variables in a picture and creates a substitution string based on the name of the tag group variable. This script assumes that the picture containing the Command button has retrieved the tag groups from the Tag Group object with the RetrieveDefinition method first.
Private Sub CommandButton1_Click()
Dim vaSymbols As Variant
Dim vaSubstitutions As Variant
Dim vaDescriptions As Variant
Dim sSubstitutions() As String
Dim sDescriptions() As String
Dim PicPath as string
'This will contain the number of symbols in a picture
Dim Size As Integer
Dim Counter As Integer
Dim FileName As String
'Set the filename
FileName = "Test"
'Delete tag group file if it exists. Kill permanently
'deletes the specified file form the hard drive. Use with
'caution.
PicPath = System.PicturePath
If DIR(PicPath + FileName + ".TGD" <> "" Then
Kill PicPath + FileName + ".TGD"
End If
'Get the symbols from the picture
Me.RetrieveTagGroupVariables Size, vaSymbols
If Size = 0 Then
Exit Sub
End If
ReDim sSubstitutions(Size - 1) As String
ReDim sDescriptions(Size - 1) As String
'Fill in the symbols and the descriptions
For Counter = 0 To Size - 1
Temp$ = "dvsys." + vaSymbols(count2) + ".f_cv"
sSubstitutions(Counter) = Temp$
sDescriptions(Counter) = "Generated tag"
Next Counter
'Create the tag group file object
Dim TGF As Object
vaSubstitutions = sSubstitutions
vaDescriptions = sDescriptions
Set TGF = _
CreateObject("TagGroupDefinitionInterfaceDll_
.TagGroupDefinitionInterface")
TGF.UpdateDefinition FileName, Size, vaSymbols, _
vaSubstitutions, vaDescriptions