Consulting

Results 1 to 12 of 12

Thread: Solved: sending emails using vba

  1. #1

    Solved: sending emails using vba

    Hello,

    I will need to run a vba script from excel, which will active outlook to send an email to the relevant email address, a standard text message plus an email attachment. I would be grateful if someone can give me an example and point me to the right direction.
    Thank you

    yours,
    Ed

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi Ed,

    The basics are quite straight-forward - the problem you will run into is that using the "Send" method will trigger the Outlook Object Model Guard designed to stop Outlook being hijacked but a real PITA when it comes to automation. (There are a few references to this subject in the forum)
    Depending on what you're attempting to achieve, this might not be a big deal, so here's the code:[VBA]Sub SendMailMessage()

    Dim objOLapp As Object
    Dim objMailItem As Object

    Set objOLapp = CreateObject("Outlook.Application")
    Set objMailItem = objOLapp.CreateItem(0) '0=mailitem

    With objMailItem
    .To = ""
    .Subject = "subject text"
    .Body = "mail body text"
    .Attachments.Add "C:\TEMP\testattachment.txt"
    .Display 'show the mail
    .Send 'send the mail = OMG!
    End With

    End Sub[/VBA]
    K :-)

  3. #3
    Your code works perfectly. Now I just have to read up about formating of the ".body=..." for my email.
    Thank you very much!

    yorus,
    Ed

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Jolly good!

    Also have a look at the Bodyformat and HTMLBody properties, which may be relevant...
    K :-)

  5. #5
    will do, many thanks

    yours,
    Ed

  6. #6
    Hello,

    Can anyone tell me how to write a short email(like this one) under the ".body =..." , I am having difficulty working this out. Many thanks.

    yours,
    Ed

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Ed

    Using Killian's example code.
    Sub SendMailMessage()
    Dim objOLapp As Object
    Dim objMailItem As Object
    Dim strBody As String
        
        Set objOLapp = CreateObject("Outlook.Application")
        Set objMailItem = objOLapp.CreateItem(0) '0=mailitem
         
        strBody = strBody & "Hello," & vbCrLf & vbCrLf
        strBody = strBody & "Can anyone tell me how to write a short email(like this one) under the "".body =..."" , I am having difficulty working this out. Many thanks." & vbCrLf & vbCrLf
        strBody = strBody & "yours," & vbCrLf & vbCrLf
        strBody = strBody & "Ed" & vbCrLf & vbCrLf
        With objMailItem
            .To = ""
            .Subject = "subject text"
            .Body = strBody
            .Attachments.Add "C:\TEMP\testattachment.txt"
            .Display 'show the mail
            .Send 'send the mail = OMG!
        End With
         
    End Sub

  8. #8
    Thank you Norie, I had a go at it however, I seems to be having problems with the empty space line within the email. please advise. Thank you

    yours,
    Ed

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Ed

    What did you try and what was the problem?

  10. #10
    My fault, the code is working perfectly! I just forgetten to change ".body=strBody".

    Many many Tahnks!

    yours,
    Ed

  11. #11
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    I've marked the thread as solved - hope that's OK...
    K :-)

  12. #12
    Thanks Killian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •