Has anyone ever had a problem with a Forall statement creating an infinite loop before?

Hi all,

I am working on some code that dumps a notes document into a word template, but I’ve run into a problem. The code that changes the dimensions in the word tables I am working with goes into an infinite loop, even though I am using a Forall statement to go through the object collection.

Here’s the code:

Forall table In m_wordDoc.Tables

				Print "Table ID is: " + table.ID

				If table.ID = TLABEL_APPROVALS Then

				'now we need to loop through the elements to make sure we add the right number of rows. We will want to skip the first row, because we 

				'already know that it is there.

					For y = 0 To Ubound(m_doc.ApprovalList)

					'we want to add every row but the first one, which should already be there

						If y <> 0 Then

							Call table.Rows.Add

						End If

					Next

					'm_foundApprovalTable = True

				End If

%REM

				Elseif table.ID = TLABEL_APPCOMMENTS Then

					For y = 0 To Ubound(m_doc.ApprovalList)

						If y <> 0 Then

							Call table.Rows.Add

						End If

					Next

					'm_foundApprovalCommentTable = True

				End If

				If m_foundApprovalTable And m_foundApprovalCommentTable Then

					Exit Forall

				End If

%END REM

			End Forall

What happens is that when it finally does find the table represented by the constant TLABEL_APPROVALS, it never moves on to the next table in the collection.

Does anyone know any reason why this is happening? Or could it be a bug?

Thanks.

brandt

Subject: RE: Has anyone ever had a problem with a Forall statement creating an infinite loop before?

No, I’ve never seen that, but then I’ve also never tried Forall with an OLE object owned by another application as the collection argument.

You have the statement Print "Table ID is: " + table.ID at the top of the loop. This output is an important clue about what’s happening; what does it tell you? Is it repeatedly printing the ID of the first table? Is it going through all the tables and then repeating on the last one? Is it going through the tables until it reaches the ID TLABEL_APPROVALS, and then repeating on that one (which would be suspicious because that’s the first iteration where you modify the document)? What, what, what?

Maybe you should copy the m_wordDoc.tables to a variable you own, before you try iterating through it.

Subject: RE: Has anyone ever had a problem with a Forall statement creating an infinite loop before?

Andre,

Thanks for the response and sorry that I did not include that important detail of what was happening when I ran it through the debugger. The behavior I am seeing is that the Forall loop will process through the collection of tables properly until it actually finds the table it’s looking for. Once it finds that table, it gets stuck on that table and will not move forward.

I did try casting the table collection into a variant and the behavior was identical.

I am working under the impression that this may be a bug, but as you noted, I am working with an object that isn’t native to Notes/Domino. I may just change the code to use a For…Next loop and iterate that way. But if you have any other suggestions, I’d be glad to hear them.

thanks.

brandt

Subject: Has anyone ever had a problem with a Forall statement creating an infinite loop before?

Is it possible that your “table.Rows.Add” somehow disturbs the original list of tables and the code can’t find the next table because of this?Are you sure it really works correctly with “table.Rows.Add” without specifying row details?

You can also try with something like this:

For x=0 to ubound(m_wordDoc.Tables)

table=m_wordDoc.Tables(x)

Subject: RE: Has anyone ever had a problem with a Forall statement creating an infinite loop before?

Andrei,

I think you may be on to something there, so I just changed it to a For…Next loop, using the Tables.Count property to control the loop.

I probably should have done that in the first place. Thanks for all of the advice.

brandt