Results 1 to 10 of 10

Thread: Send Lotus Notes email with attachment via VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location

    Send Lotus Notes email with attachment via VBA

    Trying to help someone who uses Lotus Notes and wants to have VBA generate a workbook to be attached to an email and then send the email

    I don't have Lotus Notes so it's been difficult to debug by sending emails and screen shots back and forth

    The test macro to simulate the real attachment runs OK (the easy part), and the Send Email macro seems to run, but there is no email message generated and sent.

    Hoping someone with Lotus Notes experience (and even better access to Notes) can let me know what I have to do


    Option Explicit
    
    Const sYourEmail As String = "YourEmail@Somewhere.com"  '   see if you can send to yourself
    
    Sub TestMail()
        Dim wbEmail As Workbook
        Dim sFilename As String, sName As String
        
        'simulate making the real workbook to be attached
        'init
        sName = Application.UserName
        sFilename = ThisWorkbook.Path & Application.PathSeparator & sName & ".xlsx"
    
        'delete file if it exists
        On Error Resume Next
        Application.DisplayAlerts = False
        Kill sFilename
        Application.DisplayAlerts = True
        On Error GoTo 0
        
        'create attachment workbook -- JUST A TEST
        Application.ScreenUpdating = False
        ThisWorkbook.Worksheets("Sheet2").Copy
        Set wbEmail = ActiveWorkbook
        ThisWorkbook.Worksheets("Sheet3").Copy after:=wbEmail.Worksheets(wbEmail.Worksheets.Count)
        ThisWorkbook.Worksheets("Sheet4").Copy after:=wbEmail.Worksheets(wbEmail.Worksheets.Count)
        ThisWorkbook.Worksheets("Sheet5").Copy after:=wbEmail.Worksheets(wbEmail.Worksheets.Count)
        wbEmail.SaveAs Filename:=sFilename, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        wbEmail.Close
        Application.ScreenUpdating = True
        Stop
        
        'This is the part where i can't test - see if you can send email to yourself
        Call SendNotesMail("Update (" & Format(Now, "yyyy-mm-dd") & ")", sFilename, sYourEmail, "Please Review")
    
    End Sub


    Option Explicit
    
    'https://www.mrexcel.com/forum/excel-questions/959083-vba-script-send-mail-through-lotus-notes.html
    
    Sub SendNotesMail(Subject As String, Attachment As String, Recipient As String, BodyText As String)
        
        Const EMBED_ATTACHMENT As Long = 1454
        Const RICHTEXT As Long = 1
    
        Dim notesSession As Object
        Dim notesDatabase As Object
        Dim notesDocument As Object
        Dim notesEmbedObject As Object
        Dim notesAttachment As Object
    
        Stop
    
        'Instantiate the Lotus Notes COM's Objects.  ---- This is where it fails for me since I don't have Lotus Notes
        Set notesSession = CreateObject("Notes.NotesSession")
        Set notesDatabase = notesSession.GETDATABASE("", "")
    
        'If Lotus Notes is not open then open the mail-part of it.
        If notesDatabase.IsOpen = False Then notesDatabase.OPENMAIL
    
        'Create the e-mail and the attachment.
        Set notesDocument = notesDatabase.CREATEDOCUMENT
        Set notesAttachment = notesDocument.CREATERICHTEXTITEM(Attachment)
    
    '    Set notesEmbedObject = notesAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
        'Add values to the created e-mail main properties.
        With notesDocument
            .Form = "Memo"
            .sendto = Recipient
            .Subject = Subject
            .Body = BodyText
            .SAVEMESSAGEONSEND = True
            .PostedDate = Now()
            .SEND 0, Recipient
        End With
    
        'Release objects from memory.
        Set notesEmbedObject = Nothing
        Set notesAttachment = Nothing
        Set notesDocument = Nothing
        Set notesDatabase = Nothing
        Set notesSession = Nothing
        MsgBox "The e-mail has successfully been created and distributed", vbInformation
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 09-13-2018 at 01:00 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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