+ Reply to Thread
Results 1 to 4 of 4

Automatic Email Macro

Hybrid View

Jrockusa Automatic Email Macro 01-22-2010, 11:22 AM
Richard Buttrey Re: Automatic Email Macro 01-22-2010, 12:19 PM
Jrockusa Re: Automatic Email Macro 01-22-2010, 12:36 PM
Richard Buttrey Re: Automatic Email Macro 01-22-2010, 12:55 PM
  1. #1
    Registered User
    Join Date
    10-14-2009
    Location
    Milwaukee, WI
    MS-Off Ver
    Excel 2003
    Posts
    10

    Automatic Email Macro

    Hello,

    I am using the below code to automatically email excel reports via Lotus Notes and it works great. My question is that at times I will run this macro and there will be no report to send that day (an error obviously will occur as the macro cant locate the file). Is there an IF, THEN statement I can include in the code so if the file does not exist the macro should simply end? Any help would be appreciated. Thanks!

    'Set up the objects required for Automation into lotus notes
        Dim Maildb As Object 'The mail database
        Dim UserName As String 'The current users notes name
        Dim MailDbName As String 'THe current users notes mail database name
        Dim MailDoc As Object 'The mail document itself
        Dim AttachME As Object 'The attachment richtextfile object
        Dim Session As Object 'The notes session
        Dim EmbedObj As Object 'The embedded object (Attachment)
        Dim stSignature As String
        
        'Start a session to notes
        Set Session = CreateObject("Notes.NotesSession")
        'Get the sessions username and then calculate the mail file name
        'You may or may not need this as for MailDBname with some systems you
        'can pass an empty string
        UserName = Session.UserName
        MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
        'Open the mail database in notes
        Set Maildb = Session.GETDATABASE("", MailDbName)
         If Maildb.IsOpen = True Then
              'Already open for mail
         Else
             Maildb.OPENMAIL
         End If
        'Set up the new mail document
        Set MailDoc = Maildb.CREATEDOCUMENT
        MailDoc.Form = "Memo"
        
        'Attach Your Signature
        stSignature = Maildb.GetProfileDocument("CalendarProfile") _
                                                .GetItemValue("Signature")(0)
        
        'MailDoc.Recipient = Recipient
        Addressee = "Jerad.Pearson@usbank.com"
        Recipient = Split(Addressee, ",")
        MailDoc.sendto = Recipient
        
        'MailDoc.Recipient = CopyTo
        'MailDoc.copyto = "Jerad.Pearson@usbank.com"
        
        'MailDoc.Recipient = BlindCopyTo
        'MailDoc.blindcopyto = "Jerad.Pearson@usbank.com"
        
        'MailDoc.Subject = Subject
        MailDoc.Subject = "Retail Processing Report"
        'MailDoc.Body = BodyText
        MailDoc.Body = "Hello,  Attached you will find a copy of your Error Report." & stSignature
        'MailDoc.SAVEMESSAGEONSEND = SaveIt
        MailDoc.SAVEMESSAGEONSEND = True
        
        'Set up the embedded object and attachment and attach it
        Attachment = "G:\Share\Financial and Quality Control and Training\Reporting\Transfer Agency\Data Information\Processing Team 1.xls"
        If Attachment <> "" Then
            Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
            Set EmbedObj = AttachME.EmbedObject(1454, "", Attachment, "Attachment")
           
        End If
        'Send the document
       MailDoc.SEND 0, Recipient
        'Clean Up
        Set Maildb = Nothing
        Set MailDoc = Nothing
        Set AttachME = Nothing
        Set Session = Nothing
        Set EmbedObj = Nothing
    End Sub

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Automatic Email Macro

    Hi,

    Try putting an 'Else' in the bit of code which attaches the file. e.g

     If Attachment <> "" Then
            Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
            Set EmbedObj = AttachME.EmbedObject(1454, "", Attachment, "Attachment")
     Else
            Exit Sub
     End If
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    10-14-2009
    Location
    Milwaukee, WI
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Automatic Email Macro

    Hello Richard,

    I did as you said and it did not work. I get the below error message:

    Run-time error '7225':

    File G:\Share\Financial and Quality Control and Training\Reporting\Transfer Agency\Data Information\Processing Team 1.xls not found

    Any additional thoughts?

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Automatic Email Macro

    Hi,

    Sorry, my fault. I missed the point that the attachment is actually being predefined and that filename therefore always exists as a string but not necessarily as a file.

    Try instead

    If Attachment <> "" Then
            On Error Goto NoFile
            Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
            Set EmbedObj = AttachME.EmbedObject(1454, "", Attachment, "Attachment")
    End If
        'Send the document
       MailDoc.SEND 0, Recipient
        'Clean Up
        Set Maildb = Nothing
        Set MailDoc = Nothing
        Set AttachME = Nothing
        Set Session = Nothing
        Set EmbedObj = Nothing
        On Error Goto 0
    Exit Sub
    
    NoFile:
    Exit Sub
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1