Code Finds & Replaces tags on all symbols EXCEPT XYPlot

Discussion created by kpotter on Mar 25, 2011
Latest reply on Mar 25, 2011 by dlunasin

I have VBA code that does a find/replace on all references in a display.  For example, lets say you have Pump 91A, with several tags, all containing "91A" in their name.  The code can change every "91A" to "91B", or whatever replacement you need.  It also works on AF References (Yes, I know about AF Relative displays, which will make all of this obsolete, but I still need this.)  I'm relying on "SetTagName" which exists for every symbol EXCEPT XYPlot.  I submitted a suggestion to tech support to add this (they ultimately said they will) and they said I can code around it by reading each tag, adding the new tag, then removing the old.  They gave me the example below, but it's not preserving settings such as:  Multiple Y Scales, Data Retreval Methods, Plot Time.  If anyone has code that does this, that would be most awesome.  Thank you!


Sub ChangeXYPlotTagDynamically()
Dim xySym As XYPlot
Dim xydef As XYDefinition
Dim myXYtags As XYTags
Dim Datasource As String
Dim TagName As String
Dim maxScale As Double
Dim minScale As Double
Dim maxConfig As pbMinMaxEnum
Dim minConfig As pbMinMaxEnum
Dim scaleCnt As Integer

Set xySym = ThisDisplay.XYPlot1
Set xydef = xySym.GetDefinition
Set myXYtags = xydef.Tags
scaleCnt = myXYtags.Count 'STATIC VALUE!!!
For counter = 1 To myXYtags.Count
    'first tag in the collection will be an old tag each time through the loop

    'get tagname
    Datasource = myXYtags(1).TagName
    TagName = Datasource + "123" 'This temporarily replaces the FindReplace function
    'Used so when new tag is created it is set to the proper config
    Dim isXtag As Boolean
    isXtag = myXYtags(1).isXtag
   'Preserve the scale if set to absolute value
   'Max and min are enumerations
    maxConfig = myXYtags(counter).Max
    minConfig = myXYtags(counter).Min
    If (maxConfig = pbScaleAbsolute) Then
       maxScale = CDbl(myXYtags(counter).AbsoluteMax)
    End If
    If (minConfig = pbScaleAbsolute) Then
       minScale = CDbl(myXYtags(counter).AbsoluteMin)
    End If
    xydef.Tags.Add TagName, isXtag
    xydef.Tags.Remove (1)
    'Since the newly added tag will always be the last one in the list, set the scaleCnt to the Count of tags
    myXYtags(scaleCnt).SetScaleConfiguration False, minConfig, minScale, maxConfig, maxScale

Next counter

xySym.SetDefinition xydef
End Sub