Sending email from Write

The following example relies on Active Messaging - an OLE wrapper for MAPI. You may or may not have this installed on your system!

1. Start the macro and declare the variables

Sub SendEmail

Dim objSession, objMessage, objRecip, strEmsg, strTo, strSubject 

2. Get the entire text of the document

strEmsg = ActiveDocument.Text.Mid(0, ActiveDocument.Text.Count)

This is the quickest way to get the text of the document – no consideration is given to font changes!

3. Set the fields for Send To and Subject

strTo = "support@ability.com" 

strSubject = "Send from Ability Write" 

These details are in the code at the moment – it would be much better to extract them from the actual document. See the suggestions at the bottom or this page for more on how to do this.

4. Start a MAPI session

Set objSession = CreateObject("MAPI.SESSION") 

objSession.Logon "MS Exchange Settings" 

Start a session and logon. This is a bit like starting Microsoft Outlook. Note the logon is dependent on the current profiles on the user machine – in other words, you may have to change "MS Exchange Settings" to whatever your logon details are.

5. Create a message

Set objMessage = objSession.Outbox.Messages.Add 

objMessage.Subject = strSubject 

objMessage.Text = strEmsg 

 

Set objRecip = objMessage.Recipients.Add 

objRecip.Name = strTo 

objRecip.Type = 1 '

objRecip.Resolve 

The first block above creates a new message and copies in the text from the document.

The second block adds the recipient details. Note the Type is set to 1 – this is the value of mapiTo constant (defined as part of OLE Messaging).

6. Send the message

objMessage.Update 

objMessage.Send 

objSession.Logoff 

End Sub

Save the message, send it and logoff.

See: Refining the send email macro for suggestions on how to refine this macro.

For the complete code listing plus refinements, see Sending email from Write - code listing.