VBA mailing from Excel with outlook

Rasmussen

New Member
Joined
Jun 10, 2019
Messages
24
Hello,

I found this VBA online, but I have some troubles with it.

When mailing worksheet, there is no problems, but the received file is empty, like there is no data in the sheets. Can someone help me with this matter?

VBA Code:
Sub Mail()

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next

    With OutMail
        .To = "mymail@domain.com"
        .CC = ""
        .BCC = ""
        .Subject = Range("J16").Value & " TOLDBEREGNING"
        .body = ""
        .Attachments.Add ActiveWorkbook.FullName
        .send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 

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.
Your code seems fine. Note, though, it's the active workbook that is being attached to the email. If the target workbook is not the active workbook, you'll need to explicitly state the name of the workbook you want to attach, for example...

VBA Code:
.Attachments.Add Workbooks("sample.xlsx").FullName
 
Upvote 0
Hello,

Even with this code replacing activesheet, the data just disappear.

The file I receive on mail is called sheet.xls, but when opening the workbook, it's named sheet (3).xls.

Can this have anything to do with the fact that the workbook is empty?
 
Upvote 0
Is this the complete and exact code that you're using? If not, can you post the complete and exact code?
 
Upvote 0
So I just noticed that it doesn't attach a file, if the workbook hasn't been saved on my PC.

Do you know a workaround for this?
 
Upvote 0
If you're not saving the workbook because it's only a temporary workbook, then you can simply temporarily save it, mail it, and then delete it. Have a look at the following link...

Mail one sheet

Also, here are other links dealing with the same sort of thing...

Mail from Excel with Outlook (VBA)
 
Upvote 0
Thanks for your input.

Do you know if it's possible to attach more than just the ActiveWorkbook? Like 2 sheets instead?
 
Upvote 0
Simply repeat .Attachments.Add as many times as you need. For example, to attach two workbooks...

VBA Code:
    With OutMail
        .To = "mymail@domain.com"
        .CC = ""
        .BCC = ""
        .Subject = Range("J16").Value & " TOLDBEREGNING"
        .body = ""
        .Attachments.Add ActiveWorkbook.FullName
        .Attachments.Add Workbooks("Book2.xlsx").FullName
        .send
    End With

Alternatively, you can use With/End With to manipulate the Attachments object...

VBA Code:
    With OutMail
        .To = "mymail@domain.com"
        .CC = ""
        .BCC = ""
        .Subject = Range("J16").Value & " TOLDBEREGNING"
        .body = ""
        With .Attachments
            .Add ActiveWorkbook.FullName
            .Add Workbooks("Book2.xlsx").FullName
        End With
        .send
    End With

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,324
Messages
6,124,250
Members
449,149
Latest member
mwdbActuary

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