Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-21-2019, 01:00 AM
ajh19742000 ajh19742000 is offline Email button in word document Windows 10 Email button in word document Office 2013
Novice
Email button in word document
 
Join Date: Dec 2015
Posts: 10
ajh19742000 is on a distinguished road
Default Email button in word document

I'm guessing this has been asked before. I have a button to send my word document, which is a form, and need the button to send it as an email but would like a couple of the fields in the form to be the subject heading in the email along side some default text. Any ideas on the code needed please?
Reply With Quote
  #2  
Old 07-21-2019, 02:18 AM
gmayor's Avatar
gmayor gmayor is offline Email button in word document Windows 10 Email button in word document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Sending a document by e-mail is easy enough and has indeed been covered before, however what is the purpose of sending the document and who is it being sent to?

You say it is a 'form'. If you want it to remain as a working form there is little point sending it as the body of a message, so you should save it as a document and send the document as an attachment to a message. The following will do that and as your form should use content controls, it takes the subject from a content control titled subject. You can modify it to use more than one control for the subject if you wish.

Note the comment at the top of the macro. It won't work without that code.

To convert the form from form fields to content controls you may find Insert Content Control Add-In useful

Code:
Option Explicit

Sub Send_As_Mail_Attachment()
'Graham Mayor - https://www.gmayor.com - Last updated - 21 Jul 2019
'Send the document as an attachment in an Outlook Email message
'Requires the code from - http://www.rondebruin.nl/win/s1/outlook/openclose.htm
'to either retrieve an open instance of Outlook or open Outlook if it is closed.
Dim bStarted As Boolean
Dim olApp As Object
Dim oItem As Object
Dim olInsp As Object
Dim wdDoc As Object
Dim oRng As Object
Dim oDoc As Document
Dim strName As String
Dim oCC As ContentControl
Dim strSubject As String

    Set oDoc = ActiveDocument
    On Error GoTo err_Handler:
    oDoc.Save
    Set oCC = oDoc.SelectContentControlsByTitle("Subject").Item(1)
    If oCC.ShowingPlaceholderText = False Then
        strSubject = oCC.Range.Text
    Else
        strSubject = "Default subject text"
    End If
    strName = oDoc.FullName
    'Get Outlook if it's running
    Set olApp = OutlookApp()
    'Create a new mailitem
    Set oItem = olApp.CreateItem(0)

    With oItem
        .Display
        .To = "someone@somewhere.com"
        .Subject = strSubject
        .Attachments.Add strName
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
        oRng.Collapse 1
        oRng.Text = "Complete the attached form and return to sender."
        '.Send 'Restore line after testing
    End With

lbl_Exit:
    Set oItem = Nothing
    Set olApp = Nothing
    Set olInsp = Nothing
    Set wdDoc = Nothing
    Set oDoc = Nothing
    Set oRng = Nothing
    Exit Sub
err_Handler:
    Err.Clear
    GoTo lbl_Exit
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to create a button to attach the active word doc to an email as a PDF without using Outlook TAKMalcolm Word VBA 1 09-21-2017 01:52 AM
Sending a document by email from Button, MAC compatible Ghostbuataar Word VBA 0 05-02-2016 09:14 AM
Email button in word document Adding email button to Word form leilers Word 5 01-09-2012 03:21 PM
Adding an Email Button to a Word Document maddoktor Word 0 12-01-2011 01:32 PM
MS Word form - email button floydwood Word VBA 0 05-10-2009 04:11 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:28 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft