excelvbalotus-notes

Attach PDF to Lotus Notes email


I found code online and modified it. It will make an email draft and attach a pdf file.
I see the attachment but when it copies the draft to an actual email the pdf disappears. No error box comes out.

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 AttachME As Object
    Dim EmbedObj 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 FSO As Object
    Dim tempFolder As String, tempCellsJPG As String
    Dim Copy_and_Paste As Boolean
    
    '--------- EDIT USER-DEFINED SETTINGS IN THIS SECTION ---------
        
    'The file to be attached to the email, if it exists
    
    Attachment = "C:\Users\SCH61\W14.pdf"
    
    SendTo = "afranzini@gmail.com"
    CopyTo = ""
    BlindCopyTo = ""
    Subject = "Email subject"
    
    '--------- END OF USER-DEFINED SETTINGS ---------
    
    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 ---------
            
        If Attachment <> "" Then
        Set AttachME = NDocumentTemp.CREATERICHTEXTITEM("Attachment")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
        NDocumentTemp.CREATERICHTEXTITEM ("Attchment")
        End If
            '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."
    
            '--------- 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
        
        '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

I tried attaching the file in the final part of the code (when it uses composedocument) changing the code this way.

    With NUIDocument
        .FIELDSETTEXT "EnterSendTo", SendTo
        .FIELDSETTEXT "EnterCopyTo", CopyTo
        .FIELDSETTEXT "BlindCopyTo", BlindCopyTo
        .FIELDSETTEXT "Subject", Subject
        If Attachment <> "" Then
        Set AttachME = NUIDocument.CREATERICHTEXTITEM("Attachment")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
        NUIDocument.CREATERICHTEXTITEM ("Attchment")
        End If
        '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
        
        '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

I get

Object doesn't support this property or method (Error 438).


Solution

  • You do the same error again as in your previous post: You attach the file to a richtext item called "Attachment" here:

       If Attachment <> "" Then
        Set AttachME = NDocumentTemp.CREATERICHTEXTITEM("Attachment")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
        NDocumentTemp.CREATERICHTEXTITEM ("Attchment")
       End If
    

    But in order to be able to copy it from BODY you need to attach it to BODY. Just replace these lines with:

       If Attachment <> "" Then
         .EMBEDOBJECT(1454, "", Attachment, "Attachment")
       End If
    

    Then the attachment will be IN the richtextitem body and can be copied as well.

    And your second code does mainly not work because NUIDocument is an Object ob Type "NotesUIDocument" while what you need for "CreateRichtextItem" to work is an object of type "NotesDocument". One is the UI/Frontend Class, the other the Backend class...

    It would work syntactically with this code:

    With NUIDocument
      Set AttachME = .Document.GETRICHTEXTITEM("BODY")
      AttachMe.EMBEDOBJECT(1454, "", Attachment, "Attachment")
    

    but unfortunately RichtextItems cannot be manipulated while being shown in frontend. So this will not work here. You could move this all around a bit, but then your copied mail content would be below the attached attachment:

    Dim body as Object
    Set NDocumentMemo = NMailDb.CREATEDOCUMENT
    If Attachment <> "" Then
      Set body = NDocumentMemo.CREATERICHTEXTITEM("Body")
      body.EMBEDOBJECT(1454, "", Attachment, "Attachment")
    End If
    Set NUIDocument = NUIWorkspace.EDITDOCUMENT(True, NDocumentMemo)
    With NUIDocument
      ...
    

    But I don't really get, why you need the temporary document at all...