I've written a very simple macro to send a workbook as an email from the workbook. There are 14 workbooks that I applied this macro to. Most of them send fine, but others do not send at all. I've narrowed it down to probably being something wrong in the .to field in Outlook 365. The formula pulls in the contact's name and places it in the .to box. I could not find anything different within the contacts to explain this. I'm using Excel 2013 and Outlook 365. The VBA follows:
Thanks,
Michel
Code:
Sub SendMail()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = Sheets("Sheet2").Range("A2").Value
.CC = ""
.BCC = ""
.Subject = Sheets("Sheet2").Range("B2").Value
.Body = Sheets("Sheet2").Range("C2").Value
.Attachments.Add ActiveWorkbook.FullName
'.Attachments.Add ("C:\test.txt")
.Send
End With
On Error GoTo 0
ActiveWorkbook.Save
ActiveWorkbook.Close
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Thanks,
Michel