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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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!
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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