At work we use BusinessObjects and publish reports through its Broadcast Agent, which is a tool for scheduling reports to be run and publishing to the BusinessObjects Web Intelligence ASP application. The "full client" has VBA macro capabilities supported by a complete BusinessObjects object model. With that being said, I have created a piece of macro code to automate the publishing process and email the report, either in the standard BO report form, PDF, or Excel XLS spreadsheet format. The class below is the email component. The original came from somewhere here on the web... When I find the original author, I'll update this posting.
Imports System.Web.Mail
Imports Microsoft.VisualBasic.Compatibility.VB6
Namespace Mail
Public Class SMTP
Private mTo As String
Private mFrom As String
Private mSubject As String
Private mMessage As String
Private mAttachments As New Collection
Private mServer As String = "localhost"
Public Sub New()
' Needed for COM Interop.
' Do not remove.
' Nothing to see here, move along...
End Sub
Public Property [To]() As String
Get
Return mTo
End Get
Set(ByVal Value As String)
mTo = Value
End Set
End Property
Public Property From() As String
Get
Return mFrom
End Get
Set(ByVal Value As String)
mFrom = Value
End Set
End Property
Public Property Subject() As String
Get
Return mSubject
End Get
Set(ByVal Value As String)
mSubject = Value
End Set
End Property
Public Property Message() As String
Get
Return mMessage
End Get
Set(ByVal Value As String)
mMessage = Value
End Set
End Property
Public Property Attachments() As Collection
Get
Return mAttachments
End Get
Set(ByVal Value As Collection)
mAttachments = Value
End Set
End Property
Public Property Server() As String
Get
Return mServer
End Get
Set(ByVal Value As String)
mServer = Value
End Set
End Property
Public Sub Send()
Try
Dim mail As New MailMessage
With mail
.From = Trim(Me.From)
.To = Trim(Me.To)
.Subject = Trim(Me.Subject)
.Body = Trim(Me.Message)
If Me.Attachments.Count > 0 Then
Dim file As String
For Each file In mAttachments
.Attachments.Add(New MailAttachment(Trim(file)))
Next
End If
End With
SMTPMail.SmtpServer = mServer
SMTPMail.Send(mail)
Catch ex As Exception
Throw New System.Exception(ex.Message, CType(ex, System.Exception))
End Try
End Sub
End Class
End Namespace
Some notes about this code:
- Since this is a COM Interop class, a few things are needed.
- Microsoft.VisualBasic.Compatibility.VB6
: This has the classic Collections object in it. If you aren't using COM, this can easily be replaced with an ArrayList. - A New method. This is the default constructor that COM needs. You can initialize any default values in here, but in this case all I needed was an empty contructor.
- Microsoft.VisualBasic.Compatibility.VB6
- The To property takes a comma delimited list of email addresses.
- The attachment collection needs to be a colelction of strings. These strings would be the file names with path.
Pretty cool, huh?
The other thing I learned was the use of Shared (VB.NET) and static (C#). I found out how to create a Singleton instance of a class and use it correctly (I think)...
Here's a VB.NET sample:
Namespace SingletonExample
Friend Class SingletonClass
Private Shared mSingletonClass As SingletonClass = Nothing
Public Shared ReadOnly Property SingletonClass() As SingletonClass
Get
If mSingletonClass Is Nothing Then
mSingletonClass = New SingletonClass
End If
Return mSingletonClass
End Get
End Property
Shared Sub New()
' Nothing to see here, keep moving...
End Sub
Public Shared Sub Initialize()
Console.WriteLine("In SingletonExample.SingletonClass")
End Sub
End Class
End Namespace
To call this, simply use the following:
SingletonExample.SingletonClass.Initialize()
I'm not totally clear yet on how this exactly works, but I'm a lot closer and can implement this methodology in my apps.
Looks like I'm a little more learned in .NET!
No comments:
Post a Comment