Send a sheet from a workbook as a CSV file as an email attachment

Godot60

Board Regular
Joined
May 11, 2017
Messages
62
Hi,

I have a workbook containing multiple sheets. I have figured out a way to send the entire workbook as an email attachment in Outlook. it works great.

What I need to do now is send just one sheet named ICIMS from the workbook as a CSV file attachment. Any ideas?

Here's what I have so far:

Sub Mail_small_Text_Outlook2()


If Range("c157").Value = "" Or Range("c159").Value = "" Then
MsgBox "Please complete all required fields"
Exit Sub
End If




Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Title: " & Sheet1.Range("A4") & vbNewLine & _
"Department: " & Sheet1.Range("C4") & vbNewLine & _
"SBU: " & Sheet1.Range("B5") & vbNewLine & _
"Level: " & Sheet1.Range("B6") & vbNewLine & _
"Manager: " & Sheet1.Range("B145")


On Error Resume Next
With OutMail
.To = "name.lastname@institution.edu"
.cc = ""
.BCC = ""
.Subject = "Successful Submission: " & Sheet1.Range("a4")
.Body = strbody
.Attachments.Add ActiveWorkbook.FullName
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Incorporate this to save as CSV:
Code:
    Dim CSVfileName As String
    
    CSVfileName = ActiveWorkbook.Path & "\ICIMS.csv"
    
    ActiveWorkbook.Worksheets("ICIMS").Copy
    ActiveWorkbook.SaveAs CSVfileName, FileFormat:=xlCSV
    ActiveWorkbook.Close False
and
Code:
.Attachments.Add CSVfileName
 
Upvote 0
You want to attach the .csv file to the email, so logically the 'save as CSV' code should be put before the email is created - before the Set OutApp line. And my Attachments.Add line replaces your Attachments.Add line, so it attaches the .csv file instead of the workbook.
 
Upvote 0
You want to attach the .csv file to the email, so logically the 'save as CSV' code should be put before the email is created - before the Set OutApp line. And my Attachments.Add line replaces your Attachments.Add line, so it attaches the .csv file instead of the workbook.
Hi John,

How would you only convert a range to be copy to .csv file. I have:

ActiveWorkbook.Worksheets("Order Report").Range("A1:D25").Copy
 
Upvote 0
How would you only convert a range to be copy to .csv file. I have:

ActiveWorkbook.Worksheets("Order Report").Range("A1:D25").Copy
Try changing this code to suit your sheet name and range:

If it doesn't work please start your own thread because your request to save a range as a .csv file is different to the OP's request to save a sheet as a .csv file.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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