Send Email only filtered range with attachment

VKMouli

New Member
Joined
Jul 28, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hi,
This code is not working properly, got error messages while running. please , can any one please correct below VBA Code.
Thanking you.

Regards, V K Mouli


VBA Code:
Sub test20221130()
'send multiple emails with spreadsheet attachments with a macro

Dim rng As Range, c As Range, AddrRange As Range
Dim i As Long, lastRow As Long, lastRow2 As Long
Dim targetWorkbook As Workbook
Dim objFSO As Object
Dim varTempFolder As Variant, v As Variant
Dim AttFile As String, Dest As String
Dim sh As Worksheet, shMail As Worksheet

Set sh = Sheets("Master")
Set shMail = Sheets("Mail info")

lastRow = sh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lastRow3 = shMail.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set AddrRange = shMail.Range("A1:C" & lastRow3)

v = sh.Range("A2:Z" & lastRow).Value '<<==== Change last column is needed

Set objFSO = CreateObject("Scripting.FileSystemObject")
varTempFolder = objFSO.GetSpecialFolder(2).Path & "\Temp " & Format(Now, "dd-mm-yyyy- hh-mm-ss")
objFSO.CreateFolder (varTempFolder)

Application.ScreenUpdating = False

With CreateObject("scripting.dictionary")

For i = 2 To UBound(v)
If Not .exists(v(i, 1)) Then
.Add v(i, 1), Nothing

With sh
sh.Range("A1").AutoFilter 1, v(i, 1)
Set rng = .AutoFilter.Range
Set targetWorkbook = Workbooks.Add
.UsedRange.SpecialCells(xlCellTypeVisible).Copy targetWorkbook.Worksheets(Sheets.Count).Range("A1")
AttFile = v(i, 1) & Format(Now, "dd-mm-yyyy- hh-mm-ss") & ".xlsx"
Dest = Application.WorksheetFunction.VLookup(v(i, 1), AddrRange, 2, False)
Dest1 = Application.WorksheetFunction.VLookup(v(i, 1), AddrRange, 3, False)

With targetWorkbook
.ActiveSheet.Columns.AutoFit
.SaveAs varTempFolder & "\" & AttFile
.Close
End With

With CreateObject("Outlook.Application").CreateItem(0)
.To = Dest
.Cc = Dest1
.subject = "Subject"
.Body = "Please find..."
.Attachments.Add varTempFolder & "\" & AttFile
.display 'to show
'.Send 'to send
End With

End With
End If

Next i
End With

sh.Range("A1").AutoFilter
Application.ScreenUpdating = True

objFSO.deletefolder (varTempFolder)

'kill temp file
Kill TempFilePath & TempFileName & FileExtStr

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,215,243
Messages
6,123,837
Members
449,129
Latest member
krishnamadison

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