The sample attaches the document containing the code so in effect it will send the file in its last 'SAVED' state. So it would be wise to save the document prior to executing the code. This can implemented in code as well.
To set up this sampleyou will need a new Word document.
In the Word document use the ALT+F11 key combination to switch to the Visual Basic Editor (VBE).
In VBE, insert a module into the new document.
Select TOOLS/REFERENCES from the main VBE menu.
Find the Microsoft Outlook library and check it (use the latest version if more than one is present).
Select OK.
Insert the following code into the module.
Public Sub createEmailDocWithAttachmentSample()
Dim olObj As Outlook.Application
Dim mlItem As Outlook.MailItem
getoutlook oleToOutlook:=olObj
If olObj Is Nothing Then
MsgBox prompt:="Unable to find Outlook!", _
buttons:=vbExclamation + vbOKOnly, _
Title:=ThisDocument.FullName
Exit Sub
End If
Set mlItem = olObj.CreateItem(itemtype:=MailItem)
With mlItem
.Display
.To = "test@test.com"
.Subject = "this is a test"
.Body = "Basic text"
.Attachments.Add Source:=ThisDocument.FullName
End With
Set mlItem = Nothing
Set olObj = Nothing
End Sub
Private Function getoutlook(oleToOutlook As Outlook.Application)
Set oleToOutlook = Nothing
On Error Resume Next
Set oleToOutlook = GetObject(, "Outlook.application")
On Error GoTo 0
If oleToOutlook Is Nothing Then
On Error Resume Next
Set oleToOutlook = CreateObject("Outlook.Application")
On Error GoTo 0
End If
End Function
Save the new document.
Execute the createEmailDocWithAttachmentSample routine to try it.