Attaching a Spreadsheet

seasmith

New Member
Joined
Jul 6, 2011
Messages
44
I have code (see below) to send an email automatically if a button is pressed at it has been 30 days since the last time is was pressed. I am trying to attach the spreadsheet to the email but I am having no luck.

This is the code I'm using:

Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
x = Date

If x - Cells(3, 4) > 30 Then

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "A relief valve needs to be tested. Please look at the attached spreadsheet to see which pressure relief it is."
On Error Resume Next
With OutMail
.To = email@email.com
.CC = ""
.BCC = ""
.Subject = "Pressure Relief Valve"
.Body = strbody
.Send 'Sends Email"
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

Cells(3, 4) = x

End If
End Sub

I've tried using .Attachments.Add but no luck
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi,

You don't show the attachment code you tried.

If it's not attaching using Attachments.Add it's probably because you haven't specified the path.

.FullName includes the path to the file.

Code:
.Attachments.Add ThisWorkbook.FullName
 
or 
 
.Attachments.Add "C\junk\junk.xls"
 
or
 
AttachFileName = Application.GetOpenFilename("Files (*.**)," & _
"*.**", 1, "Select File", "Open", False)
.Attachments.Add (AttachFileName)
 
Upvote 0
Thanks so much the .Attachments.Add ThisWorkbook.FullName worked great!
I just couldn't figure out what to put after the .Attachments.Add in the code. Thanks again.
 
Upvote 0
Hi,
Try the below code. .Attachment.Add does not work. You need to use the syntax as .AddAttachment "File Name"

With OutMail
.Subject = "Test Mail from LearnExcelMacro.com"
.From = "vishwamitra01@yahoo.com"
.To = "vishwamitra02@gmail.com;info@learnexcelmacro.com"
.CC = "vishwamitra01@gmail.com"
.BCC = ""
.HTMLBody = "Write your complete HTML Page"

' For multiple Attachment you can add below lines as many times

.AddAttachment "C:\ExcelMacro-help.xls"
.AddAttachment "C:\ExcelMacro-help2.xls"
End With


---

To know more in detail.. you can visit this link:

http://www.learnexcelmacro.com/2011/12/how-to-send-an-email-using-excel-macro-from-gmail-or-yahoo


http://www.learnexcelmacro.com/2011/12/how-to-send-email-by-excel-macro-from-outlook/
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,707
Members
452,939
Latest member
WCrawford

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