Link to home
Start Free TrialLog in
Avatar of CYBERWORLD
CYBERWORLD

asked on

using visual basic to launch Lotus Notes Email

I am writing a program in visual basic 6. I want the vb program to launch Lotus Notes Send mail screen and pass the To, CC, Subject, Bodytext and most importantly, the ATTACHMENT (which located at the harddisk) to the Lotus Notes. So that all the user needs to do is to click the SEND button. I have the following code for your reference, it works EXCEPT that I failed to attach the attachment to the email. Any idea to attach the attachment?

Please supply your sample code if you have written the same workable code before.

Thanks a million. You guys are great!


        Dim MySubject As String
        Dim MyBodyText As String
        Dim MyReceiver As String
        Dim MyAttachment as String
        MyReceiver = rs8.Fields("Email")
        MySubject = "Report arrived"
        MyAttachment = "C:\report.pdf"           <<<<<<<<<<<<<<<<<<
        MyBodyText = "Dear Sir, Report arrived. Thanks and Regards"
        Call SendNotesMail(MySubject, MyAttachment, MyBodyText, MyReceiver)
----------------------------------------------------------------------------------
Public Sub SendNotesMail(Subject As String, Attachment As String, BodyText As String, SendTo As String, Optional CC As String = "", Optional BCC As String = "", Optional SaveIt As Boolean = False)
    'Set up the objects required for Automation into lotus notes
    Dim Maildb As Object 'The mail database
    Dim UserName As String 'The current users notes name
    Dim MailDbName As String 'THe current users notes mail database name
    Dim MailDoc As Object 'The mail document itself
    Dim intAttach As Integer
    Dim Session As Object 'The notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)
    Dim Server As String
   
    On Error GoTo LotusNotesFail
   
    'Start a session to notes
    Set Session = CreateObject("Notes.NotesSession")
    Server = Session.GetEnvironmentString("MailServer", True)
    MailDbName = Session.GetEnvironmentString("MailFile", True)
    UserName = Session.UserName
   
    'Open the mail database in notes
    Set Maildb = Session.GetDatabase(Server, MailDbName)

    'Set up the new mail document
    Set MailDoc = Maildb.CreateDocument

    With MailDoc
        .Form = "Memo"
        .SendTo = SendTo
        .EnterCopyTo = CC
        .EnterBlindCopyTo = BCC
        .Subject = Subject
        .SaveMessageOnSend = SaveIt

        'Set up the embedded object and attachment and attach it
        Dim aryAttachment() As String

        aryAttachment = Split(Attachment, "|")               <<<<<<<<<<< start look at here
        Dim ric As Object
        Set ric = .CreateRichTextItem("Body")
        ric.AppendText BodyText & Chr$(13)
        For intAttach = LBound(aryAttachment) To UBound(aryAttachment)
          Set EmbedObj = ric.EmbedObject(1454, "", aryAttachment(intAttach), "Attach")
        Next intAttach
        .Save False, False

        'Send the document
        '.PostedDate = Now() 'Gets the mail to appear in the sent items folder
        '.Send False
    End With
   
    DoEvents
    Dim ws As Object
    Set ws = CreateObject("notes.notesuiworkspace")
    DoEvents
    ws.OpenDatabase Server, MailDbName
    ws.EDITDOCUMENT True, MailDoc
   
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set ric = Nothing
    Set ws = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
   
    Exit Sub
 
LotusNotesFail:
    MsgBox ("Cannot open new memo in Lotus Notes. Please make sure you have Lotus Notes running in your computer.")

End Sub


Avatar of HemanthaKumar
HemanthaKumar

Use Lotus.NotesSession instead of notes.NotesSession OLE, This has UI enabled ole classes.. so that you can launch the memo in lotus notes and attach files and more... Even Domino.NotesSession works much better as notes.notessession is somewhat downlevel...


~Hemanth
Avatar of CYBERWORLD

ASKER

sorry, I can't get your meaning.

Do you mean this portion of code? If yes, How to change it?

    'Start a session to notes
    Set Session = CreateObject("Notes.NotesSession")
    Server = Session.GetEnvironmentString("MailServer", True)
    MailDbName = Session.GetEnvironmentString("MailFile", True)
    UserName = Session.UserName
Notes has two COM-level; interfaces -- a pair of OLE Automation rot classes (Notes.NotesSession and Notes.NotesUiWorkspace) and a standard COM class, (either Lotus.NotesSession or Domino.NotesSession, depending how it is registered).

Not sure what Hemantha was getting out by trying to get you to switch, and he may have reversed them.

Anyway, in your case, it does not matter, because you NEED to use Notes.NotesUiWorkspace (there is no other UIWorkspace available), so you SHOULD be consistent throughout, and use Notes.NotesSession with Notes.NotesUiWorkspace, not Lotus.NotesSession or Domino.NotesSession.

Since you save the back-end document before displaying it in the UI, why don't you have the code quit before displaying in the UI, then you can look over the saved document, see if the attachments are really there (which would indicate a problem with your front end transition), or if they are missing (indicates a problem with you back end attach/save).  That will help narrow down where to focus for the fix.

How do you check?  The saved doc is essentially considered a "draft" document.  It should appear in your drafts view, which is sorted by date, so your item shoudl be the first or last one depending on how your sorting is set.  WIthout openining up the message, open its properties (Alt-Enter), and go to the second tab, which is a field list.  Attachments cause a special field called $File to be created (there can be multiple $File fields, one for each attachment).  Go all the way to the top of the field list, and see if there are any $File fields.
Um... as I am really new to Lotus Notes Program, i am not really understand your meaning.. sorry

After I run the program, I find that there is an attachment icon inside the message of lotus notes. But if I double-click it, it cannot be view or save. It seems that the attachment is not really attached in the notes message.

Thanks very much
My assumption was that the ole is not registered properly and lotus. or domino. would give much stability.

Anyway sisnce you say the attachment is not there.. probably the attachment was stored in network drive with spaces in the file/dir name.. This issue was pretty commonly spread while accessing is using COM... As a test make a simple directory with one word and no spaces (eg: c:\temp\test.doc) and try if it solves then you have hit the limitation or else you have to change the way the doc is saved..try .Save true, true  ... force saving !
Not sure what you didn't understand.  Within Notes (if you were using LotusScript instead of VB), there are two sets of classes:

Front end -- manipulate the UI -- base object is NotesUiWorkspace
Back end -- access to data structures -- base object is NotesSession

There is some overlap, since teh front-end form has fields that map into the back end fields (called items, or the NotesItem object), etc.

In an early implementation of exposing the objects, Lotus chose to expose them as OLE automation, which means the client controller is based on an interface into the foreground Lotus Notes program.  These were exposed as Notes.NotesUiWorkspace and Notes.NotesSession.  Later, Lotus made a set of COM classes for NotesSession that do NOT require the Notes client to run (though it DOES have to be installed and configured).  That's Lotus.NotesSession.  But there's no Lotus.NotesUiWorkspace, because by definition, NotesUiWorkspace deals with the client.

Since you wish to present the client UI when your code is through, you need Notes.NotesUiWorkspace, and since you also need NotesSession, you may as well use Notes.NotesSession to match Notes.NotesUiWorkspace, instead of Lotus.NotesUiWorkspace.

Now, there are several places your code could go wrong.  The basic structure of your code is to use the back-end classes to create a pre-populatd Notes document (a message is called a document), save it, and then request the front-end classes to open the same document in the standard UI.  Where could teh code go wrong?

1) You create the back-end document incorrectly
2) You display the front end document incorrectly
3) There could be a bug passing the back-end document to the front end classes
4) There could be something wrong with the classes themselves, as Hemantha suggests

So, I'm merely asking that you eliminate #1 as a possibility.  If you did #1 correctly, there would be a document in the Notes mail file with all the fields containing the correct values.  Because of the way the Notes mail file is structured, that document would be in the Drafts view (at the top or bottom, as it is the newest Draft).  You can see the fields by pressing Alt-Enter to open document properties, and looking at the second tab.  The attachment is associated with the special field named $File.  So, please check that out.  if $File is there, then you've done #1 correctly.  If it isn't, then either you've done it incorrectly, or your Notes setup is buggy.
Thank all experts.

In the draft, it can see the attachment attached to the message.

However, when I double click the attachment. The Lotus Notes hangs and the red box come out.

So, does it mean the back-end document incorrectly?
CYBERWORLD

You really should change the classes as HemanthaKumar suggested. You're probably doing something wrong with your classes; the Red Box appears only if something REALLY goes wrong. It's pretty difficult to find out if you use the old classes. In your references, you should use the LOTUS classes and not the NOTES classes. Can't tell you precisely 'cause I don't have the stuff installed on this machine.

I'm not even sure whether you really can use the FrontEnd classes in a VB program - I never managed to do this (why should one, anyway? Lotus Script is better suited to program the UI than VB).

Hope this helps

Jan
ic... the reason why i use VB is because the lotus notes send mail function is only a very small portion of my program
Right, but then you don't need the UI Classes, do you? Sending mail can be accomplished with the Back End classes.
As I am too new to Lotus Notes, could you pls check the code whether I should      
change the code from

Set Session = CreateObject("Notes.NotesSession")

to

Set Session = CreateObject("Lotus.NotesSession")

But after changing, I found that it has error (I think the error handler receives an error, so make it exit)

Any other changes should be made to the code?
You shouldn't need to use CreateObject. If you have referred to the classes correctly, it should be something along the lines

Dim Session as NotesSession
Set Session = New NotesSession(...)

This way, you have full type checking and reasonable chances that the compiler will detect if you do something wrong. Again, I don't have the correct installation here. Check the previous posts of gwaletee, most of the info is in there.
What is the error ?

BTW, WHat is your notes install ver?
ASKER CERTIFIED SOLUTION
Avatar of qwaletee
qwaletee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
gwaletee - I stand corrected.

I just wonder whether it makes sense to use the Notes Interface to send an e-mail from a VB application; my gut feeling says that the advantage is low compared to the price (stability, memory, ...). At least in the applications I did, I always found a way doing things WITHOUT the Notes UI - one of the reasons being that many users didn't like Notes because of the clumsy user interface. (yes, I _do_ use Notes).

Jan
uhm clumsy.. a new one !

If they don't want to user then why the heck do you bother to open memo in notes ?? You can as well use smtp to do that.. don't ask how ..
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RanjeetRain,
> Hey buddy!

> Check out this piece of code from here: http://www.angelfire.com/realm/vb-shared/send_mail_with_notes.zip 
> from http://www.angelfire.com/realm/vb-shared/notesendmail.htm

> Good luck :)

You do know who put out that code, right?
I do know him. Why ask :) Did he "rip" it from somewhere? LOL. I remember someone putting up a big fight with me here coz i didn't give him the credit for the code he wrote :))

How is life here?!?!? Hope to be back here soon.