+ Reply to Thread
Results 1 to 12 of 12

Review Before Sending Excel Attachment in Lotus Notes

Hybrid View

  1. #1
    Registered User
    Join Date
    05-17-2012
    Location
    New York, NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Review Before Sending Excel Attachment in Lotus Notes

    Hello,

    I am trying to incorporate a piece of code that will attach an Excel file to a Lotus Notes email. I have found a code that works beautifully except for one thing: I do NOT want the email to send out automatically. I'd like the code to activate Lotus Notes and then let the user write a full message, change recipients, decide if they'd like to send it or not, etc. Surprisingly, it is not as easy as deleting the last few lines which sends the email (that seems to break the code altogether).

    Could someone please help me troubleshoot this issue?

    Thanks,

    Evan


    Sub SendWithLotus()
       Dim noSession As Object, noDatabase As Object, noDocument As Object
       Dim obAttachment As Object, EmbedObject As Object
       Dim stSubject As Variant, stAttachment As String
       Dim vaRecipient As Variant, vaMsg As Variant
       
       Const EMBED_ATTACHMENT As Long = 1454
       Const stTitle As String = "Active workbook status"
       Const stMsg As String = "The active workbook must first be saved " & vbCrLf _
             & "before it can be sent as an attachment."
       'Check if the active workbook is saved or not
       'If the active workbook has not been saved at all.
       If Len(ActiveWorkbook.Path) = 0 Then
          MsgBox stMsg, vbInformation, stTitle
          Exit Sub
       End If
       'If the changes in the active workbook have been saved or not.
       If ActiveWorkbook.Saved = False Then
          If MsgBox("Do you want to save the changes before sending?", _
                vbYesNo + vbInformation, stTitle) = vbYes Then _
                ActiveWorkbook.Save
       End If
       'Get the name of the recipient from the user.
       Do
          vaRecipient = Application.InputBox( _
                Prompt:="Please add name of the recipient such as:" & vbCrLf _
                & "will@yahoo.co.uk or just the name if internal mail within Unity.", _
                Title:="Recipient", Type:=2)
       Loop While vaRecipient = ""
       'If the user has canceled the operation.
       If vaRecipient = False Then Exit Sub
       'Get the message from the user.
       Do
          vaMsg = Application.InputBox( _
                Prompt:="Please enter the message such as:" & vbCrLf _
                & "Enclosed please find the weekly report.", _
                Title:="Message", Type:=2)
       Loop While vaMsg = ""
    
       'If the user has canceled the operation.
       If vaMsg = False Then Exit Sub
       'Add the subject to the outgoing e-mail
       'which also can be retrieved from the users
       'in a similar way as above.
       Do
            stSubject = Application.InputBox( _
                Prompt:="Please add a subject such as:" & vbCrLf _
                & "Weekly Report.", _
                Title:="Subject", Type:=2)
       Loop While stSubject = ""
       'Retrieve the path and filename of the active workbook.
       stAttachment = ActiveWorkbook.FullName
       'Instantiate the Lotus Notes COM's Objects.
       Set noSession = CreateObject("Notes.NotesSession")
       Set noDatabase = noSession.GETDATABASE("", "")
       'If Lotus Notes is not open then open the mail-part of it.
       If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
       'Create the e-mail and the attachment.
       Set noDocument = noDatabase.CreateDocument
       Set obAttachment = noDocument.CreateRichTextItem("stAttachment")
       Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
       'Add values to the created e-mail main properties.
       With noDocument
          .Form = "Memo"
          .SendTo = vaRecipient
          .Subject = stSubject
          .Body = vaMsg
          .SaveMessageOnSend = True
       End With
       'Send the e-mail.
       With noDocument
          .PostedDate = Now()
          .Send 0, vaRecipient
       End With
    
       'Release objects from the memory.
       Set EmbedObject = Nothing
       Set obAttachment = Nothing
       Set noDocument = Nothing
       Set noDatabase = Nothing
       Set noSession = Nothing
    
       AppActivate "Lotus Notes"
       
       MsgBox "The e-mail has successfully been created and distributed.", vbInformation
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Are you still looking for a solution? Instead of NotesSession and CreateDocument, I think you'll need to use NotesUIWorkspace and ComposeDocument. As a start:
        Dim NUIWorkspace As Object
        Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")            
        NUIWorkspace.ComposeDocument , , "Memo"
    Post back if you need further help.
    Post responsibly. Search for excelforum.com

  3. #3
    Registered User
    Join Date
    05-17-2012
    Location
    New York, NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Very helpful, Chippy. Thanks a bunch. Any sense on how to attach the active workbook to the email once the "memo" has been created?

    Thanks again,

    Evan

  4. #4
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Create an object using CreateRichTextItem then call its EmbedObject method, specifying ActiveWorkbook.FullName along with the other required arguments.

  5. #5
    Registered User
    Join Date
    05-17-2012
    Location
    New York, NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Sorry if this is a bit dense, but I'm not familiar with creating a rich text item or calling its EmbedObject method. Could you please help me with the code for attaching the workbook? I have named the file "DestWB" earlier in my code.

    This works to create a new document, but I'm getting an error message on the line that begins "Set Body =".... any advice?
    Dim NUIWorkspace, Body As Object
        Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
        Set MailDoc = NUIWorkspace.ComposeDocument(, , "Memo")
        Set Body = MailDoc.CREATERICHTEXTITEM("Body")
        Call Body.ADDNEWLINE(2)
        Call Body.EmbedObject(1454, "", DestWB, "Attachment")

  6. #6
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Quote Originally Posted by bambaataa View Post
    I'm getting an error message on the line that begins "Set Body =".... any advice?
    What is the error? Always quote the exact and full error message.

    Try this instead:
    Option Explicit
    
    Const EMBED_ATTACHMENT = 1454
    
    Sub Create_UI_Email()
    
        Dim NUIWorkspace As Object, NUIDocument As Object, NRTattachment As Object
        
        Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
        NUIWorkspace.ComposeDocument , , "Memo"
        Set NUIDocument = NUIWorkspace.CurrentDocument
            
        With NUIDocument
            .FieldSetText "EnterSendTo", "email1@domain.com"
            .FieldSetText "EnterCopyTo", "email2@domain.com"
            .FieldSetText "EnterBlindCopyTo", ""
            .FieldSetText "Subject", Now & " test"
        
            .GotoField "Body"
            .InsertText "This is the email body text." & vbLf & vbLf
            
            Set NRTattachment = .document.CreateRichTextItem("Attachment")
            NRTattachment.EmbedObject EMBED_ATTACHMENT, "", ActiveWorkbook.FullName
        End With
    
    End Sub

  7. #7
    Registered User
    Join Date
    05-17-2012
    Location
    New York, NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Thanks for this. I'm having two issues, though.

    1. When I run the code above, the email is created and the fields are filled out (with email1@domain.com, etc.); however, the active file is not attached. Any suggestions as to what may be going wrong?

    2. Occasionally (maybe 20% of the time), I get this error message: "Run-time Error '91': Object variable or With block variable not set." The error occurs on the line ".FieldSetText "EnterSendTo", "email1@domain.com" -- any idea what might be causing this and why it only happens some of the time?

    Thanks,

    Evan

  8. #8
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Quote Originally Posted by bambaataa View Post
    1. When I run the code above, the email is created and the fields are filled out (with email1@domain.com, etc.); however, the active file is not attached. Any suggestions as to what may be going wrong?
    In my tests (Notes 6.5) the file is attached, but isn't visible to the user until they send or close and reopen the email. However, I can understand why you would want the attachment to be visible in the open document as confirmation that the file has been attached, so I've spent some time investigating and writing the code to achieve this. Part of the solution involves the VBA SendKeys command, which isn't reliable so the solution might not always work for you, however it always worked in my tests.

    2. Occasionally (maybe 20% of the time), I get this error message: "Run-time Error '91': Object variable or With block variable not set." The error occurs on the line ".FieldSetText "EnterSendTo", "email1@domain.com" -- any idea what might be causing this and why it only happens some of the time?
    That error must mean that the NUIDocument object is undefined (Nothing). It could be caused by VBA attempting to access the new document before the Lotus Notes UI has created it. I've added a check which loops until NUIDocument is not Nothing, waiting for up to 10 seconds.

    Here is the complete code:
    Option Explicit
    
    Const EMBED_ATTACHMENT = 1454
    
    Sub Create_UI_Email()
    
        Dim NUIWorkspace As Object, NUIDocument As Object, NRTattachment As Object
        Dim NUIDatabase As Object
        Dim timeout As Date
        Dim docID As String
        
        Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
        Set NUIDatabase = NUIWorkspace.CurrentDatabase
        
        'Create a UI document
        
        NUIWorkspace.ComposeDocument , , "Memo"
        
        'Get the UI document from the workspace, waiting for up to 10 seconds
        
        timeout = Now + TimeValue("00:00:10")
        Do
            Set NUIDocument = NUIWorkspace.CurrentDocument
            If NUIDocument Is Nothing Then Application.Wait Now + TimeValue("00:00:01")
        Loop Until Not NUIDocument Is Nothing Or Now > timeout
        
        If Not NUIDocument Is Nothing Then
        
            With NUIDocument
            
                'Set email fields
                
                .FieldSetText "EnterSendTo", "email1@domain.com, email2@domain.com"
                .FieldSetText "EnterCopyTo", "email3@domain.com"
                .FieldSetText "EnterBlindCopyTo", ""
                .FieldSetText "Subject", Now & " test"
            
                'Insert email body text
                
                .GoToField "Body"
                .InsertText "This is the email body text." & vbLf & vbLf
                
                'Insert attachment - the active workbook
                
                Set NRTattachment = .Document.CreateRichTextItem("Attachment")
                NRTattachment.EmbedObject EMBED_ATTACHMENT, "", ActiveWorkbook.FullName
                        
                'Attachment doesn't appear in the UI document, although it is present in the sent or saved email.  We must close
                'the document and open it again to make the attachment visible to the user
                
                'Get the universal ID of this UI document so that we can open the same document later
                
                docID = .Document.UniversalID
                
                'Save and close this UI document
                
                .Save
                .Close
                
            End With
        
            'Close causes a Lotus Notes window to display
            '  Caption: Send Mail
            '  Do you want to send, save or discard your changes? Choose Cancel to continue editing
            '  5 Buttons: Send & Save; Send Only; Save Only; Discard; Cancel
            
            'Press v key for Save Only
            
            Application.Wait Now + TimeValue("00:00:02")
            AppActivate "Send Mail"
            SendKeys "v"
        
            'Open the newly created document again, thus making the attachment visible to the user
            
            Set NUIDocument = NUIDatabase.Database.GetDocumentByUNID(docID)
            NUIWorkspace.EditDocument True, NUIDocument
    
        Else
        
            MsgBox "Timeout occurred waiting for document to be created in Lotus Notes workspace"
            
        End If
        
    End Sub
    Last edited by Chippy; 06-25-2012 at 03:32 PM.

  9. #9
    Registered User
    Join Date
    03-25-2013
    Location
    Khmer
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Review Before Sending Excel Attachment in Lotus Notes

    dear sir,
    why it error on
    Set NRTattachment = .Document.CreateRichTextItem("Attachment")
    NRTattachment.EmbedObject EMBED_ATTACHMENT, "", ActiveWorkbook.FullName
    that cause my mail didn't have any attachment.

  10. #10
    Registered User
    Join Date
    05-17-2012
    Location
    New York, NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Great!

    We are 99% there. The email is created, the attachment is appended, and the email is saved as a draft. However, when the line "NUIWorkspace.EditDocument True, NUIDocument" begins to run, I get the following error message:

    Run-time error '-2147417848(80010108)':
    Automation error
    The object invoked has disconnected from it's clients

    Any ideas what is causing this? Everything is working correctly and the email is sitting in the "Drafts" folder--I just need it to open up again.

    Thanks

    Evan

  11. #11
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Again, this could be a timing issue - VBA being too fast for the Lotus Notes UI. Try adding a wait of 1 or more seconds before the GetDocument and/or EditDocument, like this:

            'Open the newly created document again, thus making the attachment visible to the user
            
            Application.Wait Now + TimeValue("00:00:01")
            Set NUIDocument = NUIDatabase.Database.GetDocumentByUNID(docID)
            Set NUIDocument = NUIWorkspace.EditDocument(True, NUIDocument)
            'NUIDocument.GoToField "Subject"
            NUIDocument.GoToField "Body"
    In the above code, EditDocument is being called as a function so that GoToField can be invoked to place the cursor in one of the email fields.

    I can't think of anything else which could cause the error (never happened on my system). If the wait doesn't fix it, you'll have to do some basic debugging - set breakpoints, step through the code, examine variables (e.g. verify that NUIDocument is not Nothing).

  12. #12
    Registered User
    Join Date
    05-17-2012
    Location
    New York, NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Review Before Sending Excel Attachment in Lotus Notes

    Very, very helpful. Thanks for all of the assistance!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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