send each sheet to each persons via outlook

nagasree

New Member
Joined
Oct 30, 2021
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
I have a master sheet and a code to split it into separate sheet based on reviewer names, now i need to send all the splitted sheet to each of the reviewers based on sheet names, example: sheet named raj must be sent to raj@gmail.com, sheet named ravi must be sent to ravi@gmail.com I managed to find a code to send a single sheet via mail, i need help to send all the sheets to respective persons via outlook.
Attaching the code to send a single sheet.
Sub EmailWithOutlook()
Dim oApp As Object
Dim oMail As Object
Dim WB As Workbook
Dim FileName As String
Dim wSht As Worksheet
Dim ShtName As String
Dim CurrDate As String
CurrDate = format(Date, "MM-DD-YY")

Application.ScreenUpdating = False
' Make a copy of the active worksheet
' and save it to a temporary file
Sheets("raj").Activate
ActiveSheet.Copy
Set WB = ActiveWorkbook

FileName = WB.Worksheets(1).Name & " " & CurrDate
On Error Resume Next
Kill "C:\Users\Desktop\workfiles\" & FileName
On Error GoTo 0
WB.SaveAs FileName:="C:\Users\Desktop\workfiles\" & FileName

'Create and show the Outlook mail item
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
'Uncomment the line below to hard code a recipient
.To = "raj@gmail.com"
'Uncomment the line below to hard code a subject
.Subject = "Subject Line"
'Uncomment the lines below to hard code a body
.body = "Hi Raj" & vbCrLf & vbCrLf & _
"Please find the attached file for work"
.Attachments.Add WB.FullName
.Display
End With

'Delete the temporary file
'WB.ChangeFileAccess Mode:=xlReadOnly
'Kill WB.FullName
'WB.Close SaveChanges:=False

'Restore screen updating and release Outlook
Application.ScreenUpdating = True
Set oMail = Nothing
Set oApp = Nothing
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.
Hi,
not fully tested but give following a try & see if does what you want

Rich (BB code):
Sub EmailWithAttachment()
    
    Dim emailApplication    As Object, emailItem As Object
    Dim ws                  As Worksheet, wsMaster As Worksheet
    Dim wb                  As Workbook
    Dim RecipientName       As String, strFilename As String
    
    Const FolderPath As String = "C:\Users\Desktop\workfiles\"
    
    With Application
        .ScreenUpdating = False: .DisplayAlerts = False
    End With
    
    On Error GoTo myerror
    Set wsMaster = ThisWorkbook.Worksheets("Master")
    Set emailApplication = CreateObject("Outlook.Application")
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> wsMaster.Name Then
            
            ws.Copy
            Set wb = ActiveWorkbook
            
            RecipientName = wb.Worksheets(1).Name
            strFilename = RecipientName & ".xlsx"
            
            wb.SaveAs Filename:=FolderPath & strFilename, FileFormat:=51
            
            emailaddress = RecipientName & "@gmail.com"
            
            Set emailItem = emailApplication.CreateItem(0)
            
            With emailItem
                .To = emailaddress
                .Subject = "Subject Line"
                .body = "Hi " & RecipientName & vbCrLf & vbCrLf & _
                        "Please find the attached file For work"
                .Attachments.Add wb.FullName
                .Display
            End With
            
            'clean up
            wb.ChangeFileAccess Mode:=xlReadOnly
            Kill wb.FullName
            
            wb.Close False
            
            Set emailItem = Nothing
            Set wb = Nothing
        End If
    Next ws
    
    MsgBox "All Complete", 64, "Complete"
    
myerror:
    If Not wb Is Nothing Then wb.Close False
    With Application
        .ScreenUpdating = True: .DisplayAlerts = True
    End With
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Change the name of the Master sheet shown in BOLD as required

Dave
 
Upvote 0
Hi,
not fully tested but give following a try & see if does what you want

Rich (BB code):
Sub EmailWithAttachment()
   
    Dim emailApplication    As Object, emailItem As Object
    Dim ws                  As Worksheet, wsMaster As Worksheet
    Dim wb                  As Workbook
    Dim RecipientName       As String, strFilename As String
   
    Const FolderPath As String = "C:\Users\Desktop\workfiles\"
   
    With Application
        .ScreenUpdating = False: .DisplayAlerts = False
    End With
   
    On Error GoTo myerror
    Set wsMaster = ThisWorkbook.Worksheets("Master")
    Set emailApplication = CreateObject("Outlook.Application")
   
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> wsMaster.Name Then
           
            ws.Copy
            Set wb = ActiveWorkbook
           
            RecipientName = wb.Worksheets(1).Name
            strFilename = RecipientName & ".xlsx"
           
            wb.SaveAs Filename:=FolderPath & strFilename, FileFormat:=51
           
            emailaddress = RecipientName & "@gmail.com"
           
            Set emailItem = emailApplication.CreateItem(0)
           
            With emailItem
                .To = emailaddress
                .Subject = "Subject Line"
                .body = "Hi " & RecipientName & vbCrLf & vbCrLf & _
                        "Please find the attached file For work"
                .Attachments.Add wb.FullName
                .Display
            End With
           
            'clean up
            wb.ChangeFileAccess Mode:=xlReadOnly
            Kill wb.FullName
           
            wb.Close False
           
            Set emailItem = Nothing
            Set wb = Nothing
        End If
    Next ws
   
    MsgBox "All Complete", 64, "Complete"
   
myerror:
    If Not wb Is Nothing Then wb.Close False
    With Application
        .ScreenUpdating = True: .DisplayAlerts = True
    End With
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Change the name of the Master sheet shown in BOLD as required

Dave
Hello the macro is running and it shows subscription out of range
 
Upvote 0
Hello the macro is running and it shows subscription out of range

Did you rename the master sheet where shown in RED to that of your actual master sheet name?

Dave
 
Upvote 0
yes i did
found this, but this code is also not working, can u please modify it according to my need
 
Upvote 0
can you comment out this line

VBA Code:
On Error GoTo myerror

run the code & tell me which line is showing the error

Dave
 
Upvote 0
can you comment out this line

VBA Code:
On Error GoTo myerror

run the code & tell me which line is showing the error

Dave
Its not taking to line, no yellow highlighted, directly says subscription out of range
 
Upvote 0
Its not taking to line, no yellow highlighted, directly says subscription out of range

can you place copy of your workbook with dummy data in a filesharing site like dropbox & provide a link to it?

Dave
 
Upvote 0
found this, but this code is also not working, can u please modify it according to my need

if you want to use another's code suggest that you liaise with the author.

Dave
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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