Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

VB Script to Send Email via Outlook

$
0
0

Good afternoon, all -

I have a VBA script that runs as an Excel macro to send an email to a distribution list. this is working fine and I was wanting to convert a copy of it for use in a VB Script task in SSIS.

I thought I'd be able to simply copy the script over and have no problems, but, apparently, I shouldn't think.

I've been looking up scripts to do the task online, but the ones I find don't seem to be at all similar to what I'm already using in my macro. I guess this is not exactly unexpected, but I figured VB is VB, whether it's Excel or SSIS. But, maybe not.

Anyway, the code I'm using is;

'*****  Set date values.

    Dim sYear As String = Year(Now)
    Dim sMonth As String = Format(Month(Now), "00")
    Dim sDay As String = Format(Day(Now), "00")

'*****  Define the distribution list
    
    Dim 'Recipient_1 As String = "John.Doe@domain.org"
    Dim 'Recipient_2 As String = "Herman.Munster@domain.org"
    Dim 'DL_1 As String = "Big.Group@domain.org"

'*****  Create the email

    Set oOutApp = New Outlook.Application
    Set oOutMail = oOutApp.CreateItem(0)
    Set objDoc = oOutMail.GetInspector.WordEditor
    Set objBkm = objDoc.Bookmarks("_MailAutoSig")

'*****  Get Outlook Account Number

    Dim OutApp As Outlook.Application

    Set OutApp = CreateObject("Outlook.Application")

    For I = 1 To OutApp.Session.Accounts.count
        If Right(OutApp.Session.Accounts.Item(I), 21) = "@specific_account.org" Then Acct_Num = I
    Next I
    'MsgBox OutApp.Session.Accounts.Item(Acct_Num)

'*****  Create the email subject line

    sSubj = "Test Email"

'*****  Create the message for the email body

    sMsg = "Test message."
    On Error Resume Next

'*****  Populate the email fields and send it

    With oOutMail
        Set .SendUsingAccount = oOutApp.Session.Accounts.Item(Acct_Num)
        .Recipients.Add (Recipient_1)
        .Recipients.Add (Recipient_2)
        .Recipients.Add (DL_1)
        .Subject = sSubj
        .HTMLBody = sMsg & .HTMLBody
        .Attachments.Add ("C:\temp\Chart1.png")
        .Send
        '.Display
    End With
    On Error GoTo 0
    Set oOutMail = Nothing
    Set oOutApp = Nothing

I have the Get Outlook Account Number in there, because we recently changed our corporate email addresses, but still maintain the old address for the foreseeable future. However, we need to ensure that all outgoing emails use the new addresses. So, I have that test to get the right account.

SSIS' VB Script Task didn't like the date values as I had them, so I had to add the "Dim … As String …" etc. in theSet date values section at the top. Originally, they were just sYear = Year(Now), etc. Same goes for the strings in the Define the distribution listsection.

The VB in SSIS also seems to gag on the Set command. Seems to want it removed. But, "Set .SendUsingAccount = oOutApp.Session.Accounts.Item(Acct_Num)" in thePopulate the email fields and send it section requires it in Excel. The specific account will not be selected, if that line is there without theSet.

So, apparently, this code will not work as is. Does anyone have a simple little email routine that will let me send emails using Outlook and that allows me to select a specific account?

Thanx in advance for any assistance!


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>