Email attachment code with email?

KainAussie

New Member
Joined
Jun 14, 2012
Messages
9
Hi,

i have a Excel document that i have created and i am trying to add a button so that after pressing the button asks for a email address and it sends the email with the subject "test email" using outlook could some one please help me out with this as i am not sure how to do it and my boss would like this done for his business so its easier to send out the quotes
 
Aargh !!! This code just isn't coming together....I've rewritten it, and hopefully it's a bit simpler to run AND follow.....sorry :oops:
Code:
Sub EmailWithOutlook()
Dim oApp As Object, oMail As Object, WB As Workbook, fileName As String
Application.ScreenUpdating = False
ActiveSheet.Copy
Set WB = ActiveWorkbook
fileName = "Temp.xls"
On Error Resume Next
Kill "C:\" & fileName
On Error GoTo 0
WB.SaveAs fileName:="C:\" & fileName
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
        With oMail
             .To = "someone@somedomain.com" ' change to suit
             .CC = "change to suit"
             .BCC = "" ' change to suit
             .Subject = "Look at my workbook!" ' change to suit
             .Body = "" 'Insert body text if required
             .Attachments.Add WB.FullName
            .Display 'change to .Send if you don't want to chack the E-Mail first
        End With
    WB.ChangeFileAccess Mode:=xlReadOnly
    Kill WB.FullName
    WB.Close SaveChanges:=False
    Application.ScreenUpdating = True
    Set oMail = Nothing
    Set oApp = Nothing
End Sub

do i just use a commandbutton1? because it starts with Sub EmailWithOutlook() if not what do i use
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
If you have a command button, right click on it and select view code.
Paste this between the first and last line in the code window
Code:
Dim oApp As Object, oMail As Object, WB As Workbook, fileName As String
Application.ScreenUpdating = False
ActiveSheet.Copy
Set WB = ActiveWorkbook
fileName = "Temp.xls"
On Error Resume Next
Kill "C:\" & fileName
On Error GoTo 0
WB.SaveAs fileName:="C:\" & fileName
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
        With oMail
             .To = "someone@somedomain.com" ' change to suit
             .CC = "change to suit"
             .BCC = "" ' change to suit
             .Subject = "Look at my workbook!" ' change to suit
             .Body = "" 'Insert body text if required
             .Attachments.Add WB.FullName
            .Display 'change to .Send if you don't want to chack the E-Mail first
        End With
    WB.ChangeFileAccess Mode:=xlReadOnly
    Kill WB.FullName
    WB.Close SaveChanges:=False
    Application.ScreenUpdating = True
    Set oMail = Nothing
    Set oApp = Nothing
 
Upvote 0
If you have a command button, right click on it and select view code.
Paste this between the first and last line in the code window
Code:
Dim oApp As Object, oMail As Object, WB As Workbook, fileName As String
Application.ScreenUpdating = False
ActiveSheet.Copy
Set WB = ActiveWorkbook
fileName = "Temp.xls"
On Error Resume Next
Kill "C:\" & fileName
On Error GoTo 0
WB.SaveAs fileName:="C:\" & fileName
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
        With oMail
             .To = "someone@somedomain.com" ' change to suit
             .CC = "change to suit"
             .BCC = "" ' change to suit
             .Subject = "Look at my workbook!" ' change to suit
             .Body = "" 'Insert body text if required
             .Attachments.Add WB.FullName
            .Display 'change to .Send if you don't want to chack the E-Mail first
        End With
    WB.ChangeFileAccess Mode:=xlReadOnly
    Kill WB.FullName
    WB.Close SaveChanges:=False
    Application.ScreenUpdating = True
    Set oMail = Nothing
    Set oApp = Nothing

im getting a error saying

the following feature cannot be saved in marco free workbooks
vb projects

to save a file with these features click no and choose a macro enabled file type in the file type list the continue saving a marco free workbook click yet

even though the document is a macro enabled document
 
Upvote 0
What version of Excel are you using ?
You may have to change this line
Code:
fileName = "Temp.xls"

TO

fileName = "Temp.xlsm"
 
Upvote 0
What version of Excel are you using ?
You may have to change this line
Code:
fileName = "Temp.xls"

TO

fileName = "Temp.xlsm"

no i am still getting a error telling me it cant be saved marco free document

surely there is a simpler code i can use? as this one is not working
 
Upvote 0
AS requested in the previous post....what version are you using ?
Also, are you trying to run this from an xlsx template. ?

AND finally, please keep to this thread.
Private E-Mails are just that...private !
 
Upvote 0
AS requested in the previous post....what version are you using ?
Also, are you trying to run this from an xlsx template. ?

AND finally, please keep to this thread.
Private E-Mails are just that...private !

office profesional plus 2010
and trying to run it from a .xlsm
 
Upvote 0
Sorry, it works fine for me....and no, I don't have another computer.....they are all govt workstations !
 
Upvote 0
Yep, can if you like ....but I only have 2007 / 03
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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