Link to home
Start Free TrialLog in
Avatar of Conor Newman
Conor NewmanFlag for Ireland

asked on

Excel VBA send Mail from OPEN Lotus Notes - Without input

Ok, I have a workbook, which tracks reviews of documentation, as each review is completed it emails the next reviewer to let them know. It uses de Bruins CDO code to do this.

I now have a request to change this to use an open lotus notes session on the users pc to send the mail. As he can't get his IT to allow access to their servers SMTP ports. I think it's daft, but customer is always right..

So I have the following code, but, I am testing this using lotus notes I d/l'd from the net and set up standalone on my pc with a gmail account connected. It doesn't work as it get the database name wrong, it presumes the database name can be calculated from my username, when as it's a gmail account it names the Db imapxxxx.nsf.

Can I get the following code to use the open Lotus Notes application to send a mail, without having the user having to input anything, or hardcoding database names etc in?


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 AttachME As Object 'The attachment richtextfile object
    Dim Session As Object 'The notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)
    'Start a session to notes
    Set Session = CreateObject("Notes.NotesSession")
    'Next line only works with 5.x and above. Replace password with your password
    Session.Initialize ("password")
    'Get the sessions username and then calculate the mail file name
    'You may or may not need this as for MailDBname with some systems you
    'can pass an empty string or using above password you can use other mailboxes.
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"

    'Open the mail database in notes
    Set Maildb = Session.GETDATABASE("", MailDbName)
     If Maildb.IsOpen = True Then
          'Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    'Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = "Memo"
    MailDoc.sendto = Distrib
    MailDoc.Subject = Subj
    MailDoc.Body = msg
    MailDoc.SAVEMESSAGEONSEND = True
    'Set up the embedded object and attachment and attach it
' Commented out, there will never be attachments, text only.
   ' If Attachment <> "" Then
   '     Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
   '     Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
   '     MailDoc.CREATERICHTEXTITEM ("Attachment")
    'End If
    'Send the document
    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    MailDoc.SEND 0, Distrib
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set AttachME = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing

Open in new window


While I have pretty decent experience using EXCEL VBA and CDO methods etc, I have never used Lotus Notes in my life much less accessed it from Excel. It took me 3 hours just to get the damn thing to send and receive from gmail... SO please bear with me If I'm missing something obvious..

Thanks for your help.
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

You are right, there's nothing simpler than allowing internal SMTP mails to be relayed by the Domino server. I'd ask them again, seems absurd to go any other way.

If you don't succeed, I wouldn't use the code above, for the mail database is opened based on some assumptions. There is a better way now: use OpenMailDatabase.

Replace lines 12-24 with this code:

Dim Directory As Object
Set Directory = Session.GetDbDirectory(Session.ServerName)
Set Maildb = Directory.OpenMailDatabase()
Avatar of Conor Newman

ASKER

Definitely looks simpler, Have tried it, it's crashing out at

Set Directory = Session.GetDbDirectory(Session.ServerName)

Error: Object doesn't support this Property or method.

Missing Ref lib maybe?
SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

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
Version 8.5.3

Ok, tried Set Directory = Session.GetDbDirectory("")
and it works, but then  faults at: Set Maildb = Directory.OpenMailDatabase()
with the same error.
It might have to be without the parentheses:

     Set Maildb = Directory.OpenMailDatabase

Can you debug the code ? And see if Directory is indeed a non-empty object?

It is better to use OLB libraries, by setting the 'Lotus Notes Automation Classes', and use the classes NotesSession, NotesDbDirectory, NotesDocument, etc.
Tried without Parentheses, been looking up the syntax' here Here

Same error, and Directory is no longer = Nothing, so it seems to have taken the value at the line above..

Using Lotus Notes Automation Classes and Lotus Domino Objects libs..
Can you open your notes.ini file (in the Notes program directory probably) and check whether the value assigned to MailFile is correct and if there is a MailServer variable.

Check also that your current Location is correctly set up. Click the bottom-right corner of the Notes screen (Office?), Edit Locations, Edit..., Mail tab, Mail file location should be Local.
Taken from Notes.ini in Program Files/IBM/Lotus/Notes

Location=Online,9E6,CN=Conor Newman/O=Conor Newman
.....
MailFile=imap5175.nsf
......
MailServer=imap.gmail.com



Mail file location is local and it is set to the same file as in the ini file (imap.nsf)
is there a way in debug mode I can display the value of "Directory" object, to see at least is it finding the correct database? Or get it to print out a value for itself?
Hmm, I wonder... imap.nsf is probably not a mail database, but it could be...  what's the template for this database (see the Database Properties dialog box, the Design tab), and when you open it, does it look like an ordinary Notes mail database to you?

MailServer probably shouldn't be imap.gmail.com in the Location document, Notes expects a Domino server at that address. If Mail is Local, leave the Mail server field empty. Not that it would matter much, I suppose...

To avoid further ado, please ask your customer for a TeamViewer or other remote session, on a spare PC, and a test user id, so you can properly test your application. You're wasting your time (big time) and mine as well ;-) Notes is a complex beast, not to be mastered in 8 hours, I'm afraid.
Crap... Right, I'm gonna download Domino as well and set it up as a trial locally..
hopefully I'll have better luck with that instead of gmail..

Don't actually need this to send an email, I just need to know the code is working so when I take it the clients site, it will run for him or at least run enough that I know I can tweek it to run.
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
You could indeed download Domino, but it'll take you even longer to set up that server... By all means, don't let me hold you back! :-)
You cracked it!!  Set Session = CreateObject("Lotus.NotesSession") wored!

Set Session = CreateObject("Lotus.NotesSession")
    Session.Initialize
    Dim Directory As Object
    Set Directory = Session.GetDbDirectory("")
    Set Maildb = Directory.OpenMailDatabase
    'Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = "Memo"
    MailDoc.sendto = Distrib
    MailDoc.Subject = Subj
    MailDoc.Body = msg
    MailDoc.SAVEMESSAGEONSEND = True

Open in new window


Faulting now at : MailDoc.Form = "Memo" WIth Object doesn't support Property or method, is there somewhere I can check the name of the default mail form?
ASKER CERTIFIED 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
Ok... aaaaannnnnd.. it works!!

You sir are an absolute legend!!
Final code for anyone else who needs this in the future..

    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 AttachME As Object 'The attachment richtextfile object
    Dim Session As Object 'The notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)
    'Start a session to notes
    Set Session = CreateObject("Lotus.NotesSession")
    'Next line only works with 5.x and above. Replace password with your password
    Session.Initialize
    Dim Directory As Object
    Set Directory = Session.GetDbDirectory("")
    Set Maildb = Directory.OpenMailDatabase
    'Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    Call MailDoc.ReplaceItemValue("Form", "Memo")
    Call MailDoc.ReplaceItemValue("SendTo", Distrib)
    Call MailDoc.ReplaceItemValue("Subject", Subj)
    Call MailDoc.ReplaceItemValue("Body", msg)
    Call MailDoc.ReplaceItemValue("PostedDate", Now())

' Set distribution
    Distrib = "me@mydomain.com"    

'Send the document
    MailDoc.SEND 0, Distrib
    
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set AttachME = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing

Open in new window

Bloody genius.

Thank you for your patience!! Much appreciated!
I can only offer my sincerest apologies... for not having spotted the obvious right away... and hence wasting our time...
Notes.NotesSession is soooo OLD (and OLE)...
But I'm happy that you're happy ! :-)
Ecstatic! ;)  Hopefully it'll work just as well on the clients set up and all will be well!

Thanks again!!
One little issue that I hadn't noticed yesterday., I've detailed it in a different question,

https://www.experts-exchange.com/questions/27944423/Lotus-Notes-mail-sent-from-excel-vba-won't-save-sent-mail.html