Macro to display emails

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have the folowing code below and have to select "allow" before sending the emails.

The code allows me to email each of the sheets to a recvipient address in cell A1 on each of the sheets


I would like to be able to view the emails be clicking send on Outlook

It would be appreciated if someone could please assist me in amending the code


Code:
 Sub mailWorksheets()                              


Application.ScreenUpdating = False               'freeze display for speedup

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Each w In ThisWorkbook.Worksheets       'loop through all sheets
If w.[a1].Value Like "*@*" Then                   'check for email address in cell [A1]
w.Copy                                                       'copy worksheet to new workbook

ActiveWorkbook.SaveAs "Sheet " & w.Name & " of " & ThisWorkbook.Name & ".xls"
zSendTo = ActiveSheet.[a1].Value                 'email recipient
zSubject = "Sales  Comm File"                   '<-edit subject line here as required
ActiveWorkbook.SendMail zSendTo, zSubject   'send email to recipient

ActiveWorkbook.ChangeFileAccess xlReadOnly  'change workbook to read-only for deletion
Kill ActiveWorkbook.FullName                         'delete saved copy of temporary workbook
ActiveWorkbook.Close False                            'close temporary workwook

End If                                                          'end of test for email address in cell [A1]
Next w                                                         'process next worksheet
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Application.ScreenUpdating = True           'refresh display

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
hi howard,

try the below, untested

Code:
Sub mail()
Dim OutApp As Object
Dim OutMail As Object
On Error Resume Next
Application.ScreenUpdating = False               'freeze display for speedup
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Each w In ThisWorkbook.Worksheets       'loop through all sheets
If w.[a1].Value Like "*@*" Then                   'check for email address in cell [A1]
w.Copy                                                       'copy worksheet to new workbook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
 
ActiveWorkbook.SaveAs "Sheet " & w.Name & " of " & ThisWorkbook.Name & ".xls"
zworkbook = ActiveWorkbook.FullName
zsendto = ActiveSheet.[a1].Value                 'email recipient
zsubject = "Sales  Comm File"                   '<-edit subject line here as required
With OutMail
        .to = zsendto
        .CC = ""
        .BCC = ""
        .Subject = zsubject
        .Attachments.Add zworkbook.FullName
        .Display
        
    Set OutMail = Nothing
    Set OutApp = Nothing
End With
ActiveWorkbook.ChangeFileAccess xlReadOnly  'change workbook to read-only for deletion
Kill ActiveWorkbook.FullName                         'delete saved copy of temporary workbook
ActiveWorkbook.Close False                            'close temporary workwook
End If                                                          'end of test for email address in cell [A1]
Next w                                                         'process next worksheet
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Application.ScreenUpdating = True           'refresh display
End Sub
 
Upvote 0
Thanks for the help, Barry

The respect sheets are no longer attached as per my original code

It would be appreciated if you could amend this so that the sheet containing the email address is attached to the applicable recipient
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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