Hi Experts,
Seek your help here for improving the code below to send HTML Format Mail Body.
I have somehow written a code which can pull some 6 lines in the mail body with line breaks . I wanted to have a code which could send color, bold , italic messages as I have to often use Bold and italic to highlight deadlines but this code just try to publish text. I dont know how to invoke a HTML message and second even if I invoke a HTML message, I will be typing the same in excel workbook so want that if I type something in BOLD or ITALIC or UNDERLINE or Color, it should be refected the same way. Can I get some mentors here to show me the right path to achieve this.
Thanks in advance.
Also the code may be crude but work so if any value add is suggested to improvise the same and do it a more appropriate way, I will be really looking towards it.
Thanks again !!!!
Private Sub Click_here_Click()
Dim userconfirmation As String
'User Intent validation
Line1:
userconfirmation = MsgBox("Are you ready to send mails to 'ALL' Recievers Marked in this List." & vbNewLine & _
" This action CANNOT be UNDONE.", vbYesNoCancel, "Confirmation")
If userconfirmation = vbNo Then
MsgBox ("OK , I understand you are not ready now." & vbNewLine & "Prepare your data and come back ")
Exit Sub
Else
If userconfirmation = vbYes Then
MsgBox ("Let's Proceed")
Else
MsgBox ("OK , I understand you are not ready now." & vbNewLine & "Prepare your data and come back ")
Exit Sub
End If
End If
'Variables for Email
Dim Subject As String
Dim Attachment As String
Dim Recipient As Variant
Dim CCRecipient As Variant
Dim BCCRecipient As Variant
Dim BodyText As Variant
Dim SaveIt As Boolean
Dim Signature As Variant
Dim Sender As Variant
'Body text Variant
Dim Line_1 As Variant
Dim Line_2 As Variant
Dim Line_3 As Variant
Dim Line_4 As Variant
Dim Line_5 As Variant
Dim Line_6 As Variant
Dim startrow As Variant
Dim ToCol As Variant
Dim Cccol As Variant
Dim bcccol As Variant
Dim subjectcol As Variant
Dim bodytextcol As Variant
Dim Saluation As Variant
'Variable for database and Lotus Note Handling
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)
Dim NUIWorkspace As Object
' Dim i As Double
Dim jpassword As Variant
Sender = ActiveWorkbook.BuiltinDocumentProperties("Author")
'Start a session to notes
'Line2:
' jpassword = InputBox("Please provide your Lotus Notes Password")
' If jpassword = "" Then
' MsgBox (" Password cannot be blank")
' GoTo Line2
' End If
Set Session = CreateObject("Notes.NotesSession")
'Next line only works with 5.x and above. Replace password with your password
'Session.Initialize (jpassword)
'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
startrow = 3
ToCol = 4
Cccol = 5
bcccol = 6
subjectcol = 7
Saluation = 2
bodytextcol = 9
Line_1 = 10
Line_2 = 11
Line_3 = 12
Line_4 = 13
Line_5 = 14
Line_6 = 15
Do While Cells(startrow, ToCol).Value <> ""
Recipient = Cells(startrow, ToCol)
CCRecipient = Cells(startrow, Cccol)
BCCRecipient = Cells(startrow, bcccol)
Subject = Cells(startrow, subjectcol)
Signature = "Regards," & vbNewLine & Sender
If Cells(startrow, Saluation) <> "" Then
BodyText = "Dear " & Cells(startrow, Saluation) & "," & _
vbNewLine & Cells(startrow, Line_1) & _
vbNewLine & Cells(startrow, Line_2) & _
vbNewLine & Cells(startrow, Line_3) & _
vbNewLine & Cells(startrow, Line_4) & _
vbNewLine & Cells(startrow, Line_5) & _
vbNewLine & Cells(startrow, Line_6) & _
vbNewLine & vbNewLine & vbNewLine
BodyText = BodyText & vbNewLine & Signature
Else
BodyText = "Hi " & " ," & _
vbNewLine & Cells(startrow, Line_1) & _
vbNewLine & Cells(startrow, Line_2) & _
vbNewLine & Cells(startrow, Line_3) & _
vbNewLine & Cells(startrow, Line_4) & _
vbNewLine & Cells(startrow, Line_5) & _
vbNewLine & Cells(startrow, Line_6) & _
vbNewLine & vbNewLine & vbNewLine
BodyText = BodyText & vbNewLine & Signature
End If
' MsgBox BodyText
'Set up the new mail document
Set MailDoc = Maildb.CREATEDOCUMENT
MailDoc.Form = "Memo"
MailDoc.sendto = Recipient
MailDoc.copyto = CCRecipient
MailDoc.blindcopyto = BCCRecipient
MailDoc.Subject = Subject
MailDoc.body = BodyText
MailDoc.SAVEMESSAGEONSEND = SaveIt
MailDoc.SAVEMESSAGEONSEND = True
'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")
MailDoc.CreateRichTextItem (Attachment)
End If
'Send the document
MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
'This line has been commented to avoid mail triffers. while testing , you need to open the same.
MailDoc.SEND 0, Recipient
'Clean Up
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
startrow = startrow + 1
Loop
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
MsgBox (Cells(startrow, ToCol).Address & " is empty hence every record after that is suspended from Processing.")
MsgBox (startrow - 3 & " Mails Sent successfully")
End Sub
Bookmarks