views:

491

answers:

3

I have a VBA function that initializes a lotus notes session, creates a document and mails it. It also accepts as an optional parameter a NotesRichTextItem which I append to the body of the email.

However, I am getting the error message "All objects must be from the same session". How do I 'import' this NotesRichTextItem into my session?

Edit-Code added

Sub SendLotusMail(SubjTxt As String, _
                BodyTxt As String, _
                EmailTo As String, _
                EmailCC As String, _
                AutoSend As Boolean, _
                Attach As String, _
                ReportTitle As String, _
                Optional AppendToBody As NotesRichTextItem = Null)
On Error GoTo EH
    NtSession.Initialize
    OpenMailDb ReportTitle
    Set NtDoc = Ntdb.CreateDocument

    NtDoc.AppendItemValue "Form", "Memo"
    NtDoc.AppendItemValue "SendTo", EmailTo
    NtDoc.AppendItemValue "CopyTo", EmailCC
    NtDoc.AppendItemValue "Subject", SubjTxt
    Set NtBodyRT = NtDoc.CreateRichTextItem("Body")
    NtDoc.AppendItemValue "Body", NtBodyRT
    If Attach <> "" Then NtBodyRT.EmbedObject EMBED_ATTACHMENT, "", Attach, "Attachment"
    NtBodyRT.AppendText BodyTxt

    'This next line throws the error "All objects must be from the same session"
    NtBodyRT.AppendRTItem AppendToBody

Edit-Solution found
I don't like it very much, but I got around all these issues by passing the RichTextItem object, it's parent NotesDocument, and it's parent's parent NotesSession to this function. So, now I'm calling this procedure with 3 optional objects instead of 1. Hooray.

Edit-New Solution found Well, the previous solution was causing me problems, so until I find (or someone suggests) a workaround, I'll just use some custom email procedures for the reports that require it. It does duplicate some code, but not significantly.

A: 

It would help to see some code here. I'll make a guess at what is happening, though.

In your VBA function, you'll need to create a new NotesRichTextItem object within your email. For instance:

Dim docMail as New NotesDocument(db)
Dim rtBody as New NotesRichTextItem(docMail, "Body")

Call rtBody.AppendRTItem(myRTparameter)

I imagine that should work without an error.

Ken Pespisa
Thanks, the code is added. I have some flexibility here, so if I need to import the RT's parent document or parent session, I can do that.
PowerUser
(Let me rephrase my prior comment). I've added my code to my post per your request. Unfortunately, I don't think your sample code applies here. So, the question is still open.
PowerUser
+1  A: 

The issue may be the fact that the NtSession object is being re-initialized in your sub. If the calling routine sends in a rich text item, I am assuming it must have created and initialized a NotesSession as well. If that's the case, you would want your code to re-use that same session. It looks like NtSession is a global - if that's the case, you could:

  1. Enforce that the calling routing always have initialized that global session;
  2. Optionally pass in a NtSession object as an argument (and your code can check if that object is null before creating and initializing its own session); or
  3. Before calling Initialize, check if NtSession already is initialized - to do that, you may be able to check an attribute and see if the object throws on error (non-tested code):

    function isNotesSessionInitialized (ns)  
        on error goto err  
        dim sUser  
        sUser = ""  
        sUser = ns.commonUserName  
    err:  
        if (sUser = "") then  
            return false  
        else  
            return true  
        end if  
    end function
    
Ed Schembor
Thanks, but I've already gotten around that issue by making 2 separate NotesSession objects, one for this function, and another for the function that calls it. Which, unfortunately, is also the source problem because I now get the error "All objects must be from the same session".
PowerUser
A: 

(I'm writing this to close out my question) I've gotten around this issue by just having separate email procs for the reports that require custom setups. Yes, there is some duplication of code, but it's far better than the behemoth I was about to make.

PowerUser