Share via

VBA code for send e-mail button

Anonymous
2011-08-24T17:37:12+00:00

Hello,

I'm trying to find some Word VBA code that I can use to send an active document as an e-mail attachment.

  • I will attach it to a button
  • We're using Windows 7 and Office 2010

Can anyone help me?

Thanks

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2011-08-26T14:28:49+00:00

Although the methods and properties for routing slips are still present in Word VBA, they cannot be used any more starting with Word 2007.

Try this instead:

Sub Test()

    Options.SendMailAttach = True

    ActiveWindow.EnvelopeVisible = True

    With ActiveDocument.MailEnvelope.Item

        .Subject = "Testing 123"

        .Recipients.Add "******@somewhere.com"

    End With

End Sub

Was this answer helpful?

0 comments No comments

18 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-12-09T21:42:56+00:00

    Try this instead:

    Sub Test()

        Options.SendMailAttach = True

        ActiveWindow.EnvelopeVisible = True

        With ActiveDocument.MailEnvelope.Item

            .Subject = "Testing 123"

            .Recipients.Add "*** Email address is removed for privacy ***"

        End With

    End Sub

    I tried this with Word2016, and it threw an error at the 'activeWindow.EnvelopeVisible=True' line.

    I know this is a REALLY OLD thread, but is there a new and better way to put a 'send me' button on a WordDocument that will pre-fill the subject and recipient?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-08-30T13:28:18+00:00

    The following should do the trick subject to a couple of provisos.

    1. You will need to set a reference to the outlook object library in Tools > references
    2. You need to enter the name of signature you wish to use where indicated. It will not insert signature graphics, so if necessary create a signature for the job.
    3. Complete the e-mail address(es) , subject line and Body text content where indicated.

    If you want greater control including the ability to use signatures with graphics then you need the longer code in the link I posted earlier. Both versions will work.

    Option Explicit

    Sub Send_As_Mail_Attachment()

    ' Set a reference to the Outlook object library

    ' Send the document as an attachment in an Outlook Email message

    'Enter the name of the signature ( does not insert graphics in signatures)

    Const sText As String = "signaturename"

    Dim bStarted As Boolean

    Dim oOutlookApp As Outlook.Application

    Dim oItem As Outlook.MailItem

    Dim SigString As String

    Dim Signature As String

    On Error Resume Next

    'Prompt the user to save the document

    ActiveDocument.Save

    'Get Outlook if it's running

    Set oOutlookApp = GetObject(, "Outlook.Application")

    'Outlook wasn't running, start it from code

    If Err <> 0 Then

       Set oOutlookApp = CreateObject("Outlook.Application")

       bStarted = True

    End If

    SigString = "C:\Users" & Environ("username") & _

                "\AppData\Roaming\Microsoft\Signatures" & sText & ".htm"

    If Dir(SigString) <> "" Then

        Signature = GetBoiler(SigString)

    Else

        Signature = ""

    End If

    'Create a new mailitem

    Set oItem = oOutlookApp.CreateItem(olMailItem)

        With oItem

            .to = "someoneATsomewhere.com"

        'Set the recipient for a copy is required

            .BCC = "someoneelseATsomewhere.com"

            .Subject = "This is the subject"

            'create the body with html tags

            .HTMLBody = "<font face=""Calibri"" size=""4"" >Please complete the attached form " & _

            "and return to the sender. <br><br>" _

            & Signature

        'Add the document as an attachment

            .Attachments.Add Source:=ActiveDocument.FullName, _

            Type:=olByValue, DisplayName:="Document as attachment"

            .Display

         End With

    'Clean up

    Set oItem = Nothing

    Set oOutlookApp = Nothing

    End Sub

    Function GetBoiler(ByVal sFile As String) As String

        Dim fso As Object

        Dim ts As Object

        Set fso = CreateObject("Scripting.FileSystemObject")

        Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)

        GetBoiler = ts.ReadAll

        ts.Close

    End Function

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-08-30T12:30:37+00:00

    Once again thanks for your help. This is kind of working. I don't know if what I want is possible but let me try to be clearer.

    I would like to:

    • Have a command button on my document
    • The user would click it (to e-mail)
    • Word would attach the active document to a new e-mail (it's a form)
    • The e-mail would have a predefined e-mail address and subject line

    Some funny things happen with this macro.

    • When I use the code "when clicking" the button, I get an object error
    • When I run the code with no button, I don't get the error. (tested this many times)
    • The Word document does not attach, it appears to be creating an HTML body e-mail
    • When the e-mail arrives, there is no attachment (contents are in the body)

    We need the attachment because we are creating a protected form and we need people to fill it out and send to us.

    Let me know if you think I can do this.

    Thanks

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-08-26T14:08:55+00:00

    Thanks for the link. This seems like an extra long solution. I'm really hope there is something shorter and easier. Have you ever tested this?

    Was this answer helpful?

    0 comments No comments