`CLOSE all *Here's an example of creating and sending email by automating Outlook:
LOCAL oOutlook, oNameSpace, oMailItem
oOutlook = CreateObject("Outlook.Application")
oNameSpace = oOutlook.GetNameSpace("MAPI")
oMailItem = oOutlook.CreateItem(0)
#DEFINE CR CHR(13)
WITH oMailItem
.Subject = "Daily Reports"
.Body = " Your daily report is there." CR ;
.Recipients.Add("[email protected]")
ENDWITH
oMailItem.Send()
*I am looking for a way to add an excel file to this little program?`
CodePudding user response:
You could try this. However, it will only work for that specific email, you will need to specify other paths, for every other email. Assuming you'll be needing more than one.
LOCAL oOutlook, oNameSpace, oMailItem
oOutlook = CreateObject("Outlook.Application")
oNameSpace = oOutlook.GetNameSpace("MAPI")
oMailItem = oOutlook.CreateItem(0)
#DEFINE CR CHR(13)
WITH oMailItem
.Subject = "Daily Reports"
.Body = " Your daily report is there." CR
.Recipients.Add("[email protected]")
' Attach your Excel file
.Attachments.Add("C:\path\to\file.xlsx")
ENDWITH
oMailItem.Send()
CodePudding user response:
The Attachments.Add method creates a new attachment in the Attachments collection. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. So, if you need to attach an excel file, you have to pass a file path to the Add
method:
Sub AddAttachment()
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myItem = Application.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\Test.xslx", _
olByValue, 1, "Test"
myItem.Display
End Sub
So, in your sample code just need to add a single line of code:
WITH oMailItem
.Subject = "Daily Reports"
.Body = " Your daily report is there." CR ;
.Recipients.Add("[email protected]")
.Attachments.Add("C:\Test.xslx", olByValue, 1, "Excel file" )
ENDWITH