none
Send word document as an attachment email to a specific receipient RRS feed

  • Question

  • Hi guys, 

    I am trying to create a button to send email to a specific email address with an attachment. This is what i have so far but it doesn't seem to work when the code is assigned to the "button"

    'Code'

    Sub SendDocumentInMail()

    Dim bStarted As Boolean
    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem

    On Error Resume Next

    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        'Outlook wasn't running, start it from code
        Set oOutlookApp = CreateObject("Outlook.Application")
        bStarted = True
    End If

    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(olMailItem)

    With oItem
        'Set the recipient for the new email
       .To = "recipient@mail.com"
        'Set the recipient for a copy
        .CC = "recipient2@mail.com"
        'Set the subject
        .Subject = "New subject"
        'The content of the document is used as the body for the email
        .Body = ActiveDocument.Content
        .Send
    End With

    If bStarted Then
        'If we started Outlook from code, then close it
        oOutlookApp.Quit
    End If

    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing

    End Sub

    Any advise will be greatly appreciated. 

    Thanks in advance.

    Tuesday, November 11, 2014 5:26 PM

Answers

  • Hello,

    What line of code exactly generates the error?

    Did you try to debug the code going through each line of code?

    It looks like you need to reference the available Outlook object library. To do this from Visual Basic (VB) or Visual Basic for Applications, follow these steps:

    1. In the Visual Basic Editor, on the Tools menu, click References.
    2. Click to select the Microsoft Outlook 15.0 Object Library check box, and then click OK.

    You may find the How to automate Outlook from another program article helpful. It describes all the required steps for automating Outlook.

    • Marked as answer by GeekzInside Wednesday, November 12, 2014 2:03 PM
    Wednesday, November 12, 2014 12:09 PM

All replies

  • Hello -Crafter-IT Hospital,

    "doesn't seem to work" Can you elaborate on this please? Any error message?


    Best regards George

    Tuesday, November 11, 2014 5:46 PM
  • Sorry about that... the error message i get when trying to run it is attached
    Tuesday, November 11, 2014 6:33 PM
  • Anybody have any ideas? 

    Wednesday, November 12, 2014 1:27 AM
  • Hi,

    Thanks for your question. This forum only focuses on questions and feedback for Microsoft Office client. I'm moving it to the forum of Outlook for Developers as it's an VBA code related question, where you can get more experienced responses:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=outlookdev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Ethan Hua
    TechNet Community Support


    It's recommended to download and install Configuration Analyzer Tool (OffCAT), which is developed by Microsoft Support teams. Once the tool is installed, you can run it at any time to scan for hundreds of known issues in Office programs.

    Wednesday, November 12, 2014 8:31 AM
  • Hello,

    What line of code exactly generates the error?

    Did you try to debug the code going through each line of code?

    It looks like you need to reference the available Outlook object library. To do this from Visual Basic (VB) or Visual Basic for Applications, follow these steps:

    1. In the Visual Basic Editor, on the Tools menu, click References.
    2. Click to select the Microsoft Outlook 15.0 Object Library check box, and then click OK.

    You may find the How to automate Outlook from another program article helpful. It describes all the required steps for automating Outlook.

    • Marked as answer by GeekzInside Wednesday, November 12, 2014 2:03 PM
    Wednesday, November 12, 2014 12:09 PM
  • Eugene, 

    Thank yo so much... it was the simplest thing i was missing. I just added the MS Outlook 14.0 library reference you mentioned on your post and it worked. 

    Thanks :) 

    Wednesday, November 12, 2014 2:04 PM