Results 1 to 2 of 2

Macro to create an email from sheets BR1 to Last sheet

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,765

    Macro to create an email from sheets BR1 to Last sheet

    I need someone to amend my VBA Code to generate an email for sheets Br1 to last sheet. My sample data has only 3 sheets to be generated in Outlook , but by live data has 20 sheets


    The Subject is named "subjectText1" on sheet "Email Branches" and Body is named "Bodytext1" I need a seperate email for each sheet from "Br1" to the last sheet. The email addresses are in AA1 to AA5 on each of these sheets. The email must only be created for each sheet where the average value in Col E2:E3 does exceed 60


    I get Method or data member not found


    It would be appreciated if someone could kindly check and amend my code


    OutlookMail.Attachments.Add AttachedSheet.FullName

     Sub GenerateEmails()
        Dim OutlookApp As Object
        Dim OutlookMail As Object
        Dim ws As Worksheet
        Dim rngEmail As Range
        Dim avgDays As Variant
        Dim Ztext As String
        Dim Zsubject As String
        Dim AttachedSheet As Worksheet
        Dim sheetCounter As Integer
        
        ' Create Outlook application
        Set OutlookApp = CreateObject("Outlook.Application")
        
        ' Initialize sheet counter
        sheetCounter = 1
        
        
        For Each ws In ThisWorkbook.Sheets
            If ws.Name >= "BR1" 
                
                ' Check average value in Col E2:E3
                On Error Resume Next
                avgDays = Application.WorksheetFunction.Average(ws.Range("E2:E3"))
                On Error GoTo 0
                
                If IsNumeric(avgDays) And avgDays > 60 Then ' Only proceed if average exceeds 60 days
                    ' Get email addresses from AA1 to AA5 on the current sheet
                    Set rngEmail = ws.Range("AA1:AA5")
                    
                    ' Set subject and body text using a more direct approach
                    Zsubject = ThisWorkbook.Sheets("Email Branches").Range("SubjectText1").Value
                    Ztext = ThisWorkbook.Sheets("Email Branches").Range("BodyText1").Value
                    
                    ' Loop through each email address and create an email
                    For Each cell In rngEmail
                        If cell.Value <> "" Then ' Check if the cell is not empty
                            ' Create a new email
                            Set OutlookMail = OutlookApp.CreateItem(0)
                            
                            ' Set email properties using additional variables
                            OutlookMail.Subject = Zsubject
                            OutlookMail.Body = Ztext
                            OutlookMail.To = cell.Value
                            
                            ' Attach the current sheet with a unique name
                            ws.Copy Before:=Sheets(1)
                            Set AttachedSheet = Sheets(1)
                            AttachedSheet.Name = "AttachmentSheet" & sheetCounter
                            sheetCounter = sheetCounter + 1
                            
                            ' Attach the copied sheet to the email
                            OutlookMail.Attachments.Add AttachedSheet.FullName
                            
                            ' Display the email (you can remove or replace this line if you want to send without displaying)
                            OutlookMail.Display
                            
                            ' Release the email object
                            Set OutlookMail = Nothing
                            ' Delete the temporary copied sheet
                            Application.DisplayAlerts = False
                            AttachedSheet.Delete
                            Application.DisplayAlerts = True
                        End If
                    Next cell
                End If
            End If
        Next ws
        
        ' Release the Outlook application object
        Set OutlookApp = Nothing
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to create an email, attach current workbook and also paste a range from sheet
    By StormFusion in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-12-2019, 08:32 AM
  2. [SOLVED] Macro to create 3 Summary Sheets from a Data Sheet
    By rehana402003 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-22-2019, 06:27 AM
  3. Macro To Send Individual sheets To Different Email Address Based On Sheet Name
    By markusvirus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-23-2016, 05:29 PM
  4. [SOLVED] Macro for searching on sheet 1 with data from sheet 2 and create new sheets
    By stitchoz in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 03-03-2014, 12:25 PM
  5. How to create individual sheets from one sheet and email them individually
    By Shoju in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-21-2013, 11:18 AM
  6. Macro to create email with refrence taken from excel sheet
    By sameeru in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2013, 08:37 AM
  7. Problems creating a macro to create a single email from each row of a sheet
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-17-2009, 03:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1