How to email data in Excel with a file name change each day?

  • Thread starter Paperback Writer
  • Start date
P

Paperback Writer

I have a macro, and I'd like to insert a new instruction that will email a
query in Excel (I know how to do that) with a name variant that automatically
changes each day (don't know how to do that).

For instance, if I ran my macro today, I'd want to email an Excel document
(based on a query) with a file name of TRANSACTIONS020508.xls. Tomorrow, I'd
like the macro to automatically email TRANSACTIONS020608.xls.

Thanks!!!
 
D

Daniel Pineault

I don't know about Macros but it can easily be done using vba to automate
outlook.

you can do it with code like

~~~~~~~~~~~~~~~
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

On Error GoTo ErrorMsgs

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message. Substitute
' your names here.
Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "Last test." & vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance
' Add attachments to the message.
AttachmentPath = "TRANSACTIONS" & Format(Now(),"mmddyy") & ".xls"
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
..........End If
End If
Next
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set objOutlookRecip = Nothing
Set objOutlookAttach = Nothing
ErrorMsgs:
If Err.Number = "287" Then
MsgBox "You clicked No to the Outlook security warning. " & _
"Rerun the procedure and click Yes to access e-mail" & _
"addresses to send your message. For more information, & _
"see the document at http://www.microsoft.com/office" & _
"/previous/outlook/downloads/security.asp. " "
Else
Msgbox Err.Number, Err.Description
End If
~~~~~~~~~~~~~~~~~~~~~

the bulk of this code can be found at

http://msdn2.microsoft.com/en-us/li...odc_ac_olauto_sendanoutlookmessageusingaccess
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top