# e-mailing with Excel macro

**URL:** <https://boards.straightdope.com/t/e-mailing-with-excel-macro/267124>\
**Category:** Factual Questions\
**Created:** [October 2, 2004, 12:38am UTC](https://boards.straightdope.com/t/e-mailing-with-excel-macro/267124 "2004-10-02T00:38:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![batsto](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@batsto](https://boards.straightdope.com/u/batsto)\
**Post date:** [October 2, 2004, 12:38am UTC](https://boards.straightdope.com/t/e-mailing-with-excel-macro/267124/1 "2004-10-02T00:38:00Z")

</div>

I’m trying to use a macro that automatically e-mails the file. Problem is the file won’t always have the same name, but I found this code on the internet that claims it uses the filename of the current ActiveWorkbook. I however get an error every time I try it this way. Code looks like this:  
Dim myOutlook As Object  
Dim myMailItem As Object  
Set otlApp = CreateObject(“Outlook.Application”)  
Set otlNewMail = otlApp.CreateItem(olMailItem)  
fName = ActiveWorkbook.Path & “” & ActiveWorkbook.Name

With otlNewMail  
.To = "[Brian\_Budd@cable.comcast.com](mailto:Brian_Budd@cable.comcast.com); [klopeks@comcast.net](mailto:klopeks@comcast.net) "

.Subject = “Pending Disco Report”  
.Body = “Attached is today’s Report.” & Chr(13) & “Regards,” & Chr(13) & “Ben” & Chr(13) & Chr(13)  
.Attachments.Add fName  
.Send  
End With

otlApp.Quit

Set otlNewMail = Nothing  
Set otlApp = Nothing  
Set otlAttach = Nothing  
Set otlMess = Nothing  
Set otlNSpace = Nothing

the problem is, both of these lines are giving me an error:

fName = ActiveWorkbook.Path & “” & ActiveWorkbook.Name

and  
.Attachments.Add fName

any one have any idea what the problem is here? How do I tell it to attach the active workbook if I don’t know what the name is always going to be?

---

<div class="post-metadata">

**Author:** ![K364](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/k364/32/5_2.png) [@K364](https://boards.straightdope.com/u/K364)\
**Post date:** [October 3, 2004, 5:07pm UTC](https://boards.straightdope.com/t/e-mailing-with-excel-macro/267124/2 "2004-10-03T17:07:50Z")

</div>

What is the error message?

To start with, ActiveWorkbook is a property of the Application object, so your syntax could be “Application.ActiveWorkbook.Name”. But the Application object should be the default object, so your syntax _should_ be ok.

Check that you have the correct libraries in your references - in the VBA editor, goto Tools/References and check that you have “Microsoft Excel 9.0 Object Library” (for Excel 2000, 8.0 for Excel 97). This library is where the Application object “lives”.
