Find a specified requirement in a specified folder
Public Function GetReqByPath(fullPath$, _
  Optional delimChar As String = "\") _
    As Req
' Find a specified requirement in a specified folder
' This function returns a Req object specified by its
' full path. For example:
'  Set r = GetReqByPath("SCRATCH\OTA_REQ_DEMO\OTA_S_O_1")
'  returns the OTA_S_O_1 object.
' A requirement name is not unique in the project, but it is
'  unique as a direct child of another requirement.
'  Therefore, these routine works by walking down the
'  requirement tree along the fullPath until the requirement
'  is found at the end of the path.
  Dim rFact As ReqFactory
  Dim theReq As Req, ParentReq As Req
  Dim ReqList As List
  Dim NodeArray() As String, PathArray() As String
  Dim WorkingDepth As Integer
' Trim the fullPath and strip leading and trailing delimiters.
  fullPath = Trim(fullPath)
  Dim pos%, ln%
  pos = InStr(1, fullPath, delimChar)
  If pos = 1 Then
    fullPath = Mid(fullPath, 2)
  End If
  ln = Len(fullPath)
  pos = InStr(ln - 1, fullPath, delimChar)
  If pos > 0 Then
    fullPath = Mid(fullPath, 1, ln - 1)
  End If
' Get an array of requirements, and the length
' of the path.
  NodeArray = Split(fullPath, delimChar)
  'WorkingDepth = UBound(NodeArray)
' Walk down the tree.
  'tdc is the global TDConnection object.
  Set rFact = tdc.ReqFactory
  For WorkingDepth = LBound(NodeArray) To UBound(NodeArray)
' First time, find under the root (-1).
'After that, under the previous requirement found: ParentReq.ID
    If WorkingDepth = LBound(NodeArray) Then
      Set ReqList = rFact.Find(-1, "RQ_REQ_NAME", _
          NodeArray(WorkingDepth), TDREQMODE_FIND_EXACT)
      Set ReqList = rFact.Find(ParentReq.ID, "RQ_REQ_NAME", _
      NodeArray(WorkingDepth), TDREQMODE_FIND_EXACT)
    End If
    If ReqList.Count = 0 Then
          Set GetReqByPath = Nothing
          Exit Function
    End If
    ' Delete parent. Each loop has to find it again.
    Set ParentReq = Nothing
    Dim strItem$, reqID&, strID$, thePath$
   'Get a single string from the list returned
   'by ReqFactory.Find. We know it's the first
   'item because the filter can only return one
   'item that exactly matches the RQ_REQ_NAME.
   strItem = ReqList(1)
   ' ReqFactory.Find returns a List
   ' of strings of format: ID,Name.   
   ' For example "9,Products/Services On Sale" 
   ' Extract the ID from the string by splitting the
   ' string at the comma.
   pos = InStr(strItem, ",")
   strID = Mid(strItem, 1, pos - 1)
   ' Convert the ID to a long, and get the object
   reqID = CLng(strID)
   Set theReq = rFact.Item(reqID)
   strID = Mid(strItem, 1, pos - 1)
   'Now check that the object is at the correct depth.
   'If so, we've found the requirement. On the next loop,
   'we'll look from here down.
   thePath = theReq.Path
   PathArray = Split(thePath, "\")
    '            Debug.Print "Number of elements is " & UBound(PathArray)
    '            Debug.Print theReq.ID, theReq.Name
   Set ParentReq = theReq
     If UBound(PathArray) = WorkingDepth Then
     Exit For
   End If
     If ParentReq Is Nothing Then Exit For
  Next WorkingDepth
  Set GetReqByPath = ParentReq
Exit Function
  Set GetReqByPath = Nothing
End Function