PDA

View Full Version : Solved: Mail problem



machunt
02-25-2012, 11:15 AM
Hi,
Today is my first day in this forum, and I am stuck with some severe problems.

I am presently using a vba code to send mail using Lotus notes.
The code is working properly, but I need some modifications within the code.

I will number the points it for easy understanding.

1.Presently the Mail Database address needs to be written in the VBA, I want it to take the address from (B4)-sheet Main

2.The Recipient of the mail needs to be written in the VBA, I want it to take the address from (B8)-sheet Main

3.I want a ccRecipient of the mail to be send with address present from (B9)-sheet Main

4.I want the subject to be taken the address from (B10)-sheet Main

5.The body needs to contain the data from the sheet (Main) reference (B12:F12)

6.Once the mail is sent I want the data from sheet (Main) reference (B12:F12) be copied to the sheet (Body Text), below the existing records and the status on the sheet (Body Text) to be updated to "Mail sent".

That's all.
Thanks and regards,
Machunt

Kenneth Hobs
02-25-2012, 06:15 PM
Welcome to the forum!

Run the test from that first sheet.

Option Explicit

Sub Test_Send_Email_via_Lotus_Notes()
Send_Email_via_Lotus_Notes
CopyB12F12
End Sub

Sub Send_Email_via_Lotus_Notes()
Dim Maildb As Object
Dim MailDoc As Object
Dim Body As Object
Dim Session As Object
'Start a session of Lotus Notes
Set Session = CreateObject("Lotus.NotesSession")
'This line prompts for password of current ID noted in Notes.INI
Call Session.Initialize
'or use below to provide password of the current ID (to avoid Password prompt)
'Call Session.Initialize("<password>")
'Open the Mail Database of your Lotus Notes
'Set Maildb = Session.GETDATABASE("", "D:\Data\Mails\machunt.nsf") '//Refer to B4
Set Maildb = Session.GETDATABASE("", "D:\Data\Mails\" & Range("B4").Value2 & ".nsf") '//Refer to B4
If Not Maildb.IsOpen = True Then Call Maildb.Open
'Create the Mail Document
Set MailDoc = Maildb.CREATEDOCUMENT
Call MailDoc.REPLACEITEMVALUE("Form", "Memo")
'Set the Recipient of the mail
'Call MailDoc.REPLACEITEMVALUE("SendTo", "machunt2005@gmail.com") '//Refer to B8
Call MailDoc.REPLACEITEMVALUE("SendTo", Range(B8.Value2)) '//Refer to B8
'Set subject of the mail
'Call MailDoc.REPLACEITEMVALUE("Subject", "123 Test") '//Refer to B10
Call MailDoc.REPLACEITEMVALUE("Subject", Range("B10").Value2) '//Refer to B10
'Create and set the Body content of the mail
Set Body = MailDoc.CREATERICHTEXTITEM("Body") '// This body needs to contain the data which are meant to retest.
'Call Body.APPENDTEXT("Hello World")
Call Body.APPENDTEXT(Join(WorksheetFunction.Transpose(WorksheetFunction.Transpos e(Range("B12:F12"))), vbTab))

'Send the document
'Gets the mail to appear in the Sent items folder
Call MailDoc.REPLACEITEMVALUE("PostedDate", Now())
Call MailDoc.SEND(False)
'Clean Up the Object variables - Recover memory
Set Maildb = Nothing
Set MailDoc = Nothing
Set Body = Nothing
Set Session = Nothing
End Sub

Sub CopyB12F12()
Dim lr As Long
lr = Worksheets("Pass 1 Results").Range("A" & Rows.Count).End(xlUp).Row + 1
Range("B12:F12").Copy
Worksheets("Pass 1 Results").Range("A" & lr).PasteSpecial xlValues
Worksheets("Pass 1 Results").Range("F" & lr).Value2 = "Mail sent"
Application.CutCopyMode = False
End Sub


If you do this alot, you might want to make a more custom Sub along the lines of:

ub test()
Dim lErr As ErrObject
SendNotesMail _
"PMP Handbook5", _
"c:\t.pdf", _
"khobson@odot.org,khobson@aaahawk.com", _
"Click file: " & vbCrLf & _
"file://u:\Material\pmp\PMP%20Handbook.pdf" & vbCrLf & _
"or, open the attachement.", , lErr
If lErr.Number <> 0 Then MsgBox lErr.Number & vbCrLf & lErr.Description
End Sub

'Escape characters, %20=space, http://everything2.com/node/1350052
'Similar to: Brian Walters, http://www.ozgrid.com/forum/showthread.php?t=67089
Public Sub SendNotesMail(Subject As String, Attachment As String, _
ByVal Recipient As String, _
BodyText As String, _
Optional SaveIt As Boolean = True, _
Optional ByRef lErr As ErrObject)
'lErr is used when using the Sub in a batch process,
'to handle instances where an error appears

'Example of use:
'SendNotesMail "The Subject", "C:\My Documents\TestFile.txt", _
"john@doe.com, jane@doe.com", _
"This is the body text, can be longer", True, lErr

'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 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)
Dim ArRecipients() As String 'Array of recipients
Dim i As Long 'Counter
Dim oBody As Object 'Body of text in for rich text format

'Early Bind - Tools > Reference > Lotus Notes Automation Classes, notes32.tlb
'Dim ln As lotus.NOTESSESSION
'Set ln = CreateObject("Notes.NotesSession")
'Dim db As lotus.NOTESDATABASE
'Set db = ln.GETDATABASE("", "mail\username.nsf")
'Dim mDoc As lotus.NOTESDOCUMENT
'Set mDoc = db.CREATEDOCUMENT


'Create an array of recipients (Separated by commas)
ArRecipients() = Split(Recipient, ",")

'Start a session to notes
Set Session = CreateObject("Notes.NotesSession")
On Error GoTo err_h

'Open the mail database in notes
UserName = Session.UserName
'************** ADD YOUR username.
Set Maildb = Session.GETDATABASE("", "D:\Data\Mails\machunt.nsf")
If Maildb.IsOpen = False Then
Maildb.OPENMAIL
End If

'Set up the new mail document
Set MailDoc = Maildb.CREATEDOCUMENT
MailDoc.Form = "Memo"
MailDoc.sendto = ArRecipients
MailDoc.Subject = UCase(Subject)
'MailDoc.Body = UCase(BodyText)
Set oBody = MailDoc.CREATERICHTEXTITEM("Body")
oBody.APPENDTEXT BodyText

'This is supposed to be the property, but works
'on some systems only
'without an apparent reason of failure
MailDoc.SAVEMESSAGEONSEND = SaveIt

'Set up the embedded object and attachment and attach it
If Attachment <> "" Then
Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
End If

'Send the document
MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
MailDoc.Send 1
MailDoc.Save True, True, False

'Clean Up
err_h:
Set lErr = Err
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
End Sub

machunt
02-25-2012, 09:12 PM
Hi Kenneth,

Thanks for your reply, It worked great. I just changed this line,as it was throwing error. I hope I am doing it right, Please provide me your guidance.


Call MailDoc.REPLACEITEMVALUE("SendTo", Range(B8.Value2))
' Changed to
Call MailDoc.REPLACEITEMVALUE("SendTo", Range("B8").Value2)


and added this line to send the CC mail.

Call MailDoc.REPLACEITEMVALUE("CopyTo", Range("B9").Value2)


The only problem I am facing is that,
I had some Test plans and ID's and general Info on the sheet (Main).So I added this line but it is showing me an error. [ Solved: using your method ]

Call Body.APPENDTEXT(Join(WorksheetFunction.Transpose(WorksheetFunction.Transpos e(Range("B12:F12"))), vbTab))

Call Body.APPENDTEXT(Range("B14:B23").Value2) ' I added this line as I want the info to be attached below the data.

Hi Kenneth,
I used your coding to add these ranges. Like this
Call Body.APPENDTEXT(Join(WorksheetFunction.Transpose(WorksheetFunction.Transpos e(Range("B15:C15"))), vbTab))
Call Body.APPENDTEXT("" & Chr$(13))
This solves the major problem ,
I just have one issue that the mail's and not showing in my send items. Other than this eveything is solved.Thanks Kenneth

Thanks for helping me out.
Best Regards,
Mac

Kenneth Hobs
02-26-2012, 11:02 AM
Did you try this?

MailDoc.SAVEMESSAGEONSEND = True

machunt
02-26-2012, 11:52 AM
Hi Kenneth,
Yes I tried it, still it's not showing in the sent item. Is it version problem, I am not understanding, I am using 8.5.2
Thanks
Mac

machunt
02-28-2012, 01:40 AM
Hi Kenneth,
I have used the CC field to reroute the mail to my inbox, as such It gets backed up in my inbox instead of sent item. That's solves the problem, not exactly but still a work around. And I can live with that :D
I really appreciate your help, support and guiding me through this. I don't know how to mark the post as solved. I request someone senior can please mark it solved.
Thanks forum.
Best Regards,
Mac
This post is 5* thanks to Kenneth

dpmaki
08-23-2012, 11:57 AM
I just posted a similar issue that I'm having however - my recipient list is constantly changing and I need 1 email to be sent to each individual user on the recipient list - they cannot be added to one email.