Option Public
Option Declare

Sub Initialize
	' agent mines group and sends e-mail with results
	Dim sfilepath As String			' person to receive report
	' ******************* update below **********
	sfilepath = "u:\temp\grouplist.csv"
	'**********************************************
	
	Dim s As New NotesSession
	Dim w As New NotesUIWorkspace	
	Dim db As NotesDatabase							' current db
	Dim snm As String										' server w/names.nsf
	Dim nDoc As NotesDocument						' group doc
	Dim nCol As NotesDocumentCollection		' selected groups to export
	Dim ntype As String									' group type
	Dim tmpstr As String									' group export line string
	Dim sfilenum As Integer								' file handle for sfilepath
	
	On Error Goto SErrorHandler
	
	' setup environment
	Set db = s.CurrentDatabase
	Set nCol = db.UnprocessedDocuments
	snm = db.Server
	
	' get groups checked
	If (nCol.Count = 0 ) Then
		Msgbox "No groups to process.", , "Cancelled"
		Exit Sub
	End If
	
	' start file
	sfilenum = WriteFileHeader(sfilepath)
	If (sfilenum = 0) Then
		Msgbox "Unable to start export file at:" & sfilepath & ".", , "Cancelled"
		Exit Sub
	End If
	
	Set nDoc = nCol.GetFirstDocument()
	While Not (nDoc Is Nothing)
		' check type of group (0 or 1 include e-mail types)
		ntype = nDoc.GroupType(0)
		If (ntype = "0" Or ntype="1") Then
			' add, build line, write it
			tmpstr = |"| & nDoc.ListName(0) & |","| & nDoc.InternetAddress(0) & |"|
			Call WriteLine(tmpstr, "1", sfilenum)
		End If
		' loop
		Set nDoc = nCol.GetNextDocument(nDoc)
	Wend
	
	' close file
	Call WriteFileFooter(sfilenum)
	
	' done
	Print "Done execution."
SExit:
	Exit Sub
	
SErrorHandler:
	Print "(Group E-mail Export - Initialize) Unexpected Error: " & Cstr(Err) & " " & Error$ & ", on line: " & Cstr(Erl) & ". <br>"
	Resume SExit
	
End Sub

Function DoFileKill(expfilepath As String) As Integer
	' kills passed file at expfilepath
	' returns 1 for success, 0 for error
	
	On Error Goto FErrorHandler
	
	' kill the file
	Kill expfilepath
	
	' return success
	DoFileKill = 1
DoExit:
	Exit Function
	
FErrorHandler:
	Print "(DoFileKill Function) Error" & Str(Err) & ": " & Error$ & " at line # " & Cstr(Erl)
	DoFileKill=0
	Resume DoExit
	
End Function

Function WriteFileHeader(expfilepath As String) As Integer
	Dim filenum As Integer		' filehandle
	Dim tempdir As String			' temp directory on server
	
	On Error Goto FErrorHandler
	
	' setup filepath
	filenum = Freefile
	
	' kill existing file if exists
	Call DoFileKill(expfilepath)
	
	' open up new file for write
	Open expfilepath For Output As filenum
	
	' return success as file handle
	WriteFileHeader = filenum
	Exit Function
	
FErrorHandler:
	Print "(WriteFileHeader) Error. Info: " & Str(Err) & ": " & Error$ & " on line: " & Cstr(Erl) & "<br>" 
	WriteFileHeader =0
	Exit Function
	
End Function

Function WriteLine(linetext As String, sendflag As String, filehandlenum As Integer) As Integer
	' writes a line either to file or browser depending on sendflag
	If (sendflag="1") Then
		Print # filehandlenum, linetext
	Else
		Print linetext
	End If
End Function

Function WriteFileFooter(filehandle As Integer) As Integer
	' closes open file
	Close filehandle
	' return success
	WriteFileFooter=1
End Function