E-mail individual sheets from one WB

Javi

Active Member
Joined
May 26, 2011
Messages
440
Hi All, I would appreciate any assistance you experts could give me. Thank you in advance.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
I use the below code to e-mail the active sheet in my workbook and it works well.
<o:p> </o:p>
However my workbook has many sheets and its taking me a very very long time to go into each sheet and e-mail. Each sheet need to go to a separate e-mail address. My code also stops and requires me to hit the send button I would like to eliminate this step and have it sent automatically when I run the code.
<o:p> </o:p>
I guess what I'm asking is there a way to call out in the code specific sheets to go to specific e-mail addresses?
<o:p> </o:p>
For example.
<o:p> </o:p>
Workbook monthlyreport has “sheet1=325” “sheet2=326” “sheet3=327” and so on…
<o:p> </o:p>
“sheet1” 325 needs to be e-mailed to tom@home.com;jim@home.com;
“sheet2” 326 needs to be e-mailed to mike@work.com;dean@homework.com
“sheet3” 327 needs to be e-mailed to david@worksite.com;richard@mainoffice.com
And so on.
<o:p> </o:p>
Thanks


Code:
Sub Email_Tabs()
'
' Email_Tabs Macro
'
   Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim OutApp As Object
    Dim OutMail As Object
    ActiveSheet.Unprotect
    Set Source = Nothing
    On Error Resume Next
    Set Source = Cells 'Range("A1:J12000").SpecialCells(xlCellTypeVisible)
        
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
    On Error GoTo 0
    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, " & _
               "please correct and try again.", vbOKOnly
        Exit Sub
    End If
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set wb = ActiveWorkbook
    Set Dest = Workbooks.Add(xlWBATWorksheet)
    Source.Copy
    With Dest.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Selection of " & wb.Name & " " _
                 & Format(Now, "dd-mmm-yy h-mm-ss")
    If Val(Application.Version) < 12 Then
        'You use Excel 2000-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2010
        FileExtStr = ".xls": FileFormatNum = -4143
    End If
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = "[EMAIL="Enter_your_to_e-mail_here@e-mail.com"]Enter_your_to_e-mail_here@e-mail.com[/EMAIL]"
            .CC = ""
            .BCC = ""
            .Subject = "Enter_Your_Subject_Here"
            .Body = "Enter the text you want in the body of your e-Mail here.  Thank You."
            .Attachments.Add Dest.FullName
           'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display Send   'or use .Display
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With
    Kill TempFilePath & TempFileName & FileExtStr
    Set OutMail = Nothing
    Set OutApp = Nothing
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
End With
'
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Here's a quick idea for you. I haven't much time so I've not tested it and there's no error checking.

I have used Select case here. It assumes that every sheet in the worksheet is to be sent.
You need to add an email address for each sheet index
Code:
Dim OutMail As Object
For i = 1 To Worksheets.Count
Worksheets(i).Activate
 
Select Case i
 
Case 1
If i = 1 Then
EmailSendTo = "[EMAIL="fred@test.com"]fred@test.com[/EMAIL]"
End If
Case 2
If i = 2 Then
EmailSendTo = "[EMAIL="John@test.com"]John@test.com[/EMAIL]"
End If
Case 3
If i = 3 Then
EmailSendTo = "[EMAIL="Jack@test.com"]Jack@test.com[/EMAIL]"
End If
End Select
 
ActiveSheet.Unprotect
Set Source = Nothing
 
 
--remaining code--
 
End With
Next i
End Sub
 
Upvote 0
Thanks for your reply.

I have been playing with the below code and it seen to be working but I get this popup.

I'm trying to avoid the pop up message window (Allow - Deny - Help) from appearing when I create an e-mail with the below code. The final code will be sending quite a few e-mails and this window appears for every e-mail which requires me to sit and wait for each one to be generated. Any help with this issue would be greatly appreciated.






<TABLE class=tborder id=post2895327 cellSpacing=0 cellPadding=6 width="100%" align=center border=0><TBODY><TR vAlign=top><TD class=alt1 id=td_post_2895327 style="BORDER-RIGHT: #ffffff 1px solid">
Sub one_Tab_2_email()'' one_Tab_2_email Macro'ThisWorkbook.Sheets("811").Copy With ActiveWorkbook .SendMail Array("test@inc.com", "614@yahoo.com"), _ "811 This is the Subject line" .Close SaveChanges:=False End With ThisWorkbook.Sheets("865").Copy With ActiveWorkbook .SendMail Array("test1@inc.com", "614@yahoo.com"), _ " 865 This is the Subject line" .Close SaveChanges:=False End With 'ThisWorkbook.Sheets("865").Copy ThisWorkbook.Sheets(Array("867", "865")).Copy With ActiveWorkbook .SendMail Array("test3@inc.com", "0614@yahoo.com"), _ " 867 & 865 This is the Subject line" .Close SaveChanges:=FalseEnd With'End Sub</PRE>

<!-- / message --></TD></TR><TR><TD class=alt2 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 1px solid; BORDER-BOTTOM: #ffffff 1px solid">
user_online.gif
</TD><TD class=alt1 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 0px solid; BORDER-BOTTOM: #ffffff 1px solid" align=right><!-- controls -->
progress.gif
</TD></TR></TBODY></TABLE></P>
 
Upvote 0
Here is the code thats working but has the popup

Code:
Sub one_Tab_2_email()
'
' one_Tab_2_email Macro
'
ThisWorkbook.Sheets("811").Copy
    With ActiveWorkbook
          .SendMail Array("test[EMAIL="test@inc.com"]@inc.com[/EMAIL]", "[EMAIL="fjm0614@yahoo.com"]614@yahoo.com[/EMAIL]"), _
          "811 This is the Subject line"
        
         .Close SaveChanges:=False
         
End With
                  
         ThisWorkbook.Sheets("865").Copy
            With ActiveWorkbook
          .SendMail Array("test1[EMAIL="test1@inc.com"]@inc.com[/EMAIL]", "[EMAIL="fjm0614@yahoo.com"]614@yahoo.com[/EMAIL]"), _
          " 865 This is the Subject line"
        
         .Close SaveChanges:=False
         
End With
'ThisWorkbook.Sheets("865").Copy

  ThisWorkbook.Sheets(Array("867", "865")).Copy

            With ActiveWorkbook
          .SendMail Array("test3[EMAIL="test3@inc.com"]@inc.com[/EMAIL]", "[EMAIL="fjm0614@yahoo.com"]0614@yahoo.com[/EMAIL]"), _
          " 867 & 865 This is the Subject line"
        
         .Close SaveChanges:=False

End With
'
End Sub
 
Upvote 0
Hi,
have a look at Rons site here:
http://www.rondebruin.nl/mail/prevent.htm

Unfortunately the issue with the Outlook security pop-up is not easy to solve with Xl2003 and various methods are discussed.
If you have Xl2007 then its just a configuration issue.

The use of CDO mail is another option and shown on Rons site.
 
Last edited:
Upvote 0
Thank you for the info.

This issue is really kicking my butt. I have Excel 2007 and I can't seem to get the configuration to allow this code to run without the warning popup. Any advice in layman's terms would be appreeciated.
 
Upvote 0
Last edited:
Upvote 0

Forum statistics

Threads
1,215,916
Messages
6,127,701
Members
449,398
Latest member
m_a_advisoryforall

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