vba to send mail through lotus notes

M15tyw00d

Active Member
Joined
Nov 19, 2010
Messages
264
I have the following from a post. Can it be revised to send only the active worksheet rather than the whole workbook?

quote_icon.png
Originally Posted by Miss Pocahontas NateO:<o:p></o:p>
<o:p></o:p>
Thanks for the original code below which attaches and sends the active workbook through Lotus Notes.<o:p></o:p>
<o:p></o:p>
I removed the:<o:p></o:p>
<o:p></o:p>
MailDoc.PostedDate = Now()
On Error GoTo errorhandler1
MailDoc.Send 0, Recipient<o:p></o:p>
<o:p></o:p>
...and replaced it with:<o:p></o:p>
<o:p></o:p>
Set workspace = CreateObject("Notes.NotesUIWorkspace")
Call workspace.EDITDOCUMENT(True, MailDoc).GOTOFIELD("Body")<o:p></o:p>
(I found this in http://www.ozgrid.com/forum/showthread.php?t=18259)<o:p></o:p>
<o:p></o:p>
...which keeps the email message open instead of sending.<o:p></o:p>
<o:p></o:p>
Also, I changed the Recipient to ccRecipient, so the user can type whichever email address he needs to address to from his custom drop-down list within Lotus Notes, and type his message in the body of the email. This is the setup that is most useful for my purposes.<o:p></o:p>
<o:p></o:p>
The only thing I'm having trouble with is:<o:p></o:p>
<o:p></o:p>
1. After the user revises the workbook, but forgets to click save, if the user runs the code, it will send the workbook WITHOUT the revisions. How can I integrate coding within this same module to save the workbook before attaching it to the email?<o:p></o:p>
<o:p></o:p>
2. How can I disable the dialog box that pops up, which the user needs to click OK? It's the dialog box that contains the FilePath of the workbook. I tried setting DisplayAlerts to False, but that didn't work. I'm new to <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help;">VBA</acronym>. I just want the code to run and open the email message without that dialog box.<o:p></o:p>
<o:p></o:p>
3. When the code is run, the Lotus Notes email message window remains in the background behind all the other open windows. How can I get it to pop up in front of all the other windows automatically?<o:p></o:p>
<o:p></o:p>
NateO, below is your code, which I modified per my comments above:<o:p></o:p>
<o:p></o:p>
Sub LotusNotsSendActiveWorkbook()
'Send an e-mail & attachment using Lotus Not(s)
'Original Code by Nate Oliver (NateO)<o:p></o:p>
'Declare Variables for file and macro setup<o:p></o:p>
Dim UserName As String, MailDbName As String, ccRecipient As String, attachment1 As String
Dim Maildb As Object, MailDoc As Object, AttachME As Object, Session As Object
Dim EmbedObj1 As Object<o:p></o:p>
With Application
.ScreenUpdating = False
.DisplayAlerts = False<o:p></o:p>
<o:p></o:p>
' Open and locate current LOTUS NOTES User
Set Session = CreateObject("Notes.NotesSession")
UserName = Session.UserName
MailDbName = _
Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
Else
Maildb.OPENMAIL
End If<o:p></o:p>
<o:p></o:p>
' Create New Mail and Address Title Handlers
Set MailDoc = Maildb.CreateDocument<o:p></o:p>
MailDoc.Form = "Memo"<o:p></o:p>
' Select range of e-mail addresses
ccRecipient = Sheets("EmailSheet").Range("B2").Value
' Or send to a signle address
' Recipient = "yourname@isp.com"
MailDoc.CopyTo = ccRecipient<o:p></o:p>
' Subject & Body stored in a**worksheet
MailDoc.Subject = Sheets("EmailSheet").Range("C2").Value
' MailDoc.Body = Sheets("EmailSheet").Range("ENTER CELL OF BODY").Value
' These can be entered here manually instead
' MailDoc.Subject = "Check this out!"
' MailDoc.Body = "Made you look!"<o:p></o:p>
<o:p></o:p>
' Select Workbook to Attach to E-Mail
MailDoc.SaveMessageOnSend = True
MsgBox ActiveWorkbook.FullName
attachment1 = ActiveWorkbook.FullName<o:p></o:p>
If attachment1 <> "" Then
On Error Resume Next
Set AttachME = MailDoc.CREATERICHTEXTITEM("attachment1")
Set EmbedObj1 = AttachME.EmbedObject(1454, "attachment1", ActiveWorkbook.FullName, "")
On Error Resume Next
End If<o:p></o:p>
<o:p></o:p>
'Displays email message without sending; user needs to click Send
Set workspace = CreateObject("Notes.NotesUIWorkspace")
Call workspace.EDITDOCUMENT(True, MailDoc).GOTOFIELD("Body")<o:p></o:p>
<o:p></o:p>
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing<o:p></o:p>
<o:p></o:p>
.ScreenUpdating = True
.DisplayAlerts = True
End With<o:p></o:p>
<o:p></o:p>
errorhandler1:<o:p></o:p>
<o:p></o:p>
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing<o:p></o:p>
<o:p></o:p>

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Add this code to save the active sheet as a .xlsx file:
Code:
    Dim fileName
    
    fileName = ActiveWorkbook.Path & "\" & ActiveSheet.Name & ".xlsx"
    ActiveSheet.Copy
    ActiveWorkbook.SaveCopyAs fileName
    ActiveWorkbook.Close SaveChanges:=False
and change the EmbedObject line to:
Rich (BB code):
Set EmbedObj1 = AttachME.EmbedObject(1454, "attachment1", fileName, "")
and delete the now redundant lines referring to attachment1.
 
Upvote 0
So would I change it to look like this?

Code:
Sub LotusNotsSendActiveWorkbook()'Send an e-mail & attachment using Lotus Not(s)
'Original Code by Nate Oliver (NateO)
'Declare Variables for file and macro setup
Dim UserName As String, MailDbName As String, ccRecipient As String, attachment1 As String
Dim Maildb As Object, MailDoc As Object, AttachME As Object, Session As Object
Dim EmbedObj1 As Object
With Application
.ScreenUpdating = False
.DisplayAlerts = False


' Open and locate current LOTUS NOTES User
Set Session = CreateObject("Notes.NotesSession")
UserName = Session.UserName
MailDbName = _
Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
Else
Maildb.OPENMAIL
End If


' Create New Mail and Address Title Handlers
Set MailDoc = Maildb.CreateDocument
MailDoc.Form = "Memo"
' Select range of e-mail addresses
ccRecipient = "misty.wood@horcros.com"
' Or send to a signle address
' Recipient = "yourname@isp.com"
MailDoc.CopyTo = ccRecipient
' Subject & Body stored in a**worksheet
' MailDoc.Subject = Sheets("EmailSheet").Range("C2").Value
' MailDoc.Body = Sheets("EmailSheet").Range("ENTER CELL OF BODY").Value
' These can be entered here manually instead
MailDoc.Subject = "New Order"
MailDoc.Body = "Ready to print SO# "


' Select Workbook to Attach to E-Mail
  [COLOR=#ff0000]  Dim fileName
    [/COLOR]
[COLOR=#ff0000]    fileName = ActiveWorkbook.Path & "\" & ActiveSheet.Name & ".xlsx"
    ActiveSheet.Copy
    ActiveWorkbook.SaveCopyAs fileName
    ActiveWorkbook.Close SaveChanges:=False[/COLOR]
MailDoc.SaveMessageOnSend = True
MsgBox ActiveWorkbook.FullName
attachment1 = ActiveWorkbook.FullName[COLOR=#FF0000] ---  THIS ROW GETS DELETED[/COLOR]
If attachment1 <> "" Then[COLOR=#FF0000] ---  THIS ROW GETS DELETED[/COLOR]
On Error Resume Next[COLOR=#FF0000] ---  THIS ROW GETS DELETED[/COLOR]
Set AttachME = MailDoc.CREATERICHTEXTITEM("attachment1")[COLOR=#FF0000] ---  THIS ROW GETS DELETED[/COLOR]
Set EmbedObj1 = AttachME.EmbedObject(1454, "attachment1", ActiveWorkbook.FullName, "")[COLOR=#ff0000] ---  THIS ROW GETS DELETED[/COLOR]
[COLOR=#ff0000][FONT=Verdana]Set EmbedObj1 = AttachME.EmbedObject(1454, "attachment1", fileName, "")[/FONT][/COLOR]
On Error Resume Next
End If


'Displays email message without sending; user needs to click Send
Set workspace = CreateObject("Notes.NotesUIWorkspace")
Call workspace.EDITDOCUMENT(True, MailDoc).GOTOFIELD("Body")


Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing


.ScreenUpdating = True
.DisplayAlerts = True
End With


errorhandler1:


Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing


End Sub
 
Last edited:
Upvote 0
Yes - it looks correct, but you'll find that you need the Set AttachME line (I should have said the attachment1 variable). Why not try it and see if it works?
 
Upvote 0
It says I don't have a beginning If to my End If...

Code:
' Select Workbook to Attach to E-MailDim fileName
    
fileName = ActiveWorkbook.Path & "\" & ActiveSheet.Name & ".xlsx"
ActiveSheet.Copy
ActiveWorkbook.SaveCopyAs fileName
ActiveWorkbook.Close SaveChanges:=False
MailDoc.SaveMessageOnSend = True
MsgBox ActiveWorkbook.FullName
Set AttachME = MailDoc.CREATERICHTEXTITEM("attachment1")
Set EmbedObj1 = AttachME.EmbedObject(1454, "attachment1", fileName, "")
On Error Resume Next
End If
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,715
Members
449,118
Latest member
MichealRed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top