How to email with filename using a macro

vane0326

Well-known Member
Joined
Aug 29, 2004
Messages
819
I have a problem this macro works perfectly when you email to someone BUT when it emails in the Subject bar it does not pick up the name of the workbook. You know how when open your workbook then you go to FILE then SEND TO then you choose MAIL RECIPIENT (AS ATTACHMENT) Then that box pops up and in the subject bar the name of the file is in there. This code does not put the name of the file in the SUBJECT bar. Any one has a idea to modified this code?

Thanks!
Public Sub MyOutlook()

Dim AppOutlook As Object
Set AppOutlook = CreateObject("Outlook.application")

With AppOutlook.CreateItem(olMailItem)
.To = "noone@home.com"


.Subject = "Sheet: " & ws.Name



'Populate your attachment the way you like as shown at the http://www.rondebruin.nl/mail/folder2/mail1.htm web site.
'just one example is shown below.

.AddAttachment WBname


.Send
End With

Set AppOutlook = Nothing
End Sub
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
If you want the path and the name of the workbook, use:
Code:
.Subject = "Sheet: " & ActiveWorkbook.FullName

If you only need the filename, this should work:
Code:
.Subject = "Sheet: " & ActiveWorkbook.Name
Hope that helps!
 

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
What caught you out, was the ws.Name reference.
You hadn't declared the variable or given it a value, so the code mutely inserted a null value for you.
I always use Option Explicit to get around this issue

Denis
 

vane0326

Well-known Member
Joined
Aug 29, 2004
Messages
819
Thank You for responding and I changed the code just a little bit. This is the code I was looking for . Thanks so much!


.Subject = ActiveWorkbook.Name
 

Watch MrExcel Video

Forum statistics

Threads
1,123,143
Messages
5,599,975
Members
414,354
Latest member
Flaxarn

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
Top