Note: The other languages of the website are Google-translated. Back to English

How to apply a button to send email with current Word file attached? 

If you need to send an email message from a Word document via Outlook, and attach the current Word file as well, you can create a command button, and then, send the message by clicking this button without opening the Outlook. This article, I will introduce how to deal with it quickly and easily.

Apply a button to send email with current Word file attached


Apply a button to send email with current Word file attached

Please do with the following steps for solving this job in Word file:

1. First, you should create a command button, please click Developer > Legacy Tools > Command Button(ActiveX Control), see screenshot:

doc button to send email 1

2. Select the button, and click Properties under the Developer tab, in the Properties pane, type the caption text you need into the Caption field, see screenshot:

doc button to send email 2

3. Then, close the Properties pane, now, right click the button, and choose View Code, see screenshot:

doc button to send email 3

4. And then, in the displayed Microsoft Visual Basic for Applications window, copy and paste the below code between the original scripts, see screenshot:

Dim xOutlookObj As Object
    Dim xEmail As Object
    Dim xDoc As Document
    Application.ScreenUpdating = False
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmail = xOutlookObj.CreateItem(olMailItem)
    Set xDoc = ActiveDocument
    xDoc.Save
    With xEmail
        .Subject = "Fax-data"
        .Body = "This is a test email."
        .To = "yy@addin99.com"
        .Importance = olImportanceNormal
        .Attachments.Add xDoc.FullName
        .Display
    End With
    Set xDoc = Nothing
    Set xEmail = Nothing
    Set xOutlookObj = Nothing
    Application.ScreenUpdating = True

doc button to send email 4

Note: In the above code, you should change the subject, body text or sent address to your need.

5. Then, save and close this code, click Design Mode to turn off the design mode. Now, when clicking the command button you have created, an email will be created with the current Word document as attachment, see screenshot:

doc button to send email 5

6. At last, you just need to click Send button to send this message.


Recommended Word Productivity Tools

 

Kutools For Word - More Than 100 Advanced Features For Word, Save Your 50% Time

  • Complicated and repeated operations can be done one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders into one with your desired order.
  • Split the current document into separate documents according to heading, section break or other criteria.
  • Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
Comments (23)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I am trying to have the email send with the subject line being the contents of one of my fields. Is this possible?
This comment was minimized by the moderator on the site
Hola, me podrían apoyar para que el archivo se convierta en PDF y se adjunte al correo en ves del word por favor.
Muchas gracias.
This comment was minimized by the moderator on the site
Hi Extend Office Team,

I believe 'xEmile' is misspelt. It should be 'xEmail'.

While the code runs and there is no issue, the following statement will do nothing:

Set xEmail = Nothing
This comment was minimized by the moderator on the site
Hi,Abhi,
Thank you for your comment, as you said, the 'xEmile' is misspelt, it should be 'xEmail'. I have updated the code.
Thank you again!
This comment was minimized by the moderator on the site
Can this be done so you only send one page of a document?
This comment was minimized by the moderator on the site
Hello, Don,
To deal with your task, please apply the below code,in the code, you should change the start page and end page to your own:

Private Sub CommandButton1_Click()

Dim xCurPageStart As Long, xCurPageEnd As Long, xCurPageRange As Range

Dim xCurPage As Integer, xPages As Integer

Dim xOutlookObj As Object

Dim xEmail As Object

Dim xDoc As Document, xNewDoc As Document

Dim xFilePath As String

Dim xOldStart As Long, xOldEnd As Long

On Error Resume Next

Application.ScreenUpdating = False

Set xOutlookObj = CreateObject("Outlook.Application")

Set xEmail = xOutlookObj.CreateItem(olMailItem)

xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)

Set xDoc = ActiveDocument

xOldStart = xDoc.Application.Selection.Start

xOldEnd = xDoc.Application.Selection.End

xDoc.Save

xFilePath = xFilePath + "\attached file" + VBA.Mid(xDoc.FullName, VBA.InStrRev(xDoc.FullName, ".")) 'attached file is the attachment file name,change to your need

xCurPageStart = 2 'start page

xCurPageEnd = 2 'end page

xPages = Selection.Information(wdNumberOfPagesInDocument)

xCurPageStart = Selection.GoTo(what:=wdGoToPage, Which:=wdGoToNext, Name:=xCurPageStart).Start

If xCurPage = xPages Then

xCurPageEnd = ActiveDocument.Content.End

Else

xCurPageEnd = Selection.GoTo(what:=wdGoToPage, Which:=wdGoToNext, Name:=xCurPageEnd + 1).Start

End If

Set xCurPageRange = ActiveDocument.Range(xCurPageStart, xCurPageEnd)

xCurPageRange.Select

xCurPageRange.Copy

Set xNewDoc = Application.Documents.Add(Visible:=False)

xNewDoc.Activate

xNewDoc.Content.PasteAndFormat wdFormatOriginalFormatting

xNewDoc.SaveAs2 FileName:=xFilePath

xNewDoc.Close

xDoc.Range(xOldStart, xOldEnd).Select

With xEmail

.Subject = "Fax-data"

.Body = "This is a test email."

.To = "yy@addin99.com"

.Importance = olImportanceNormal

.Attachments.Add xFilePath

.Display

End With

VBA.Kill xFilePath

Set xDoc = Nothing

Set xEmail = Nothing

Set xOutlookObj = Nothing

Application.ScreenUpdating = True

End Sub
This comment was minimized by the moderator on the site
I have made a fillable form in MS word. I have used legacy tools for calculation etc. I have also added a submit button which works to send the form to email. The problem is that when I reopen this document after saving, it does not work. I have even tried saving it as macro enabled document but no use. Can you please help ?
Thanks
This comment was minimized by the moderator on the site
I had it working at first, and then for some reason now, it gives me an error of error 429 about activeX component can't create object. i literally just copied and pasted. This worked before but i just changed font size, made button bigger, and relabeled caption. i don't think i accidentally changed some other settings? any idea what it could be?
This comment was minimized by the moderator on the site
Hi! I'd like to make the command button send an email with the Word document as the body of the email (my word document is a form). Is there a way to do that?
This comment was minimized by the moderator on the site
I'd like my document to send as a PDF document rather than a word document.
This comment was minimized by the moderator on the site
<p>For example using text from a textbox/textfield in the Word-document, and inserting it as text in the subject line of your E-mail?</p>
This comment was minimized by the moderator on the site
Yes this is also what i'm after can anybody help with this request?
This comment was minimized by the moderator on the site
Does this also work for Lotus Notes?
What must I change in "xOutlookObj"?
This comment was minimized by the moderator on the site
Hi Extend office,
Can we set a different email not linked to Outlook? Like direct email to GoDaddy account?
This comment was minimized by the moderator on the site
I am trying to find out the same information. I use gmail and I need to create a form to send out to varies different email address that can automatically submit back to my gmail account.
This comment was minimized by the moderator on the site
I have create the document but my issue  now is that if I send this form to say 4 people to complete, when they open it up from the email attachment it opens in read only mode and when they click submit it asks them to save as before it will allow the code to open an email addressed to me. Is there a way around this other than giving them the location of the file?
This comment was minimized by the moderator on the site
Can you add Bcc to this code? I have tried adding Bcc and receive a compile error for expected expression.
This comment was minimized by the moderator on the site
Hello Heather,

You can add Bcc to this code. For example, you can add .Bcc = "zmt@addin99.com" just below .To = "yy@addin99.com". So the code becomes:

Dim xOutlookObj As Object
Dim xEmail As Object
Dim xDoc As Document
Application.ScreenUpdating = False
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmail = xOutlookObj.CreateItem(olMailItem)
Set xDoc = ActiveDocument
xDoc.Save
With xEmail
.Subject = "Fax-data"
.Body = "This is a test email."
.To = "yy@addin99.com"
.Bcc = "zmt@addin99.com"
.Importance = olImportanceNormal
.Attachments.Add xDoc.FullName
.Display
End With
Set xDoc = Nothing
Set xEmail = Nothing
Set xOutlookObj = Nothing
Application.ScreenUpdating = True

Please see the attached picture. Have a nice day.
Sincerely,
Mandyhttps://www.extendoffice.com/images/stories/comments/comment-zmt/Add_Bcc_to_code.png
This comment was minimized by the moderator on the site
Hello folks,

I want to apply a button where I click and send the document as email, not as an attachement, to one Email-Adress. So that the document is the text of the email. Just like the effekt of the button "send to recipient" on the ribbon.

Can someone give me the right code for that?

Thank you.
This comment was minimized by the moderator on the site
Hello,
May be the code can't help to send the current word document as the email body, if to do, the picture and file formatting within the document will be lost.
If you want to send the current document as email body, the Mail Merge can do you a favor.
Do you need the Mail Merge for sending email?
If you need, please comment here.
Thank you!
This comment was minimized by the moderator on the site
Hello,

thank you for your help!

I would try it with your suggestion. So yes, I need the Mail Merge for sending E-Mail.
This comment was minimized by the moderator on the site
Hello,

thank you for your help!

I would try it with your suggestion. Could u give me the code for mail merge?

Thanks!
This comment was minimized by the moderator on the site
Hello,
To send current word document as mail body, you can add the Send to Mail Recipient command to your QAT, please do as this:
1. Open your word file that you want to send, and then click File > Options.
2. On the left pane, select Quick Access Toolbar.
3. Select Commands Not in the Ribbon from the Choose commands from.
4. Choose and select Send to Mail Recipient and click the button Add > >.
5. Click OK to close the dialog.
https://www.extendoffice.com/images/stories/comments/comment-skyyang/doc-send-file-1.png

Now, this Send to Mail Recipient command will show up in the Quick Access Toolbar at the top of your Word window. Then, you can insert the relative information into the message header. After finishing the information, please click Send a copy, the message with current word document as body will be sent at once.

https://www.extendoffice.com/images/stories/comments/comment-skyyang/doc-send-file-3.png

Please try this, hope it can help you!
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations