12 Replies Latest reply on Dec 13, 2018 2:08 PM by greche

    PiPutVal giving 'index outside bounds of the array' in results


      I'm trying to implement piputval in DataLink using some VB script I found on this site, but I keep ending up with 'Index was outside the bounds of the array.' for the output. When I check the PI tag in AF it is not being updated. The VB script is below.


      Option Explicit

      Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

      'This subroutine is called by the <Send above values> button on the
      'left hand side of the "PutVal" worksheet
      Sub put_data1()
         Dim i As Integer
         Dim numoftags As Integer
         'The following four variables are arguments to PIPutVal()
         Dim sTagname As String  'Tagname
         Dim stime As String     'Timestamp
         Dim sServer As String   'PI server name
         Dim valueCell As Range  'Cell reference containing value to be written
         Dim resultCell As Range 'Cell reference to hold result
         Dim apierr As Long
         Dim rowstr As String
         Dim buf As String
         Dim macroResult As Variant
         Dim timeCell As Range 'Cell reference to hold time of putval
         i = 0
         numoftags = 3  'we have three tags, from cell B4 to B6
         'server is in E8
         sServer = Worksheets("PutVal").Cells(8, 5).Text
         While i < numoftags
            'resultCell is in column 5 (E)
            Set resultCell = Worksheets("PutVal").Cells(i + 4, 5)
            'valueCell is in column 4 (D)
            Set valueCell = Worksheets("PutVal").Cells(i + 4, 4)
            'tagname is in column 3 (C)
            sTagname = Worksheets("PutVal").Cells(i + 4, 3).Text
            'timestamp is in column 2 (B)
            stime = Worksheets("PutVal").Cells(i + 4, 2).Text
            'Call the PIPutVal() macro function
            'Note that we pass just a , for the PIServer argument; i.e., we
            'are using the default PIServer
            macroResult = Application.Run("PIPutVal", sTagname, valueCell, stime, sServer, resultCell)
            'move down to the row number
            i = i + 1
         Sleep (1000)
         i = 0
         While i < numoftags
            rowstr = Format(i + 4)
            buf = "G" & rowstr
            buf = "=PIExTimeVal($C$" & rowstr & ",$B" & rowstr & ",""" & sServer & """)"
            Selection.FormulaArray = buf
            i = i + 1
      End Sub