Sub Initialize

' dims used with these functions

Dim db As NotesDatabase			' this/current db
Dim sendtonm As String			' the recipient of this report when mailed
Dim expfilenm As String			' file attachment name
Dim expsendflag As String		' "0" to download, "1" to send in e-mail
Dim filehandlenum As Integer		' filehandle to attachment file being generated
Dim msubject As String			' memo subject for SendFile function
Dim mbodyintro As String		' memo body intro for SendFile function

....

filehandlenum = FileOpenWrite(expfilenm)
If (filehandlenum = 0) Then
	' error occured opening file
	Print |Unabled to Open File. Aborting Export.|
	Exit Sub
End If

....

Call FileWriteLine("string to write", expsendflag, filehandlenum)

....


Call FileCloseWrite(filehandlenum)

....


Call SendFile(db, sendtonm, expfilenm, msubject, mbodyintro)

....

If (FileKill(expfilenm) = 0 ) Then
	' error occured killing file
	Print |Unabled to remove temporary File, | & expfilenm & |, from temporary directory. Remove manually.|
End If

....


Exit Sub


_____________________________________


Function FileOpenWrite(expfilenm As String) As Integer
	Dim filenum As Integer		' filehandle
	Dim tempdir As String		' temp directory on server
	Dim fname As String		' filename/filepath
	
	On Error Goto FErrorHandler
	
	' setup filepath
	filenum = Freefile
	tempdir = Environ("Temp")
	fname = tempdir & "\" & expfilenm & ".xls"
	
	' open up new file for write
	Open fname For Output As filenum
	
	' return success as file handle
	FileOpenWrite = filenum
	Exit Function
	
FErrorHandler:
	Print "(FileOpenWrite) Error. Info: " & Str(Err) & ": " & Error$ & " on line: " & Cstr(Erl) & "<br>" 
	FileOpenWrite =0
	Exit Function
	
End Function

Function FileOpenRead(inpfilenm As String) As Integer
	Dim filenum As Integer		' filehandle
	Dim tempdir As String		' temp directory on server
	Dim fname As String		' filename/filepath
	
	On Error Goto FErrorHandler
	
	' setup filepath
	filenum = Freefile
	tempdir = Environ("Temp")
	fname = tempdir & "\" & inpfilenm & ".xls"
	
	' open up new file for input
	Open fname For Input As filenum
	
	' return success as file handle
	FileOpenRead = filenum
	Exit Function
	
FErrorHandler:
	Print "(FileOpenRead) Error. Info: " & Str(Err) & ": " & Error$ & " on line: " & Cstr(Erl) & "<br>" 
	FileOpenRead =0
	Exit Function
	
End Function


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


Function FileCloseWrite(filehandlenum As Integer) As Integer
	' closes open file
	Close filehandlenum

	' return success
	FileCloseWrite=1
End Function

Function FileCloseRead(filehandlenum As Integer) As Integer
	' closes open file
	Close filehandlenum

	' return success
	FileCloseRead=1
End Function


Function SendFile(db As NotesDatabase, sendtonm As String, expfilenm As String, msubject As String,_
mbodyintro As String) As Integer
	' sends created attachment file, in e-mail
	' db - current db, used to create mDoc
	' sendtonm - person to whom to send attachment
	' expfilenm - attachment filename/filepath
	' msubject - subject of memo
	' mbodyintro - body intro text before attachment

	Dim tempdir As String			' temp directory on server
	Dim fname As String			' filename/filepath to file to attach
	Dim mDoc As NotesDocument		' memo doc
	Dim mBody As NotesRichTextItem		' Body field of mDoc
	
	On Error Goto FErrorHandler
	On Error 4294 Goto UserNotFoundHandler
	
	' get file full path
	tempdir = Environ("Temp")
	fname = tempdir & "\" & expfilenm
	' create memo
	Set mDoc = db.CreateDocument()
	mDoc.Form = "Memo"
	mDoc.SaveMessageOnSend = False
	mDoc.From = sendtonm
	mDoc.ReplyTo = sendtonm
	mDoc.Principal = sendtonm
	mDoc.INetFrom = sendtonm
	mDoc.SendTo = sendtonm
	mDoc.Subject = msubject
	Set mBody = mDoc.CreateRichTextItem("Body")
	Call mBody.AppendText(mbodyintro)
	Call mBody.AddNewline(2)
	Call mBody.EmbedObject(EMBED_ATTACHMENT, "", expfilenm)
	Call mDoc.Send(False)
	
	' return success as file handle
	SendFile = 1
Exit Function


FErrorHandler:
	Print |(SendFile) Error| & Str(Err) & |: | & Error$ & | at line # | & Cstr(Erl)
	SendFile=0
	Exit Function
	
UserNotFoundHandler:
	Print |(SendFile) Unabled to send to, | & sendtonm & |. User not found in the Domino Directory(s).|
	SendFile=0
	Exit Function

End Function



Function FileKill(expfilenm as String) As Integer
	
	On Error Goto FErrorHandler

	' kill any existing temp file
	Kill expfilenm
	
	' return success
	FileKill = 1

	Exit Function

FErrorHandler:
	Print |(FileKill) Error| & Str(Err) & |: | & Error$ & | at line # | & Cstr(Erl)
	FileKill = 0
	Exit Function

End Function