below is the updated macro that is NOT attaching the file. CAN SOMEONE PLS ASSIST?
Sub mailer()
Dim ws_list As Worksheet
Dim ws_mail As Worksheet
Dim newbook As Workbook
Dim i As Long
Dim lr As Long
Dim lr2 As Long
Dim fname As String
Dim recipientName As String
Dim my_message As String
' Define the HTML email message
my_message = "<!DOCTYPE html>" & _
"<html>" & _
"<head>" & _
"<style>" & _
"body { font-family: 'Aptos', sans-serif; font-size: 10pt; }" & _
"</style>" & _
"</head>" & _
"<body>" & _
"<p>Hi [person],</p>" & _
"<p>Hope you are well.</p>" & _
"<p>I am reaching out to gain a better understanding of how our xx xx and handles the charge-out of xx for fixed/personnel costs. Specifically, I would like to discuss the following points:</p>" & _
"<ul>" & _
"<li>The process for xx by individuals and product lines.</li>" & _
"<li>How the percentage xx are determined and assigned.</li>" & _
"<li>Any key considerations or methodologies used in this allocation process.</li>" & _
"</ul>" & _
"<p>In the attached workbook, if you can kindly assign the % the respective xx will be working in the corresponding xx (columns W:Z).</p>" & _
"<p>For context, I am currently working with xx from the xx Division, and this inquiry is specifically focused on the allocations of fixed/personnel costs within the xx Division. As part of our ongoing efforts to realign our xx, it is crucial that we fully understand the mechanics behind these allocations. Your expertise and insights would be greatly appreciated.</p>" & _
"<p>Could we schedule a meeting at your earliest convenience to discuss this in detail? I am looking forward to your response and am available for a meeting at a time that works best for you.</p>" & _
"<p>Thank you so much for your cooperation.</p>" & _
"</body>" & _
"</html>"
' Set the worksheet references
Set ws_list = ThisWorkbook.Worksheets("Email list")
Set ws_mail = ThisWorkbook.Worksheets("Summary")
' Get the last row of email list data
lr = ws_list.Range("C" & Rows.Count).End(xlUp).Row
' Exit if there are no names listed
If lr < 11 Then Exit Sub
For i = 11 To lr
' Get the recipient's name from column C
recipientName = ws_list.Range("C" & i).Value
' Sanitize the recipientName to remove any invalid characters
recipientName = Replace(recipientName, "/", "_")
recipientName = Replace(recipientName, "\", "_")
recipientName = Replace(recipientName, ":", "_")
recipientName = Replace(recipientName, "*", "_")
recipientName = Replace(recipientName, "?", "_")
recipientName = Replace(recipientName, """", "_")
recipientName = Replace(recipientName, "<", "_")
recipientName = Replace(recipientName, ">", "_")
recipientName = Replace(recipientName, "|", "_")
' Construct the file name with the recipient's name
fname = "C:\Users\john.smith\xx\Desktop\Test\Consolidated IES_" & recipientName & ".xlsx"
' Copy the Summary sheet to a new workbook
ws_mail.Copy
' The copied sheet will now be the active workbook
Set newbook = ActiveWorkbook
' Convert columns C:AE to static values
lr2 = newbook.Sheets(1).Range("C" & Rows.Count).End(xlUp).Row
newbook.Sheets(1).Range("C11:AE" & lr2).Value = newbook.Sheets(1).Range("C11:AE" & lr2).Value
' Turn off any filters
newbook.Sheets(1).AutoFilterMode = False
' Filter by column Q to match names in the Email list (Column C)
newbook.Sheets(1).Range("C10:BF" & lr2).AutoFilter field:=15, Criteria1:="<>*" & recipientName & "*"
' Delete rows where names don't match
On Error Resume Next
newbook.Sheets(1).Range("Q11:Q" & lr2).SpecialCells(xlCellTypeVisible).EntireRow.Delete
On Error GoTo 0
newbook.Sheets(1).AutoFilterMode = False
' Exit loop if no data is available for this user
If newbook.Sheets(1).Range("Q11").Value = vbNullString Then
newbook.Close SaveChanges:=False
GoTo end_loop
End If
' Hide unnecessary columns
With newbook.Sheets(1)
.Columns("F").Hidden = True
.Columns("K:M").Hidden = True
.Columns("P:U").Hidden = True
.Columns("AL:BF").Hidden = True
.Rows("5:7").ClearContents
End With
' Set the active cell to A1 and scroll to row 11
With newbook.Sheets(1)
.Activate
.Range("A11").Select
ActiveWindow.ScrollRow = 11
.Range("A1").Select
End With
' Save the new workbook with the personalized file name
Application.DisplayAlerts = False
Debug.Print "Saving file as: " & fname ' Debug message for tracking
newbook.SaveAs fname
Application.DisplayAlerts = True
' Confirm the file exists before proceeding to attach
If Dir(fname) <> "" Then
' Call the emailer function to send email with the attachment
Call emailer(ws_list.Range("D" & i).Value, recipientName, fname)
Else
MsgBox "File not saved properly: " & fname, vbExclamation
newbook.Close SaveChanges:=False
GoTo end_loop
End If
' Close the temporary workbook
newbook.Close SaveChanges:=False
' Delete the file after sending the email
Debug.Print "Deleting file: " & fname ' Debug message for tracking
Kill fname
end_loop:
' Continue to the next iteration
Next i
End Sub
Sub emailer(email As String, user As String, fname As String)
Dim olook As Object
Dim omail As Object
Dim my_message As String
Dim my_sig As Variant
' HTML content
my_message = "<!DOCTYPE html>" & _
"<html>" & _
"<head>" & _
"<style>" & _
"body { font-family: 'Aptos', sans-serif; font-size: 10pt; }" & _
"</style>" & _
"</head>" & _
"<body>" & _
"<p>Hi " & user & ",</p>" & _
"<p>Hope you are well.</p>" & _
"<p>I am reaching out to gain a better understanding of how our xx and handles the charge-out of xx for fixed/personnel costs. Specifically, I would like to discuss the following points:</p>" & _
"<ul>" & _
"<li>The process for xx by individuals and product lines.</li>" & _
"<li>How the percentage allocations are determined and assigned.</li>" & _
"<li>Any key considerations or methodologies used in this allocation process.</li>" & _
"</ul>" & _
"<p>In the attached workbook, if you can kindly assign the % the respective employee will be working in the corresponding division (columns W:Z).</p>" & _
"<p>For context, I am currently working with xx from the xx Division, and this inquiry is specifically focused on the allocations of fixed/personnel costs within the xx Division. As part of our ongoing efforts to realign our xx, it is crucial that we fully understand the mechanics behind these allocations. Your expertise and insights would be greatly appreciated.</p>" & _
"<p>Could we schedule a meeting at your earliest convenience to discuss this in detail? I am looking forward to your response and am available for a meeting at a time that works best for you.</p>" & _
"<p>Thank you so much for your cooperation.</p>" & _
"</body>" & _
"</html>"
' Create Outlook application and email
Set olook = CreateObject("Outlook.Application")
Set omail = olook.CreateItem(0)
omail.Display
my_sig = omail.HTMLBody
With omail
.To = email
.Subject = "Input required: xx and recharge %"
.HTMLBody = my_message & my_sig
' Debugging: Print the file path to verify
Debug.Print "Attaching file: " & fname
' Attach the file to the email
On Error Resume Next
.Attachments.Add fname
If Err.Number <> 0 Then
MsgBox "Error attaching file: " & fname, vbExclamation
Debug.Print "Error attaching file: " & Err.Description
Err.Clear
End If
On Error GoTo 0
' Display the email for review
'.Send ' Uncomment this line to send automatically
End With
End Sub
Bookmarks