Reputation: 251
I'm trying to send and email with an attachment with VBA. The code works fine without the Attachments.Add line, but with it it get the error "Run-time error '440': The operation failed."
I've looked online and can't seem to find a reason for this. Am I not setting the email object correctly?
Code below:
Sub test()
Static objOutlook
Dim objMailItem
Dim objFileSystem
Dim objNamespace
Dim objSentFolder
Const olFolderInbox As Long = 6
Set objOutlook = CreateObject("Outlook.Application")
objOutlook.Session.GetDefaultFolder(olFolderInbox).Display
objOutlook.ActiveExplorer.WindowState = WindowState
Set objMailItem = objOutlook.CreateItem(0)
objMailItem.Display
With objMailItem
.Subject = "test"
Set recip = .Recipients.Add("[email protected]")
.Attachments.Add = "file.xls"
.Body = ""
.DeleteAfterSubmit = False
End With
objMailItem.Send
Set objFileSystem = Nothing
Set objMailItem = Nothing
Set objOutlook = Nothing
End Sub
Upvotes: 2
Views: 5326
Reputation: 166755
.Attachments.Add "file.xls"
there is no = required (or allowed...)
If you're not doing so already, you should pass the full path to the file, and not just the name, otherwise your code may fail if the current directory is not what you expect.
Upvotes: 2