• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Code to send an Email with .msg attachment

debxxx1

New Member
Hi Genius people,

If you can help me with below I will be very helpful. Thanks in advance.

I have some Outlook message(.msg) files in my local system. I am trying to send an outlook email where these (.msg) files will be attached. Below is the code which I am using but in the attachments.add line I am getting error "Operation Failed". Please help

Code:
Sub CreateHTMLMail()

    Dim olApp                          As Object
    Dim objMail                        As Object
    Dim body, head                      As String
    Dim strfile                        As String
   
    Set olApp = CreateObject("Outlook.Application")
   
    strfile = "C:/Users/Debasi1N/Documents/abc/Inbox/20180110-0933_External Training.msg"
   
    'Create e-mail item
    Set objMail = olApp.CreateItem(0)


        head = "<HTML><BODY><P>HiTeam,</P>"
        body = body & "<BR /><P>This is a testing email <STRONG>Line check 1</STRONG> line check 2, <STRONG>11/17</STRONG>!  Line check 3.</P>"
        body = body & "<BR /><P>Please find attached sample email.</P>"
        body = body & "<BR /><P>Thanks,<BR />SE Team</P></BODY></HTML>"

        With objMail
            .subject = wksM.Cells(6, 3)
            .To = wksM.Cells(2, 3)
            .CC = wksM.Cells(4, 3)
            .Attachments.Add = strfile
            .BodyFormat = olFormatHTML
            .HTMLBody = head & body
            .Display
        End With

End Sub
 
I have found here when a file name has spaces in it, that causes issues.

Try changing the name of the file to something without spaces in the name. Either use underscores to replace the spaces or simply run all the words together.

See if that works.
 
Back
Top