How can I loop through?

hi,

i’m trying to do a change in a field that’s repeated 50 times in one document. i’m using a script agent to do this.

so right now i have fields named, doc.name1, doc.name2, doc.name3, so on and so forth until doc.name50.

is there a way to go through these fields through a loop of some kind? please help!

thanks!

sarah

Subject: How can I loop through?

'this will let you get a handle on all 50 fields one at a time.

Dim item as NotesItem

For i = 1 to 50

set  item = doc.getFirstItem("name" + cstr(i))

Next

Subject: RE: How can I loop through?

you might use trim(cstr(i)) as following (from Kentucky):

For i = 1 to 50

set item = doc.getFirstItem(“name” + trim(cstr(i)))

Next

I used this kind of code in my agent quite often.

Goog luck.

GY

Subject: RE: How can I loop through?

what would the trim do?

also, here’s a sample of my code:

If doc_config.ft1(0) <> “Entry not found in index” Then

		xlwrkbook.Worksheets("Sheet1".Range("A1").Value=doc_config.ft1(0)

change = doc_config.d1(0)

End if

Subject: RE: How can I loop through?

i think cstr might pad the number with a space. I usually find its more work and just easier to concantonate them with an & sign.

myval = doc.getItemValue(“field” & i)

xlwrkbook.sheets(“Sheet1”).range(“A1”).value = myval

Subject: RE: How can I loop through?

hmm… this code doesn’t seem to work.

xlwrkbook.sheets(“Sheet1”).range(“A1”).value = myval

i think it’s because “myval” is a variant. it works with a string field. does anyone know of any tricks?

thanks again

Subject: RE: How can I loop through?

if isarray(myval) thenwork. xlwrkbook.sheets(“Sheet1”).range(“A1”).value = join(myval,", ")

else

work. xlwrkbook.sheets(“Sheet1”).range(“A1”).value = myval

end if

you could also loop through the array if you would like.

i = 0

forall val in myval

i = i + 1

work. xlwrkbook.sheets(“Sheet1”).range(“A” & i).value = val

end forall

Subject: RE: How can I loop through?

i think the issue is that “myval” is a variant, and even when it’s not an array, it won’t work.

work. xlwrkbook.sheets(“Sheet1”).range(“A1”).value = myval

myval has to be converted to a string before it’s placed into the cell. does anyone know a way?

Subject: RE: How can I loop through?

By default, all notes fields are arrays of one or more values. You need to explictly access the 0th element when using .getItemValue just as you would when using the shorthand notation.

Try

dim myval as string

myval = doc.getItemValue(“field” & i)(0)

xlwrkbook.sheets(“Sheet1”).range(“A1”).value = myval

If you expect your field to contain an array, you will need to loop through as shown in a previous response

Subject: RE: How can I loop through?

shouldn’t the following code work?

Dim dateuser as Variant

dateuser = doc.getItemValue(“dhu” + Cstr(1))(0)

For a =Lbound(dateuser) To Ubound(dateuser)

xlwrkbook.Worksheets(“Sheet1”).Cells(3,e).Value= dateuser(a)

e = e+1

Next

the dhu1…dhu2…dhu3…etc fields are text list fields.

thanks again for any help.

Subject: RE: How can I loop through?

(1) Trim() remove space(es) before and after the (i), I can not tell why but just my experience.(2) it looks fine, but be careful here:

xlwrkbook.Worksheets(“Sheet1”.Range(“A1”).Value=…

the “A1” in the Range(“A1”) must be be:

“A” + trim(cstr(i))

Otherwise you may overwrite the value in a single cell (A1) again and again.

GY