This is an old thread, so I thought it worth updating my earlier code.
The document is always saved in order to attach it, but it is difficult to reproduce your requirement as the SendMail option requires some user interaction. To avoid that you can create a process that will do so. The following code should go in the form
template (saved as a macro enabled template), and it will need the Function to open Outlook from Ben Clothier - http://www.rondebruin.nl/win/s1/outlook/openclose.htm for reasons discussed there. The following code calls that function so will crash without
it.
Create a new document from the template and complete it.
The main macro uses a function I developed to send documents by mail, in a variety of ways, but you only need to send the document as an attachment, so that function called from the first macro. That macro saves the document to a temporary location, attaches
the temporary document to the message, and sends it to the named address, with the message body and subject placed in the message. The default signature associated with the sending account is included.
The document is then closed and the user offered the opportunity to create a new document.
Option Explicit
Sub SendDoc()
'Graham Mayor - http://www.gmayor.com - Last updated - 10/12/2016
Const strMsg As String = "Please find the enclosed document" 'the message body text
Const strRecipient As String = "someoneATsomewhere.com" 'the e-mail address of the intended recipient
Const strSubject As String = "Attached Form" 'the message subject
Dim sPath As String
sPath = Environ("TEMP") & "\Form Document.docx" 'the temporary filename
ActiveDocument.SaveAs2 FileName:=sPath, AddToRecentFiles:=False
Send_As_Mail strRecipient, strSubject, strMsg, True
ActiveDocument.Close 0
If MsgBox("Create another form?", vbYesNo) = vbYes Then
Documents.Add Template:=ThisDocument.FullName
End If
lbl_Exit:
Exit Sub
End Sub
Public Sub Send_As_Mail(strTo As String, _
strSubject As String, _
strMessage As String, _
Optional bSendAsAttachment As Boolean, _
Optional bPDFFormat As Boolean, _
Optional strAttachment As String)
'Graham Mayor - http://www.gmayor.com - Last updated - 10/12/2016
'bSendAsAttachment - Enter True/False - indicate whether to send the active document as an attachment
'Requires the code by Ben Clothier - 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 olApp As Object
Dim olInsp As Object
Dim oItem As Object
Dim wdDoc As Object
Dim oRng As Object
Dim bStarted As Boolean
Dim oDoc As Document
Dim strDocName As String
Dim strPath As String
Dim intPos As Integer
Dim iFormat As Long
Set oDoc = ActiveDocument
If Not bSendAsAttachment Then oDoc.Range.Copy
If bSendAsAttachment Then
'On Error GoTo Err_Handler
'Prompt the user to save the document
If bPDFFormat Then
strDocName = oDoc.Name
strPath = oDoc.Path & ""
intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)
strDocName = strPath & strDocName & ".pdf"
oDoc.ExportAsFixedFormat OutputFilename:=strDocName, _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, From:=1, to:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateHeadingBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
Else
strDocName = oDoc.FullName
End If
End If
Set olApp = OutlookApp()
'Create a new mailitem
Set oItem = olApp.CreateItem(0)
With oItem
.to = strTo
.Subject = strSubject
If bSendAsAttachment Then .Attachments.Add strDocName
If Not strAttachment = "" Then .Attachments.Add strAttachment
.BodyFormat = 2 'olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
.Display
If bSendAsAttachment Then
oRng.Text = strMessage & vbCr
Else
oRng.Paste
End If
'.Send 'restore after testing
End With
If bStarted Then olApp.Quit
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