简体   繁体   中英

Sending multiple worksheets in same workbook via VBA macro through Outlook Email?

I would like to copy multiple worksheets (for example, Sheet71, Sheet76, Sheet60, and Sheet77) that are located within one workbook into another workbook to send in an email to a recipient that is outlined within my email key sheet on Sheet 71.

These emails will be sent to individuals to outline their bonus pay.

Therefore, it is critical that the recipients only receive their own or who they are responsible for.

I have figured out how to send one single worksheet to one recipient, but cannot figure out how to accomplish this with multiple worksheets without using the name on the worksheet (Pierce Group Matrix, Shuff Matrix, Gamble Matrix, and Reed Matrix) versus Sheet71, Sheet76, Sheet60, and Sheet77 in VBA.

I need to be able to reference within the macro to the sheet number rather than the name, because turnover does happen.

Below is the code that I wrote to send an email to one individual in my email key sheet (Sheet81) with one worksheet but it only sends Sheet 71.

I have tried the Array keyword and multiple other keywords but can't seem to get it to work.

I need to reference to the Sheet number rather than the Sheet name because the names are changed when people are replaced.

I would prefer to make a copy like the below code does, but I am open to try a Select command if that will work.

Sub Mail()

Dim OutlookApp As Object
Dim Mess As Object, Recip
Recip = Sheet81.[C35].Value
newDate = MonthName(Month(DateAdd("m", -1, Date)), False)

 ' Make a copy of the active worksheet
' and save it to a temporary file
Sheet71.Copy
Set WB = ActiveWorkbook

Filename = WB.Worksheets(1).Name
On Error Resume Next
Kill "C:\" & Filename
On Error GoTo 0
WB.SaveAs Filename:="C:\" & Filename

Set OutlookApp = CreateObject("Outlook.Application")
Set Mess = OutlookApp.CreateItem(olMailItem)
With Mess
.Subject = (newDate + " Matrix")
.Body = ("Attached is your " + newDate + " bonus matrix.  Thanks! Neil")
.to = Recip
.Attachments.Add WB.FullName
.Display
.Send
End With
ActiveWorkbook.Close

Set OutlookApp = Nothing
Set Mess = Nothing
End Sub

In this method, I elected to create a new sub routine called sendMultMails . This will create a collection of worksheets that you choose to add. Since you do not want to use the sheet name as the reference, I used the sheet's CodeName .

So, add your sheets to the collection and loop that collection. Within the loop, you will call your other routine Mail , passing the sheet as a parameter.

Sub sendMultMails()

    Dim wsColl As New Collection, ws As Worksheet

    Rem: Add your worksheets to the collection via the worksheet's CodeName
    With wsColl
        .Add Sheet71
        .Add Sheet76
        .Add Sheet60
        .Add Sheet77
    End With

    Rem: loop through each collection item, calling the Mail Routine
    For Each ws In wsColl
        Mail ws
    Next

End Sub

Rem: Added an argument for you to pass the ws obj to this routine
Sub Mail(ws As Worksheet)

    Dim OutlookApp As Object
    Dim Mess As Object, Recip
    Recip = ws.Range("C35").Value
    newDate = MonthName(Month(DateAdd("m", -1, Date)), False)

     ' Make a copy of the active worksheet
    ' and save it to a temporary file
    ws.Copy
    Set WB = ActiveWorkbook

    Filename = WB.Worksheets(1).Name
    On Error Resume Next
    Kill "C:\" & Filename
    On Error GoTo 0
    WB.SaveAs Filename:="C:\" & Filename

    Set OutlookApp = CreateObject("Outlook.Application")
    Set Mess = OutlookApp.CreateItem(olMailItem)
    With Mess
    .Subject = (newDate + " Matrix")
    .Body = ("Attached is your " + newDate + " bonus matrix.  Thanks! Neil")
    .to = Recip
    .Attachments.Add WB.FullName
    .Display
    .Send
    End With
    ActiveWorkbook.Close

    Set OutlookApp = Nothing
    Set Mess = Nothing

End Sub

You could use the WB.Worksheets(1).CodeName to reference the Sheet number.

the CodeName property is read-only You can reference a particular sheet as Worksheets("Fred").Range("A1") where Fred is the .Name property or as Sheet1.Range("A1") where Sheet1 is the codename of the worksheet.

For more information, you could refer to this link:

Excel tab sheet names vs. Visual Basic sheet names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM