Sub Initialize
Dim ws As New notesuiworkspace
Dim tabs As Variant
Dim xlsFileName As Variant
Dim y As Integer
Dim i As Double, k As Double, m As Double, n As Double
Set session = New NotesSession
Set exportdb = session.CurrentDatabase
GetServerAndPath server, path
'Start the Notes Log
Set npslog = New NotesLog("Export Brio Report Agent")
npslog.OPENNOTESLOG server, path & exportdb.filename
npslog.LogAction "*** BRIO REPORT EXPORT STARTED (" & Cstr(Now) & ") ***"
'#########################################################################
' Start the process of export to Excel by gathering some information from the user
'#########################################################################
' Get the path and filename from user profile
Dim profileview As NotesView
Set profileview = exportdb.GetView( "Profile" )
Dim profiledoc As NotesDocument
Set profiledoc = profileview.GetFirstDocument
If profiledoc Is Nothing Then
'Get appropriate file names when required
Dim szFilter$
szFilter = "Excel Spreadsheet|*.xls|All Files|*.*|"
'Prompt user where to save the file
xlsFileName = ws.OpenFileDialog(False, "Please choose a location for this spreadsheet", szFilter,"","Resource.xls")
If xlsFileName(0) ="" Then
Messagebox "You have not selected a location for this spreadsheet. Please retry again.", 48, "Selection Error"
Exit Sub
End If
Else
Redim xlsFileName(0) As String
xlsFileName(0) = profiledoc.path(0) & profiledoc.filename(0)
End If
'********************************* Checks for Target Path **************************************
'On Error Resume Next needs to be included to allow the code below to check for errors and continue
On Error Resume Next
Dim TargetPath$
TargetPath = profiledoc.path(0)
Chdir TargetPath 'Check that the TargetPath exists
If Err = 76 Then 'If it did not exist, peform these tasks
Mkdir TargetPath 'Create the TargetPath
Err = 0 'Reset Error Number for next check
Chdir TargetPath 'Verify that the TargetPath was successfully created
If Err = 76 Then 'If it wasn't created successfully
Msgbox "Excel Path does not exist or could not be created, please specify another path in the profile document.", 0 + 16, "Invalid Path"
End If
End If
'****************************************************************************************************
On Error Goto errorHandler
' Get the array of tabs to be processed
Set headerview = exportdb.GetView("ColumnHeaders")
Set controlview = exportdb.GetView("NPSM")
tabs = Evaluate("@Unique(@DbColumn("""":""NoCache"";"""";""NPSM"";2))")
Dim RangeBounds() As String
Redim RangeBounds(Ubound(tabs)) As String
y = 1
Forall t In tabs
'Open File and Write Header
filenum = Freefile()
outputfile = profiledoc.path(0) & t & ".csv"
Open outputfile For Output As filenum
'Process column headers to the tab
Call AddColumnHeaders(t, headerview)
Print "Processing contents for tab: " & Ucase(t)
npslog.LogAction " Processing contents for tab: " & Ucase(t)
'Get the first detail record to set the source view. All control documents must
'be referencing the same view. Mixing source views will cause problems in data.
Set detailvcollection = headerview.GetAllEntriesByKey(t, True)
Set detailentry = detailvcollection.GetfirstEntry
Set detaildoc = detailentry.Document
sourceviewname = detaildoc.NPS_SourceView(0)
'Set the source database and view
If detaildoc.NPS_SourceDB(0) = "Compensation Manager" Then
Set compdb = session.GetDatabase(server, path & "eriscomp.nsf" )
If Not(compdb.IsOpen) Then 'Verify if db is open
Messagebox "The Compensation Database could be opened.", 48, "Database Open Error"
Exit Sub
End If
Set sourceview = compdb.GetView(sourceviewname)
Elseif detaildoc.NPS_SourceDB(0) = "Brio Reporting" Then
'No need to check current db
Set sourceview = exportdb.GetView(sourceviewname)
Else
Set erisdb= session.GetDatabase(server, path & "eris.nsf" )
If Not(erisdb.IsOpen) Then 'Verify if db is open
Messagebox "The Filing Cabinet Database could be opened.", 48, "Database Open Error"
Exit Sub
End If
Set sourceview = erisdb.GetView(sourceviewname)
End If
If sourceview Is Nothing Then
Messagebox "You specified an incorrect source view: " & sourceviewname & ". Please retry again.", 48, "Source View Error"
Exit Sub
End If
Set docvcollection = sourceview.AllEntries
'Populate only when there are records to process
If docvcollection.count > 0 Then
npslog.LogAction " " & Cstr(docvcollection.count) & " records for tab: " & Cstr(Ucase(t))
Call DetailInstruction(detailvcollection)
Set docentry = docvcollection.getfirstentry
i = 1
Do
Set doc = docentry.document
Print "Processing Row: " & Cstr(i) & " of " & (docvcollection.count) & " for tab: " & Ucase(t)
Redim detailarray(0 To (detailvcollection.count - 1)) As String
For k = 0 To (detailvcollection.count -1)
detailarray(k) = ProcessDetailRecord(doc, DetailEntryType(k), DetailFieldName(k), DetailFieldType(k), DetailReplaceNull(k), DetailReplaceValue(k), DetailDateFormat(k))
Next
n = 0
detailtxt = ""
Forall d In detailarray
If n = 0 Then
detailtxt = Chr(34) & d & Chr(34)
Else
detailtxt = detailtxt & "," & Chr(34) & d & Chr(34)
End If
n = n + 1
End Forall
Print #filenum, detailtxt
i = i + 1
Set docentry = docvcollection.getnextentry(docentry)
Loop Until docentry Is Nothing
Close filenum
End If
'Populate Rangebounds
RangeBounds(y-1) = CalculateRangeBounds (detailvcollection.Count) & docvcollection.count + 1
'increment to next tab
y = y + 1
End Forall
npslog.LogAction " Converting to Excel (" & Cstr(Now) & ") ***"
Call CopyCSVtoExcel ( Tabs , RangeBounds, TargetPath, Cstr(xlsFilename(0)) )
npslog.LogAction "*** BRIO REPORT EXPORT COMPLETE (" & Cstr(Now) & ") ***"
Print "BRIO Report Export is complete."
Exit Sub
errorHandler:
Messagebox "Error was encountered. Please make sure that all the control documents are correct."
End Sub