Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2007

Tip: Looking for answers? Try searching our database.

Collection object will not return items using key

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter - 22 Aug 2007 21:00 GMT
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
Shauna Kelly - 23 Aug 2007 05:10 GMT
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

> Hello,
>
[quoted text clipped - 65 lines]
> Thanks,
> Peter
Peter - 23 Aug 2007 12:28 GMT
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

> Hi Peter
>
[quoted text clipped - 89 lines]
> > Thanks,
> > Peter
Peter - 23 Aug 2007 12:38 GMT
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

> Hi Peter
>
[quoted text clipped - 89 lines]
> > Thanks,
> > Peter
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.