Do Until Loop

Hello All-

In my script below I am trying to gather information from each document in the Calendar view and write it to a ~ delimited txt file.

The script seems to be falling out of the initial loop.

There are three document in the view currently.

Do you see any reason why it bails?

Thanks

Pat

Sub Initialize

Dim Session As NotesSession     

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument



Dim xxAppointmentType As String

Dim TaskType As Variant	

Dim xxLocation As Variant		

Dim xxChair As Variant

Dim xxRoom As Variant 

Dim xxSubject As Variant

Dim ExpValue As String

Dim CalEntry As String





Set Session =  New NotesSession

Set db = session.CurrentDatabase

Set View = db.GetView("($Calendar)")

Set doc = view.GetFirstDocument



' Get an unused file number so LotusScript can open a file.



Dim fileNum As Integer, I As Integer

Dim fileName As String



fileNum% = Freefile()

fileName$ = "C:\tmp\" & doc.UniversalID & ".txt"	

’ Write out some employee data.

Open fileName$ For Output As fileNum%



Do Until Not(doc Is Nothing)

	Const NotesMacro1$ = "@Name([Abbreviate];@Subset(Room;1))"

	Const NotesMacro2$ = "@Name([CN];Chair)"

	

	TaskType = doc.getItemValue("TaskType")

	'xxAppointmentType = doc.getItemValue("AppointmentType")

	

	

	xxRoom = doc.Room(0)

	xxChair = doc.Chair(0)

	xxSubject = doc.Subject(0)

	

	

	

	

	If xxAppointmentType<>"3" Then		

		Goto flag

	Else

		xxRoom = "Room: " & Evaluate(NotesMacro1$, doc)

	End If

	

	If xxAppointmentType = "3" Then

		xxChair = "Chair: " & Evaluate(NotesMacro2$, doc)

	Elseif TaskType = "2" Then

		xxChair = "Owner: " & Evaluate(NotesMacro2$, doc)

	End If

flag:

	If Not Isnull(doc.Location) Then

		xxLocation = "Location: " & doc.Location(0)

	Else

		xxLocation = ""

	End If

	

	

	

	

	

	CalEntry = xxSubject & "~" &  xxRoom & "~" &  xxLocation & "~" &  xxChair

	'Messagebox (CalEntry)

	

	

	ExpValue = doc.StartDateTime(0) & "~" & doc.EndDateTime(0) & "~" & CalEntry

	

	'Messagebox (ExpValue)

	

	

	Write #fileNum%, ExpValue

	

	

Loop

Close fileNum%

End Sub

Subject: Do Until Loop

I think you are confusing Do While and Do Until. Both perform conditional execution of the nested statements. Do While evaluates the condition “at the top” of the nest. Do Until evaluates the condition “at the bottom” after the nest has been executed.

For your needs you can use:

Do While Not(doc Is Nothing) or Do Until doc Is Nothing

You also have to grab the next document just before the loop statement.

Set doc = view.GetNextDocument(doc)

I always use the Do While construction for this reason: if doc has no value the statements are never executed. Try that with Do Until and you’ll find the statements are executed, one, but raise an error because doc is nothing.

Subject: RE: Do Until Loop

Robert -

Just to be clear . . . as related to “do loops” and the while/until condition, the way the loop is constructed determines the sequence in which the condition is evaluated for the loop, not the loop condition itself.

For example . . .

There will be exactly one pass in this loop because the while condition is checked after each iteration. So even though the condition for the loop is already false, the loop is entered, one pass executes, then the loop exits.

Dim i As Integer

Dim j As Integer

Dim intTotal As Integer

intTotal% = 0

i = 3

j = 2

Do

intTotal% = intTotal% + 1

i = i + 1

Loop While(i < j)

Print "Total number of iterations: " & intTotal%

'Output is “Total number of iterations: 1”

But without changing any values, this loop never executes because the while condition is checked before iteration starts:

Dim i As Integer

Dim j As Integer

Dim intTotal As Integer

intTotal% = 0

i = 3

j = 2

Do While(i < j)

intTotal% = intTotal% + 1

i = i + 1

Loop

Print "Total number of iterations: " & intTotal%

'Output is “Total number of iterations: 0”

In this loop, one pass executes because even though the until condition is already true when the loop is entered because the condition is checked after iteration. So again, it makes one pass and stops.

Dim i As Integer

Dim j As Integer

Dim intTotal As Integer

intTotal% = 0

i = 0

j = 2

Do

intTotal% = intTotal% + 1

i = i - 1

Loop Until(i < j)

Print "Total number of iterations: " & intTotal%

'Output is “Total number of iterations: 1”

But here, the loop never executes because the until condition is checked before iteration starts:

Dim i As Integer

Dim j As Integer

Dim intTotal As Integer

intTotal% = 0

i = 0

j = 2

Do Until(i < j)

intTotal% = intTotal% + 1

i = i - 1

Loop

Print "Total number of iterations: " & intTotal%

'Output is “Total number of iterations: 0”

Subject: Do Until Loop

Well …Remove the “NOT” from your UNTIL statement. It never goes into the loop. Plus, you GetFirstDocument, but you never GetNextDocument. Also, you should not set your constants repeatedly inside the loop. You refer to xxAppointmentType occasionally in IF statements, but you never set that variable. The only reference to setting it is commented out.

Yes, it has been a long week! :slight_smile: