Search code examples
excelvbaemaillotus-notes

How can I decide whether to save or not save an email via IBM Notes?


UPDATE 28/03/2022: I've found a solution, that works for me fine. The code below is now working. The email is only saved/not saved when it is sent immediatly.

My (old) problem was:

I want to use the following code to send an email via IBM Notes. Everything works fine, but I can't figure out how to NOT save the email in the folder "sent".

I've tried already to put the line

.PostedDate = Now()

at the "objBackendDocument"-object, and tried to clear the value, because I've read in some posts, that this maybe a criteria for IBM Notes to save an email in the folder "sent". But it didn't work.

If I'm using an alternative mailfile it doesn't save my emails at all. If I use my standard mailfile, it saves every email ignoring "blnSaveEMail".

I would like to not save the email, because I want to send automated emails with attachements, which are already on the users pc (saving storage and avoiding copies of copies).

Another idea could be to strip the attachements from the recent sent email, but this is at the moment to difficult for me. Because I'm still trying to understand how the API of IBM Notes works. :)

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'+
'+  EMail_by_Notes
'+
'+  API: Lotus Notes COM/OLE
'+  Parameter "varRecipients": Requires a VARIANT or an array (VARIANT) of verified email-adresses
'+  Parameter "strSubject": Requires a STRING as the title of the email
'+  Paramater "strMessage": Requires as STRING as the content of the email
'+  Parameter "varCopy" optional: VARIANT or an array (VARIANT) of verified email-adresses
'+  Parameter "varBlindCopy" optional: VARIANT or an array (VARIANT) of verified email-adresses
'+  Parameter "varAttachements" optional: VARIANT or an array (VARIANT) of filepath(s)
'+  Parameter "blnSendImmediately" optional: BOOLEAN
'+  Parameter "blnSaveEMail" optional: BOOLEAN
'+  Parameter "strAlternative_Mailfile" optional: STRING, contains the filename of the alternative mailfile
'+
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Public Function EMail_by_Notes( _
        varRecipients As Variant, _
        strSubject As String, _
        strMessage As String, _
        Optional varCopy As Variant = "", _
        Optional varBlindCopy As Variant = "", _
        Optional varAttachements As Variant, _
        Optional blnSendImmediately As Boolean = True, _
        Optional blnSaveEMail As Boolean = False, _
        Optional strAlternative_Mailfile As String = "" _
            ) As Boolean
    
        Dim objNotesSession As Object
        Dim objNotesWorkspace As Object
        Dim objNotesDatabase As Object
        
        Dim objBackendDocument As Object
        Dim objFrontendDocument As Object
        
        Dim objRecipients As Object
        Dim objCopy As Object
        Dim objBlindCopy As Object
        Dim objSubject As Object
        Dim objMessage As Object
        Dim objEmbedded As Object
        Dim objAttachement As Object
        Dim objProfileDoc As Object

        Dim strMailServer As String
        Dim strMailFile As String
        Dim strFilepath As String
        Dim strSignature As String
        
        Dim lngIndex As Long
        
'Starts Notes Session
    
        Set objNotesSession = CreateObject("Notes.NotesSession")
    
'Locate the mailserver
    
        strMailServer = objNotesSession.GetEnvironmentString("MailServer", True)

'Check for an alternative mailfile (in case you have a second account)
    
        If VBA.Len(strAlternative_Mailfile) = 0 Then
    
'Uses the standard account
    
            strMailFile = objNotesSession.GetEnvironmentString("MailFile", True)
            
        Else
    
'Uses an alternative mailfile, if the filename is wrong, it uses the standard account
'Unfortunately there is no error message
    
            strMailFile = "mail/" & strAlternative_Mailfile
            
        End If
    
'Connect to the database
    
        Set objNotesDatabase = objNotesSession.GETDATABASE(strMailServer, strMailFile)
    
'If your constructed path (variable strMailFile) is wrong or the database cannot be accessed
'then this line will make sure to fallback to the mailfile configured in your location document in Notes Client.
    
        If Not objNotesDatabase.IsOpen Then objNotesDatabase.OPENMAIL
        
        If blnSendImmediately = True Then

            Set objProfileDoc = objNotesDatabase.GetProfileDocument("CalendarProfile")

        End If
        
'Create a Notes document in the backend
    
        Set objBackendDocument = objNotesDatabase.CREATEDOCUMENT
            
        With objBackendDocument
    
'Fill in the contents
    
            Set objRecipients = .APPENDITEMVALUE("SendTo", varRecipients)
            Set objCopy = .APPENDITEMVALUE("CopyTo", varCopy)
            Set objBlindCopy = .APPENDITEMVALUE("BlindCopyTo", varBlindCopy)
            Set objSubject = .APPENDITEMVALUE("Subject", strSubject)
                                
    
            If blnSendImmediately = True Then

                Set objMessage = .CreateRichTextItem("body")

'Adds the user's RTF-signature from Lotus Notes
                
                With objMessage
                    .appendText strMessage & VBA.vbCrLf & VBA.vbCrLf
                    .appendrtitem objProfileDoc.GetfirstItem("Signature_Rich")
                End With
                
            End If
    
'Attach the file(s)
            
            If VBA.IsMissing(varAttachements) = False Then
            
                If VBA.IsArray(varAttachements) = True Then
                
                    For lngIndex = LBound(varAttachements) To UBound(varAttachements)
                    
                        strFilepath = varAttachements(lngIndex)
                        
                        If strFilepath <> "" And VBA.Dir(strFilepath) <> "" Then
                        
                            Set objAttachement = .CreateRichTextItem("Attachment" & lngIndex)
                            Set objEmbedded = _
                                objAttachement.EMBEDOBJECT(1454, "", strFilepath, "Attachment" & lngIndex)
                                
                        End If
                        
                    Next
                
                ElseIf VBA.Len(varAttachements) > 0 And VBA.Dir(varAttachements) <> "" Then
                
                    Set objAttachement = .CreateRichTextItem("Attachment1")
                    Set objEmbedded = _
                        objAttachement.EMBEDOBJECT(1454, "", varAttachements, "Attachment1")
                
                End If
                
            End If
            
'Save or do not save the email in the folder "sent" before sending the email immediately

            If blnSendImmediately = True Then .SAVEMESSAGEONSEND = blnSaveEMail
            
        End With

'Check, whether the email shall be sent immediately or not
    
        If blnSendImmediately = False Then
        
'Load Notes Workspace
        
            Set objNotesWorkspace = CreateObject("Notes.NotesUIWorkspace")
           
'Get the backend document in the foreground
'Also in case, the email shall be edited before sending it
        
            Set objFrontendDocument = objNotesWorkspace.EDITDOCUMENT(True, objBackendDocument)
               
            With objFrontendDocument
            
'Fill in the emails message
'Important if you use a signature in IBM Notes
        
                .GoToField "Body"
                .InsertText strMessage
                
            End With
        
        Else
        
            With objBackendDocument
                .Send False
            End With
                
        End If

        EMail_by_Notes = True
        
    End Function

Solution

  • This is what you are doing :

    • building doc1
    • working on doc1.uidocument
    • sending doc1.uidocument.document (=doc2)

    SaveMessageOnSend applies on doc1, not on uidoc nor doc2.

    Moreover, it does NOT make sense to open in the ui, and send in the back.

    You should do all in the background (look up for user signature in its profile). If you want to interact with the user, open in the foreground, and let him work and choose to save or not the mail (this is a global Notes client preference, that may be changed with field MailSaveOptions)