1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

  2. 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


  3. 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

Discussion in 'VBA Macros' started by debxxx1, May 16, 2018.

  1. debxxx1

    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 (vb):
    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
            End With

    End Sub
  2. Logit

    Logit Active Member

    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.
    Chirag R Raval likes this.

Share This Page