asked on
Imports System
Imports System.Security
Imports System.IO
Imports System.Web.Mail
Imports System.Net.Mail
Module Module1
Dim gwRecipients As Object
Dim recipientFileLoc As String
Dim recipientFile(2) As String
Dim recipientNum As Integer
Dim emailHeader(2) As String
Dim gwApp As Object
Dim gwAccount As Object
Dim gwMailbox As Object
Dim gwMessages As Object
Dim gwNewMessage As Object
Dim MonarchObj As Object
Sub Main()
MonarchObj = CreateObject("Monarch32")
MonarchObj.DisplayWindow(2)
Dim reportTrailer As String
Dim reportTrailerDay As String
Dim reportTrailerEval As Integer
Dim report As String
Dim reportNames(2) As String
Dim reportMods(2) As String
Dim modExports(2) As String
Dim fileOpen As Boolean
Dim modOpen As Boolean
Dim fileExported As Boolean
Dim gwAttachment As Object
Dim gwSendMessage As Object
' Set up the variables for report names.
' For testing purposes
recipientFileLoc = "C:\Documents and Settings\WORS13818\My Documents\"
'recipientFile(1) = "Rpt1Recipients_test.txt"
'recipientFile(2) = "Rpt2Recipients_test.txt"
' Production
'recipientFileLoc = ""
recipientFile(1) = "Rpt1Recipients.txt"
recipientFile(2) = "Rpt2Recipients.txt"
emailHeader(1) = "Rpt1 Report Attached"
emailHeader(2) = "Rpt2 Report Attached"
reportNames(1) = "\\servername\Rpt1."
reportNames(2) = "\\servername\Rpt2."
reportMods(1) = "C:\Program Files\Monarch\Models\Rpt1.xmod"
reportMods(2) = "C:\Program Files\Monarch\Models\Rpt1.xmod"
modExports(1) = "C:\Program Files\Monarch\Export\Rpt1.xls"
modExports(2) = "C:\Program Files\Monarch\Export\Rpt2.xls"
' Generate trailing part of the file name
Console.WriteLine("Evaluating current date to get report name.")
reportTrailerEval = Month(Now)
' Converts month numbers to alphabetic letters if they go above 9
If reportTrailerEval < 10 Then
reportTrailer = CStr(reportTrailerEval)
ElseIf reportTrailerEval = 10 Then
reportTrailer = "A"
ElseIf reportTrailerEval = 11 Then
reportTrailer = "B"
ElseIf reportTrailerEval = 12 Then
reportTrailer = "C"
Else
reportTrailer = "0" ' In case of error with the month number so that the program will keep going.
End If
' Adds a zero before the month day if the day is less than 10
reportTrailerDay = IIf(CStr(Day(Now)) < 10, "0" + CStr(Day(Now)), CStr(Day(Now)))
reportTrailer = reportTrailer + reportTrailerDay
Dim reportProcess As Integer
Dim currentReport As String
Dim currentModName As String
For reportProcess = 1 To 2
' Determines what report is being run for display to the console window.
If reportProcess = 1 Then
currentReport = "Rpt1"
currentModName = "Rpt1"
Else
currentReport = "Rpt2"
currentModName = "Rpt2"
End If
' Process current report through Monarch
report = reportNames(reportProcess) + reportTrailer
Console.WriteLine("Opening " + currentReport + " File")
fileOpen = MonarchObj.SetReportFile(report, False)
Console.WriteLine("Opening " + currentModName + " Model")
modOpen = MonarchObj.SetModelFile(reportMods(reportProcess))
Console.WriteLine("Exporting " + currentReport + " File")
fileExported = MonarchObj.JetExportTable(modExports(reportProcess), "Master", 0)
Console.WriteLine("Sending " + currentReport + " File")
Try
' Open Groupwise and prepare to create new messages
gwApp = CreateObject("NovellGroupWareSession") 'ActiveX Control Error!
gwAccount = gwApp.Login
gwMailbox = gwAccount.Mailbox
gwMessages = gwMailbox.Messages
Catch openex As Exception
MsgBox(openex.ToString()) ' For testing purposes
'RecordError("Unable to access GroupWise.")
End Try
Try
' Create new message and holder for recipients
gwNewMessage = gwMessages.Add("GW.MESSAGE.MAIL", "Draft")
gwRecipients = gwNewMessage.Recipients
CreateEmailRecipients(reportProcess)
Console.WriteLine("Getting Current Username")
gwNewMessage.Subject = currentReport + " Report Attached"
gwNewMessage.BodyText = currentReport + " Report Attached"
Console.WriteLine("Attaching " + currentReport + " Report File")
gwAttachment = gwNewMessage.Attachments.Add(modExports(reportProcess))
Console.WriteLine("Sending Message")
gwSendMessage = gwNewMessage.Send
Catch gwerr As Exception
RecordError("Failure to create and send " + currentReport + ".")
End Try
Next
MonarchObj.Exit()
End Sub
' Creates the list of e-mail recipients to
Sub CreateEmailRecipients(ByVal filenum As Integer)
Dim emailFile As StreamReader
Dim tempRecipients(50) As String
Dim fullFileName As String
recipientNum = 0
' For testing
fullFileName = recipientFileLoc + recipientFile(filenum)
'fullFileName = recipientFile(filenum)
If File.Exists(fullFileName) Then
emailFile = New StreamReader(fullFileName)
Try
While emailFile.EndOfStream = False
recipientNum = recipientNum + 1
tempRecipients(recipientNum) = emailFile.ReadLine
End While
Catch ex As Exception
RecordError("Unable to read e-mail recipients from " + recipientFile(filenum))
Finally
emailFile.Close()
End Try
Dim counter As Integer
For counter = 1 To recipientNum
gwRecipients.Add(tempRecipients(counter))
Next counter
Else
RecordError("The following e-mail recipient file was not found: " + recipientFile(filenum))
End If
End Sub
' Records any errors into ErrorLog.txt file. Then closes the program to prevent further errors.
Sub RecordError(ByVal errorDesc As String)
Dim errorLogName As String
Dim errorLog As StreamWriter
' Open or create the error log file
errorLogName = "ErrorLog.txt"
If File.Exists(errorLogName) Then
errorLog = New StreamWriter(errorLogName, FileMode.Append)
Else
errorLog = New StreamWriter(errorLogName, FileMode.CreateNew)
End If
errorLog.WriteLine(CStr(Now))
errorLog.WriteLine(errorDesc)
errorLog.WriteLine() ' Add three carriage returns to the end of each error recorded.
errorLog.WriteLine()
errorLog.WriteLine()
' Exit the program after recording the error
errorLog.Close()
MonarchObj.Exit()
End
End Sub
End Module