Range to a txt file attachment

Masha92

Board Regular
Joined
Jan 27, 2019
Messages
51
Office Version
  1. 365
Hi all,

I found some articles over the internet addressing similar problems like mine but for some reason couldnt tweak it to my needs. So appreciate if you can help me with it.

I have a range in a tab that i want to attach in outlook email as a text file.

Lets assume my range is
code: Sht1.range("A2:D"&sht1.cells(cells.rows.count,"D").end(xlup).row

The txt file should be called
Code: Date & "_Error Log"

Can you please help me with it?

Thanks,
Masha
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Let's start with this

Change data in red by your information.

Code:
Sub Macro3()
    Dim sh As Worksheet, sh2 As Worksheet
    Dim l2 As Workbook, wFile As String, wPath As String
    Dim dam
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Set sh = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
    Set l2 = Workbooks.Add
    Set sh2 = l2.Sheets(1)
    sh.Range("[COLOR=#ff0000]A2:D" & sh.Range("D"[/COLOR] & Rows.Count).End(xlUp).Row).Copy
    sh2.Range("A1").PasteSpecial xlPasteAll
    
    wPath = ThisWorkbook.Path & "\"
    wFile = Format(Date, "[COLOR=#ff0000]mm-dd-yyyy[/COLOR]") & "_Error Log.txt"
    
    l2.SaveAs Filename:=wPath & wFile, FileFormat:=xlTextPrinter, CreateBackup:=False
    l2.Close False
    
    Set dam = CreateObject("Outlook.Application").CreateItem(0)
    dam.To = "[COLOR=#ff0000]mail@gmail.com[/COLOR]"
    dam.Subject = "[COLOR=#ff0000]Subject[/COLOR]"
    dam.Body = "[COLOR=#ff0000]Body[/COLOR]"
    dam.Attachments.Add wPath & wFile
    dam.Send                                'send
    'dam.Display                             'show
    MsgBox "Sent"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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