Excel Dynamic filter Data Email through outlook using VBA

VKMouli

New Member
Joined
Jul 28, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Dear Experts,
I am unable to attach filtered ledger as attachment for the respective employee but this code does filtered table & text inserted in outlook.
but i am looking attachment too. please suggest which parameter is missing on this.

Ledger Statement for the month of November-22Total outstanding7,59,890INR21-Nov-22
VendorDocument NumberReferenceDocument DateNet due dateAmount in local currencyLocal CurrencyTextNameEmail id (To)Email id (Cc)
60032444530000341CITI OTHERS09-08-202209-08-2022-8,000INRUPI CREDIT REFERENCE 222132718871PrasadPrasad@gmail.comKumar@gmail.com
60032444530000418CITI OTHERS09-09-202209-09-2022-8,000INRUPI CREDIT REFERENCE 225275999048PrasadPrasad@gmail.comKumar@gmail.com
60032444530000431CITI OTHERS14-10-202214-10-2022-8,000INRUPI CREDIT REFERENCE 228705675272PrasadPrasad@gmail.comKumar@gmail.com
60032444520000016CITI-NEFT05-04-202105-04-202152,500INRNEFT OUT UTR CITIN21152895522PrasadPrasad@gmail.comKumar@gmail.com
60032444520000518CITI-NEFT29-11-202129-11-20211,82,400INRNEFT OUT UTR CITIN21278028055PrasadPrasad@gmail.comKumar@gmail.com
60032474530000232CITI-NEFT27-07-202227-07-20223,57,610INR6003247 NEFT OUT UTR CITIN22310371351 TRF TO B VSSSuryaSurya@gmail.comKumar@gmail.com
60032804530000324CITI-NEFT10-08-202210-08-20225,000INRNEFT OUT UTR S N V K SURESH BONAGIRI ADVANCESureshSuresh@gmail.comKumar@gmail.com
60069114530000231CITI-NEFT21-07-202221-07-202218,000INR6006911 NEFT OUT UTR CITIN22308001066 TRF TO VENKAKiranKiran@gmail.comKumar@gmail.com
60069114530000231CITI-NEFT21-07-202221-07-202268,000INR6006911 NEFT OUT UTR CITIN22307005986 TRF TO VENKAKiranKiran@gmail.comKumar@gmail.com
60069114530000338CITI-NEFT17-08-202217-08-20221,00,380INRNEFT OUT UTR CITIN22321398240 TRF TO MULTIMONEY FOKiranKiran@gmail.comKumar@gmail.com



Here is my Code below.

VBA Code:
Sub CreateEmails()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, rng As Range, i As Long, v As Variant
    v = Range("A2").CurrentRegion.Value
    Set OutApp = CreateObject("Outlook.Application")
    With CreateObject("scripting.dictionary")
        For i = 3 To UBound(v)
            If Not .exists(v(i, 9)) Then
                .Add v(i, 9), Nothing
                With ActiveSheet
                    .Range("A2").AutoFilter 9, v(i, 9)
                    Set rng = .Range("A1", .Range("I" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
                    Set OutMail = OutApp.CreateItem(0)
                    With OutMail
                        .To = v(i, 10)
                        .Cc = v(i, 11)
                        .Subject = "Outstanding Advance-" & Format(Now, "MMMM-YY")
                        .HTMLBody = "Dear Sir, Please find outstanding advance details below. We request you to review & repay asap. This is a Monthly reminder for your infomration & action purposes only. Regards, Finance Dept." & "<br>" & RangetoHTML(rng)
                        .Display
                    End With
                End With
            End If
        Next i
    End With
    Range("A2").AutoFilter
    ActiveCell.Columns("A:I").EntireColumn.Select
    ActiveCell.Columns("A:I").AutoFit
    ActiveCell.Rows("1:10000").EntireRow.AutoFit
    Application.ScreenUpdating = True
End Sub

Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
         .Publish (True)
    End With
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", "align=left x:publishsource=")
    TempWB.Close savechanges:=False
    'Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,215,563
Messages
6,125,560
Members
449,237
Latest member
Chase S

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