You are here:

Excel/CBA code to pick up a file and email it automatically

Advertisement


Question
I am trying to write a code for VBA to pick up a file each day and e-mail it automatically. The file has a constant name with the unique identifier being the created date in yyyymmdd format.
I have written the below code which is creating and sending the e-mail but not attaching the item
Sub Mail_Workbook_1()
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010#
' This example sends the last saved version of the Activeworkbook object #
   Dim OutApp          As Object
   Dim OutMail         As Object
   Dim Filename        As Variant
   Dim Path          As String
   Dim Todaysdate      As String
   Todaysdate = Format(Date, "yyyymmdd")
 
   
  
  Path = "C:DESKTOP"
  FileName = "DAILY_REPORT" & "_" & Todaysdate

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

   On Error Resume Next
  ' Change the mail address and subject in the macro before you run it.
   With OutMail
       .To = ""
       .CC = ""
       .BCC = ""
       .Subject = "DAILY_REPORT"
       .Body = "Good Morning, Please find attached the daily report"
       .attachments.Add Path & FileName
       .Send
   End With
   On Error GoTo 0

   Set OutMail = Nothing
   Set OutApp = Nothing
End Sub

Are you able to assist?
Thanks in advnace
Natasha

Answer

The problem is with the file address
The correct code is:


  Sub SendMail()
  Dim OutApp          As Object
  Dim OutMail         As Object
  Dim Filename        As Variant
  Dim Path          As String
  Dim Todaysdate      As String
  Todaysdate = Format(Date, "yyyymmdd")

  

 Path = "C:\DESKTOP"
 Filename = "DAILY_REPORT" & "_" & Todaysdate & ".docx"
 mydoc = Path & "\" & Filename
  Set OutApp = CreateObject("Outlook.Application")
  Set OutMail = OutApp.CREATEITEM(0)

  On Error Resume Next
 ' Change the mail address and subject in the macro before you run it.
  With OutMail
      .To = ""
      .CC = ""
      .BCC = ""
      .Subject = "DAILY_REPORT"
      .Body = "Good Morning, Please find attached the daily report"
      .attachments.Add mydoc
      .Send
  End With
  On Error GoTo 0

  Set OutMail = Nothing
  Set OutApp = Nothing
End Sub

an example to send mail with gmal. yahoo or hotmail

http://www.programarexcel.com/2014/01/macro-emite-aviso-y-envia-mail-varios.html
http://www.programarexcel.com/2013/03/enviar-mail-con-excel.html

Visit http://programarexcel.com find examples of macros that can be of use  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


marcrodos

Expertise

Expert programming macros or visual basic VBA that can help automate your Excel spreadsheets using VBA programming or macros to excel, forms and other procedures related to Macros. I also have an excellent command of Excel in almost its entirety, formulas, graphs, dynamic tables. In http://programarexcel.blogspot.com o www.programarexcel.com examples can download free excel macros programmed in VBA, macros can be downloaded and adapted to the project that you're doing to automate excel www.programarexcel.com

Experience

For several years, use Microsoft Excel on a daily basis, more specifically in my work, which is why over several years I have compiled a wealth of knowledge about their operation, adding to the basic operation of coding excel VBA macros to automate repetitive tasks . I serve as an expert in excel also in www.todoexpertos.com site, which I enjoy a great reputation. In http://programarexcel.blogspot.com o www.programarexcel.com examples can download free excel macros programmed in VBA, macros can be downloaded and adapted to the project that you're doing to automate excel www.programarexcel.com

Organizations
http://www.programarexcel.blogspot.com http://www.programarexcel.com

Publications
In public http://www.programarexcel.com o www.programarexcel.com continuously macro examples that readers can download for free and adapt to the needs of each project in excel that has the web page visitor

Education/Credentials
My profession's is a Chartered Accountant with experience in Excel, especially in VBA or macro programming, but also in the formulas and everything related to excel, I graduated several years ago at the National University of Cordoba, now I practice my profession in which a basic tool is excel. www.programarexcel.com

Awards and Honors
http://www.programarexcel.blogspot.com http://www.programarexcel.com

Past/Present Clients
http://www.programarexcel.blogspot.com http://www.programarexcel.com

©2016 About.com. All rights reserved.