VBA Code: Lotus Notes Email Preparation From Excel Worksheet

Flani

New Member
Joined
Jul 11, 2012
Messages
17
Hi All,


I managed to get a code that will automate my email preparation. The code gets recipients, subject and the body of the mail from the active worksheet and it works perfectly for the recipients and subject, not so much for the body.


Say the mail body is c1, c2, c3, c4, c5, c6, c7 and c8


C1 is a blank cell.


C2= Hi All,
(C1 here for space)
C3= Please open the file in:
(C1 here for space)
C4= is the filepath plus a formula that updates the date of the file. The font should be in blue
(C1 here for space)
C5: is another body of the mail that should be italicized and in color red
(C1 here for space)
A screenshot "mypic" should be added in this line.
(C1 here for space)
C6=another part of the mail that should be in bold letters and color red
(C1 here for space)
C7=another part of the body of the mail



The code only finishes up to C5 and adds the attachment but without the additional formatting. The other part of the body cannot be captured bythe code anymore after the attachment has been pasted. Can you help me with a code please. Thanks
 
Dear John,

could you support me with knowledge how to add to your code document.autospell="0" before sending.

Regards
Adrian
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I'm getting this popup when I run the code, any ideas?

3f95ade6ce.png
 
Upvote 0
See if you can adapt this code to your specific requirements. It does everything you asked for, including different font styles and colours, an embedded image, file attachment and automatic signature, if defined in your Notes User preferences. You just need to edit the code in 2 sections where indicated to retrieve values from the relevant Excel cells. You shouldn't need to change any other parts of the code.

In Tools - References in the VB editor, you must tick the reference to Lotus Domino Objects for this code to compile successfully.
Code:
Public Sub Send_Notes_Email()

'Requires reference to Lotus Domino Objects (domobj.tlb) for constants such as EMBED_ATTACHMENT and FONT_HELV, etc.
'Code based on answer by Bill-Hanson:
'http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/Lotus_SmartSuite/Lotus_Notes/Q_22733557.html#a19616928

    Dim NSession As Object
    Dim NUIWorkspace As Object
    Dim NMailDb As Object
    Dim NDocumentTemp As Object
    Dim NUIDocumentTemp As Object
    Dim NUIDocument As Object
    Dim NRTItemBody As Object
    Dim NRTStyle As Object, NRTStyleDefault As Object
    Dim NRTItemAttachment As Object, embeddedAttachment As Object
    Dim Subject As String
    Dim SendTo As String, CopyTo As String, BlindCopyTo As String
    Dim fileAttachment As String
    Dim embedCells As Range
    Dim FSO As Object
    Dim tempFolder As String, tempCellsJPG As String
    Dim Copy_and_Paste As Boolean
    
    '--------- EDIT USER-DEFINED SETTINGS IN THIS SECTION ---------
    
    'The Excel cells to be included in the email body as an image
    
    Set embedCells = ActiveSheet.Range("A1:C8")
        
    'The file to be attached to the email, if it exists
    
    fileAttachment = "C:\folder1\folder2\file.txt"
    
    SendTo = "email1@email.com,email2@email.com"
    CopyTo = "email2@email.com"
    BlindCopyTo = ""
    Subject = "Email subject"
    
    '--------- END OF USER-DEFINED SETTINGS ---------
    
    'Copy_and_Paste flag
    'True = copy and paste Excel cells into email body using the clipboard
    'False = save Excel cells as a temporary .jpg file and import into email body
    
    Copy_and_Paste = True
        
    Set FSO = CreateObject("Scripting.FileSystemObject")
    tempFolder = FSO.GetSpecialFolder(2)
    
    'File name for temporary .jpg file containing Excel cells
   
    tempCellsJPG = tempFolder & "\" & Replace(FSO.GetTempName(), ".tmp", ".jpg")
    
    Set NSession = CreateObject("Notes.NotesSession")   'OLE (late binding only) because we access Notes UI classes
    Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
    Set NMailDb = NSession.GetDatabase("", "")
    NMailDb.OpenMail
   
    'Create the default rich text style
    
    Set NRTStyleDefault = NSession.CreateRichTextStyle
    With NRTStyleDefault
        .NotesColor = COLOR_BLACK
        .FontSize = 10
        .NotesFont = FONT_HELV
        .Bold = False
        .Italic = False
    End With
    
    Set NRTStyle = NSession.CreateRichTextStyle
   
    'Create a temporary NotesDocument
    
    Set NDocumentTemp = NMailDb.CreateDocument
    With NDocumentTemp
        .Form = "Memo"
        
        'Add a rich text item to contain the email body text and file attachment
        
        Set NRTItemBody = .CreateRichTextItem("Body")
        With NRTItemBody
            
            '--------- ADD/EDIT CODE IN THIS SECTION FOR THE EMAIL BODY TEXT ---------
            
            'Compose the email body text
            
            .AppendText "1st paragraph - default font."
            .AddNewLine 2

            With NRTStyle
                .NotesFont = FONT_ROMAN
                .FontSize = 14
                .NotesColor = COLOR_BLUE
                .Bold = True
            End With
            .AppendStyle NRTStyle
            .AppendText "2nd paragraph - Times New Roman Blue 14 Bold"
            .AddNewLine 2
        
            'Add placeholder text which will be replaced by the Excel cells
        
            .AppendText "{PLACEHOLDER}"
            .AddNewLine 2
            
            With NRTStyle
                .NotesFont = FONT_HELV
                .FontSize = 10
                .NotesColor = COLOR_RED
                .Italic = True
            End With
            .AppendStyle NRTStyle
            .AppendText "3rd paragraph - Helvetica Red 10 italic."
            
            'Same paragraph, default style
            
            .AppendStyle NRTStyleDefault
            .AppendText "  Excel cells are shown above."
            
            If fileAttachment <> "" Then
                .AddNewLine 2
                .AppendText fileAttachment & " attached"
                .AddNewLine 1
                .EmbedObject EMBED_ATTACHMENT, "", fileAttachment
                .AddNewLine 1
            End If
            
            '--------- END OF EMAIL BODY TEXT SECTION --------
            
        End With
        
        .Save False, False
    End With
   
    'Display the temporary document in the UI
    
    Set NUIDocumentTemp = NUIWorkspace.EditDocument(True, NDocumentTemp)
   
    'Copy the rich text to the clipboard, close the window, and delete the temp doc
    
    With NUIDocumentTemp
        .gotofield "Body"
        .SelectAll
        .Copy
        'The next 2 lines are not needed
        '.Document.SaveOptions = "0" 'prevent prompt
        '.Document.MailOptions = "0" 'prevent prompt
        .Close                      'therefore temp UI doc not saved
    End With
    NDocumentTemp.Remove True

    'Compose the real email document
    
    Set NUIDocument = NUIWorkspace.ComposeDocument(NMailDb.Server, NMailDb.filePath, "Memo")
    'Set NUIDocument = NUIWorkspace.ComposeDocument(, , "Memo")      'use local computer and current database
    With NUIDocument
        .FieldSetText "EnterSendTo", SendTo
        .FieldSetText "EnterCopyTo", CopyTo
        .FieldSetText "BlindCopyTo", BlindCopyTo
        .FieldSetText "Subject", Subject

        'The memo now has everything except the rich text from the temporary UI document and the Excel cells image.
        'The automatic signature (if defined in User Preferences) should be at the bottom of the memo.  Now, we just
        'paste the rich text and Excel cells into the body
        
        .gotofield "Body"
        .Paste
   
        'Replace the placeholder text with the Excel cells image
        
        .gotofield "Body"
        .FindString "{PLACEHOLDER}"
        '.DESELECTALL                   'Uncomment to leave the placeholder text in place (cells are inserted immediately before it)
        
        If Copy_and_Paste Then
            embedCells.CopyPicture xlBitmap
            .Paste
            Application.CutCopyMode = False
        Else
            Save_Object_As_JPG embedCells, tempCellsJPG
            .Import "JPEG Image", tempCellsJPG
            Kill tempCellsJPG
        End If

        'Set NotesDocument options to save and send the email without prompts when the Close method is called
        
        .Document.SaveOptions = "1"
        .Document.MailOptions = "1"
        
        .Close
    End With
    
End Sub


'Based on http://www.jpsoftwaretech.com/export-excel-range-to-a-picture-file/

Private Sub Save_Object_As_JPG(saveObject As Object, JPGfileName As String)

    'Save a picture of an object as a JPG/JPEG file
    
    'Arguments
    'saveObject     - any object in the CopyPicture method's 'Applies To' list, for example a Range or Shape
    'JPGfileName    - the file name (including folder path if required) to save the picture as
    
    Dim temporaryChart As ChartObject
     
    Application.ScreenUpdating = False
    
    saveObject.CopyPicture xlScreen, xlPicture
    
    Set temporaryChart = ActiveSheet.ChartObjects.Add(0, 0, saveObject.Width, saveObject.Height)
    With temporaryChart
        .Border.LineStyle = xlLineStyleNone      'No border
        .Chart.Paste
        .Chart.Export JPGfileName
        .Delete
    End With
    
    Application.ScreenUpdating = True
    
    Set temporaryChart = Nothing
    
End Sub



Hi there,

I trying to use this code to create an email on lotus with a picture and an attached file, exactly as the code above, it works perfectly, don't give any error but do not attach the file. I've tried with an excel file and a txt but no success.
I searched other examples and I can send an email with the pictures and text or only with the file attached, but not all together...
Anyone can help me with?

thanks
 
Upvote 0
Hi there,

I trying to use this code to create an email on lotus with a picture and an attached file, exactly as the code above, it works perfectly, don't give any error but do not attach the file. I've tried with an excel file and a txt but no success
I suspect you haven't set the reference to Lotus Domino Objects, which is needed for constants such as EMBED_ATTACHMENT and FONT_ROMAN. Also, Option Explicit added at the top of the module will confirm whether or not you have the reference. To set the reference, click Tools -> References in the VBA editor and tick "Lotus Domino Objects". If not listed in the available references, click Browse and browse to and select domobj.tlb in your Lotus Notes installation folder.
 
Upvote 0
I suspect you haven't set the reference to Lotus Domino Objects, which is needed for constants such as EMBED_ATTACHMENT and FONT_ROMAN. Also, Option Explicit added at the top of the module will confirm whether or not you have the reference. To set the reference, click Tools -> References in the VBA editor and tick "Lotus Domino Objects". If not listed in the available references, click Browse and browse to and select domobj.tlb in your Lotus Notes installation folder.

Thanks a lot John_w, works perfectly! Spent 1 week trying to solve this...:laugh:
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top