Send specific excel worksheet as attachment in lotus notes email to recipients defined in a separate worksheet of the workbook

lakshmi.acharya

New Member
Joined
Jun 16, 2011
Messages
6
Hi,

I am trying to automate the distribution of an internal sales report to different individuals in the organization. I am using Excel 2003 and Lotus 6.5. I have no knowledge of VB/VBA but I'm sure that this can be automated through a macro and hence seeking the help of experts here.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
My workbook is named “Sales Data.xls” and is stored in the location D:\Sales Data. Sales Data.xls has 2 worksheets namely: ‘Master’ and ‘Recipients’
<o:p> </o:p>
Master worksheet has sales information (region, city, product, stock, sales quantity, cost, net sales price) for different cities in <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:country-region w:st="on">India</st1:country-region> (Mumbai, <st1:City w:st="on">Delhi</st1:City>, <st1:City w:st="on"><st1:place w:st="on">Bangalore</st1:place></st1:City>, Chennai etc.). The City names are in Column B of this worksheet.
<o:p> </o:p>
I have used a code available on this forum to split the data in ‘Master’ into separate worksheets for each City and each worksheet is named after the City. So, once I run this macro, I have the following worksheets in my workbook:
Recipient, Master, Mumbai, Delhi, Bangalore, Chennai, etc.<o:p></o:p>
<o:p> </o:p>
‘Recipient’ has email IDs of individuals who are responsible for the sales in each of these cities and I need to be able to send them an email with data for just their city. Information in worksheet ‘recipient’ is stored as below:
<o:p> </o:p>
Column A Column B
Mumbai xyz@email.com<o:p></o:p>
<st1:City w:st="on"><st1:place w:st="on">Delhi</st1:place></st1:City> abc@email.com<o:p></o:p>
<st1:City w:st="on"><st1:place w:st="on">Bangalore</st1:place></st1:City> pqr@email.com<o:p></o:p>
Chennai mno@email.com<o:p></o:p>
<o:p> </o:p>
Now, I need a macro to automate the distribution of the respective worksheet as attachment to the defined recipient through Lotus Notes. However, the worksheet ‘Master’ and ‘Recipient’ should not be distributed to anyone.<o:p></o:p>
<o:p> </o:p>
If the content of the email can be automated as well, it will be helpful.<o:p></o:p>
Subject: Sales Report for <date> (today’s date)<o:p></o:p>
Email body: <o:p></o:p>
Please review the sales report for <date> (today’s date).<o:p></o:p>
<o:p> </o:p>
“Attachment” (worksheet name.xls) (eg: Mumbai.xls)<o:p></o:p>
<o:p> </o:p>
Regards,<o:p></o:p>
Lakshmi<o:p></o:p>
<o:p> </o:p>
I would tremendously appreciate any help that I can receive in accomplishing this task.<o:p></o:p>
<o:p> </o:p>
Thanks and regards,<o:p></o:p>
Lakshmi<o:p></o:p>
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
This code saves each 'city' sheet as a separate workbook containing just that sheet:
Code:
Option Explicit

Sub Save_City_Sheets()

    'Save each 'city' sheet in the current workbook as a separate workbook containing just that sheet

    Dim saveInFolder As String
    Dim sh As Worksheet
    
    saveInFolder = "C:\Temp\Excel\"     'CHANGE AS REQUIRED
    
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    For Each sh In ThisWorkbook.Worksheets
        If sh.Name <> "Master" And sh.Name <> "Recipients" Then
        
            'Create a new workbook containing just this sheet
            sh.Copy
            
            'Suppress prompt if file already exists
            Application.DisplayAlerts = False
    
            'Save the single sheet as a workbook
            ActiveWorkbook.SaveAs saveInFolder & sh.Name
            
            'Reinstate prompts
            Application.DisplayAlerts = False
                   
            ActiveWorkbook.Close False
        End If
    Next
    
End Sub
Then combine this with code from http://www.rondebruin.nl/notes.htm to achieve your requirement, or search this forum for creating/sending email via Lotus Notes.
 
Upvote 0
Thank you, John. I now have a macro that I'm able to use in several other situations as well!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Your macro is helping me split the 'City' worksheets into several workbooks. As suggested by you I also checked Ron's post for the macro to send the email by Lotus Notes. <o:p></o:p>
<o:p> </o:p>
Two macros seemed to fit my situation:<o:p></o:p>
<o:p> </o:p>
1. Send the active sheet as an attachment: Failed at the below code (in red)<o:p></o:p>
<o:p> </o:p>
stAttachment = stPath & "\" & stFileName & ".xls"<o:p></o:p>
<o:p></o:p>
'Save and close the temporarily workbook.<o:p></o:p>
With ActiveWorkbook<o:p></o:p>
.SaveAs stAttachment<o:p></o:p>
.Close<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
2. Send worksheets to several recipients: I think the below code needs to be modified to read the worksheet name and the respective email ID as defined in ‘Recipient’ worksheet.<o:p></o:p>
<o:p> </o:p>

'Retrieve the list of recipients.<o:p></o:p></PRE>
With wsSheet<o:p></o:p></PRE>
lnLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row<o:p></o:p></PRE>
vaRecipients = .Range("A1:A" & lnLastRow).Value<o:p></o:p></PRE>
End With<o:p></o:p></PRE><o:p> </o:p>
Sorry, I'm extremely new to this and hence I was not able to figure out how each 'City' worksheet can get distributed to the list of recipients defined in my 'Recipient' workbook. I'm still hopeful that this can be accomplished by a macro but maybe I'm overreaching?!<o:p></o:p>
<o:p></o:p>
Any help would be much appreciated!!<o:p></o:p>
<o:p></o:p>
Regards,<o:p></o:p>
Lakshmi<o:p></o:p>
<o:p> </o:p>
 
Upvote 0
1. Send the active sheet as an attachment: Failed at the below code (in red)
What's the error? What's the value of stAttachment? I suspect an invalid file name or trying to save to a non-existent folder. Try debugging the code in the VB editor using the Locals window or the Immediate window (use the View menu to display these windows). Type ?stattachment in the Immediate window to show its value.

For looping through the cities and email addresses on the Recipients sheet, try something like:
Code:
    Dim lastRow As Long, row As Long
    
    With Sheets("Recipients")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).row
        For row = 2 To lastRow   'start at row 2 because row 1 is assumed to contain column headings
            MsgBox "City: " & .Cells(row, "A") & vbNewLine & "Email: " & .Cells(row, "B")
        Next
    End With
 
Upvote 0
Here is my code for sending the email after I have created different worksheets for each "City". It however, still does not send the email :(. I'm hoping you'll be able to review and let me know what is it that I'm doing wrong. I feel the vaRecipient defined in the code is somehow incorrect but I could very well be wrong.

The macro loops thru all the cities and displays the City Name and the Respective Email ID where the email needs to be sent. But the email does not get sent from Lotus. I'm assuming the macro just shows the City and email from the 'Recipients" sheet. When I debug the macro, the error message I get is: "Subscript out of range"


My code (copied from Run's website and with your inputs) starts here:

Sub Send_Sheets_Notes_Email()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
'Notes parameter for attaching the Excel files.<o:p></o:p>
Const EMBED_ATTACHMENT As Long = 1454<o:p></o:p>
<o:p></o:p>
'A folder to temporarily store the created Excel files in.<o:p></o:p>
Const stPath As String = "D:\Attachments"<o:p></o:p>
<o:p></o:p>
'The subject for the outgoing e-mails.<o:p></o:p>
Const stSubject As String = "Sales Report"<o:p></o:p>
<o:p></o:p>
'The message in the bodies of the outgoing e-mails.<o:p></o:p>
Const vaMsg As String = "The Sales Report is attached for your review." & vbCrLf & _<o:p></o:p>
"Regards," & vbCrLf & _<o:p></o:p>
"Lakshmi"<o:p></o:p>
<o:p></o:p>
'Variable that holds the list of recipients for each worksheet.<o:p></o:p>
Dim vaRecipients As Variant<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
'Variable which holds each worksheet's name.<o:p></o:p>
Dim stFileName As String<o:p></o:p>
<o:p></o:p>
'Variables for Notes.<o:p></o:p>
Dim noSession As Object<o:p></o:p>
Dim noDatabase As Object<o:p></o:p>
Dim noDocument As Object<o:p></o:p>
Dim noEmbedObject As Object<o:p></o:p>
Dim noAttachment As Object<o:p></o:p>
Dim stAttachment As String<o:p></o:p>
<o:p></o:p>
'Variables for Excel.<o:p></o:p>
Dim wbBook As Workbook<o:p></o:p>
Dim wsSheet As Worksheet<o:p></o:p>
Dim lnLastRow As Long<o:p></o:p>
Dim row As Long<o:p></o:p>
<o:p></o:p>
On Error GoTo Error_Handling<o:p></o:p>
<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
<o:p></o:p>
With Sheets("Recipient")<o:p></o:p>
InlastRow = .Cells(.Rows.Count, "A").End(xlUp).row<o:p></o:p>
For row = 2 To InlastRow 'start at row 2 because row 1 is assumed to contain column headings<o:p></o:p>
MsgBox "City: " & .Cells(row, "A") & vbNewLine & "Email: " & .Cells(row, "B")<o:p></o:p>
<o:p></o:p>
Set wbBook = ThisWorkbook<o:p></o:p>
<o:p></o:p>
'Loop through the collection of worksheets in the workbook.<o:p></o:p>
For Each wsSheet In wbBook.Worksheets<o:p></o:p>
With wsSheet<o:p></o:p>
'Copy the worksheet to a new workbook.<o:p></o:p>
.Copy<o:p></o:p>
'Retrieve the worksheet's name.<o:p></o:p>
stFileName = .Name<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
'Create the full path and name of the workbook.<o:p></o:p>
stAttachment = stPath & "\" & stFileName & ".xls"<o:p></o:p>
<o:p></o:p>
'Save and close the temporarily workbook.<o:p></o:p>
With ActiveWorkbook<o:p></o:p>
.SaveAs stAttachment<o:p></o:p>
.Close<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
'Instantiate the Lotus Notes COM's Objects.<o:p></o:p>
Set noSession = CreateObject("Notes.NotesSession")<o:p></o:p>
Set noDatabase = noSession.GETDATABASE("", "")<o:p></o:p>
<o:p></o:p>
'If Lotus Notes is not open then open the mail-part of it.<o:p></o:p>
If noDatabase.IsOpen = False Then noDatabase.OPENMAIL<o:p></o:p>
<o:p></o:p>
'Create the e-mail and add the attachment.<o:p></o:p>
Set noDocument = noDatabase.CreateDocument<o:p></o:p>
Set noAttachment = noDocument.CreateRichTextItem("stAttachment")<o:p></o:p>
Set noEmbedObject = noAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)<o:p></o:p>
<o:p></o:p>
'Add values to the created e-mail main properties.<o:p></o:p>
With noDocument<o:p></o:p>
.Form = "Memo"<o:p></o:p>
.SendTo = vaRecipients<o:p></o:p>
.Subject = stSubject<o:p></o:p>
.Body = vaMsg<o:p></o:p>
.SaveMessageOnSend = True<o:p></o:p>
.PostedDate = Now()<o:p></o:p>
.Send 0, vaRecipients<o:p></o:p>
End With<o:p></o:p>
'Delete the temporarily workbook.<o:p></o:p>
Kill stAttachment<o:p></o:p>
Next wsSheet<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
MsgBox ("The e-mails have successfully been created and distributed."), vbInformation<o:p></o:p>
<o:p></o:p>
Next<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
ExitSub:<o:p></o:p>
'Release objects from memory.<o:p></o:p>
Set noEmbedObject = Nothing<o:p></o:p>
Set noAttachment = Nothing<o:p></o:p>
Set noDocument = Nothing<o:p></o:p>
Set noDatabase = Nothing<o:p></o:p>
Set noSession = Nothing<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
Exit Sub<o:p></o:p>
<o:p></o:p>
Error_Handling:<o:p></o:p>
MsgBox "Error number: " & Err.Number & vbNewLine & _<o:p></o:p>
"Description: " & Err.Description, vbOKOnly<o:p></o:p>
Resume ExitSub<o:p></o:p>
End Sub
 
Upvote 0
vaRecipients is declared but not defined and is used in the Lotus SendTo and Send. Therefore I think you need:

vaRecipients = .Cells(row, "B")

immediately after the first MsgBox to define it as the email address for that row.

Also, you have nested For...Next loops which will send all the worksheets to each recipient. I think you need to remove the inner For...Next loop.
 
Upvote 0
Yeah - you are right! Once the vaRecipients got defined the email started pouring in. But on the flip side, like you also suggested, the email with each worksheet is also being sent to each recipient.

I am not sure I understood how to remove the inner 'for' and 'next' loop. I don't think I can just delete the 'for' ? I'm sorry to be such a novice at this but you've been a tremendous help and I've been learning the logic as and when you've responded. I just do not know the keywords and hence lost at what should be done to change the inner loop. Could you please help, yet again?
 
Upvote 0
Just delete the inner For and Next statements, along with the code related to the worksheets, which isn't needed. Put the Lotus Notes initialisation before the remaining loop because it only needs to be done once. Here's the code (untested) with those changes:
Code:
Sub Send_Sheets_Notes_Email()

    'Notes parameter for attaching the Excel files.
    Const EMBED_ATTACHMENT As Long = 1454
    
    'A folder to temporarily store the created Excel files in.
    Const stPath As String = "D:\Attachments"
    
    'The subject for the outgoing e-mails.
    Const stSubject As String = "Sales Report"
    
    'The message in the bodies of the outgoing e-mails.
    Const vaMsg As String = "The Sales Report is attached for your review." & vbCrLf & _
    "Regards," & vbCrLf & _
    "Lakshmi"
    
    'Variable that holds the list of recipients for each worksheet.
    Dim vaRecipients As Variant
    
    
    'Variable which holds each worksheet's name.
    Dim stFileName As String
    
    'Variables for Notes.
    Dim noSession As Object
    Dim noDatabase As Object
    Dim noDocument As Object
    Dim noEmbedObject As Object
    Dim noAttachment As Object
    Dim stAttachment As String
    
    'Variables for Excel.
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim lnLastRow As Long
    Dim lastRow As Long, row As Long
    
    'Instantiate the Lotus Notes OLE Objects.
    Set noSession = CreateObject("Notes.NotesSession")
    Set noDatabase = noSession.GETDATABASE("", "")
    
    'If Lotus Notes is not open then open the mail-part of it.
    If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
    
    With Sheets("Recipient")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).row
        For row = 2 To lastRow 'start at row 2 because row 1 is assumed to contain column headings
            MsgBox "City: " & .Cells(row, "A") & vbNewLine & "Email: " & .Cells(row, "B")
            
            vaRecipients = .Cells(row, "B")
            stAttachment = stPath & "\" & .Cells(row, "A") & ".xls"
            
            'Create the e-mail and add the attachment.
            Set noDocument = noDatabase.CreateDocument
            Set noAttachment = noDocument.CreateRichTextItem("Attachment")
            Set noEmbedObject = noAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
        
            'Add values to the created e-mail main properties.
            With noDocument
                .Form = "Memo"
                .SendTo = vaRecipients
                .Subject = stSubject
                .Body = vaMsg
                .SaveMessageOnSend = True
                .PostedDate = Now()
                .Send 0, vaRecipients
            End With
        Next
    
        MsgBox ("The e-mails have successfully been created and distributed."), vbInformation
    End With
    
    'Release objects from memory.
    Set noEmbedObject = Nothing
    Set noAttachment = Nothing
    Set noDocument = Nothing
    Set noDatabase = Nothing
    Set noSession = Nothing
    
End Sub
 
Upvote 0
I've been trying to change this code to work for Outlook but i keep getting errors. Can you please help me?

This is the only part I changed. I've been playing around with it all day and yesterday and I just can't find any resources to help me. Also stupid question but is D:\ mean the desktop because thats where I want it to store the PDFs. Or is that another Drive?

'Instantiate the Lotus Notes OLE Objects.
Set noSession = CreateObject("Outlook.Application")
Set noDatabase = noSession.CreateItem(0)

I know this is a really old thread but I am really hoping you can help me.

Thank you!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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