Collection object will not return items using key

P

Peter

Hello,

I have the following code:

Function CreateCrossRefCollection(sRange As Range) As Collection
Dim aField As Field, FieldName As String, ReqName As String
Dim tCase As String, TCaseObj As TCaseClass
Dim XRefCollection As Collection, ReqCollection As Collection

Set XRefCollection = New Collection

For Each aField In sRange.Fields
FieldName = UCase(aField.Code)
If aField.Type = wdFieldSequence Then
If InStr(FieldName, "TESTSECTIONLEVEL") Then
tCase = "VTKP_" & Format(aField.Result, "00") & "_"
ElseIf InStr(FieldName, "TESTMODULELEVEL") Then
tCase = tCase & Format(aField.Result, "00") & "_"
ElseIf InStr(FieldName, "TESTCASEID") Then
tCase = tCase & Format(aField.Result, "00")
End If
ElseIf aField.Type = wdFieldRef Then
If InStr(FieldName, "REF REQ_") Then
ReqName = Mid(FieldName, 5, 13)
Set TCaseObj = New TCaseClass
TCaseObj.Name = tCase
If CollectionItemExists(ReqName, XRefCollection) Then
Set ReqCollection = XRefCollection(ReqName)
ReqCollection.Add TCaseObj, tCase
Else
Set ReqCollection = New Collection
ReqCollection.Add TCaseObj, tCase
XRefCollection.Add ReqCollection, ReqName
End If
End If
End If
Next aField
Set CreateCrossRefCollection = XRefCollection
End Function

I have a document that contains a traceability matrix (Requirement numbers
with corresponding test cases). The test cases are numbered using three
SEQuence fields: "TestSectionLevel", "TestModuleLevel" and "TestID". The test
case contains crossreferences to the requirements in the trace matrix.
The code is intended to scan a predifined range in the document for the test
case fields and then locate the crossreference fields. I want one collection
for each requirement that contains one TCaseObj object (contains the test
case ID) per test case for that requirement.
Notice that this is a function. It returns a collection that contains one
collection object for each requirement found (key is reqName).

The line: "Set ReqCollection = XRefCollection(ReqName)" works perfectly when
the code is running but if I put a break in the very last line of this
function and then try to execute that line, I get the following error:
"Runtime Error '5' Invalid procedure call or argument".
I have looked at this code until I am blue in the face and cannot figure out
why I cannot retrieve the collection object using the key. Likewise, the
procedure that calls this function cannot retrieve objects from the
collection using the key.

Anybody got any ideas what is going on here?

Thanks,
Peter
 
S

Shauna Kelly

Hi Peter

Two things:

1. Try adding some debugging code so you can see exactly what you're
providing as the key when you add an item to the collection. The key must be
a string, and it must be a string that doesn't look like a number. It looks
to me like tCase might sometimes be a string like "12" and that won't work.

The reason is that you can refer to an item in a collection with either an
index number or the key. That is, mycollection.Item(1) or
mycollection.Item("MyKeyName"). If you give it something that smells like a
number, VBA assumes you're using the index number.

2. Why are you not using GetCrossReferenceItems? I ask because I'm currently
working on a tool related to cross-reference items. So I'm wondering if you
weren't aware of GetCrossReferenceItems or if you've abandoned using it
because you had problems, and if so, what those might be.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
P

Peter

Shauna,

Thanks for the reply. I have but used debugging extensively. The key is
never a number, it always begins with "REQ_". The Code of the crossreference
field is always "REF REQ_00_00_00 \h".

I will look into "GetCrossReferenceItems". I did not know about it.

Thanks,
Peter
 
P

Peter

Shauna,

I just figured out the problem. reqName is Mid(aField.code, 5, 13). It seems
that there are non-printing characters in the field code that were being
extracted. The correct command is Mid(aField.code, 6, 12). This returns what
looks identical to what is returned by 5, 13 but does not contain the
non-printing characters. I would not have found it without your response.

Thanks!
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top