Word Hacks [Electronic resources]

Andrew Savikas

نسخه متنی -صفحه : 162/ 65
نمايش فراداده

Hack 40 Send a Document as a Plain-Text Outlook Email

Use a macro to turn the text of a Word document into the body of an Outlook email.

Sometimes sending a document as an email attachment is overkill, especially if you just want the recipient to review the text of a simple document. And if the recipient is under a particularly restrictive mail server, he may not be able to receive Word document attachments, for fear of a virus.

Now, you can always try to cut and paste the text into the body of an email, but that rarely produces anything better than a giant block of continuous text that can be difficult to read from an email window.

Another option is to save the document as a plain-text .txt file, open up that file in a text editor such as Notepad, add some extra line breaks for readability, then cut and paste that into an email. Yuck.

Here's an easier option: after changing a few settings from within the Visual Basic Editor, you can write macros that directly access Outlook from within Word, let you get the text-only contents of a document, add some extra line breaks for readability, quickly create a new email message, and insert the text as the message body.

4.15.1 Setting the Reference to Outlook

First, make sure you can access the Outlook object model from within Word. Select ToolsMacroVisual Basic Editor, then choose ToolsReferences. Scroll down and check the "Microsoft Outlook 11.0 Object Library" box, as shown in Figure 4-21.

If you're using an earlier version of Outlook, select the reference to that version. The code in this hack will still work.

Figure 4-21. Setting a reference to the Outlook object model

Once you've established that reference, you can access the Outlook object model from your Word macros.

4.15.2 The Code

Place this macro in the template of your choice [Hack #50] and either run it from the ToolsMacroMacros dialog or put a button for it on a menu or toolbar [Hack #1].

Since there's a good chance Outlook may already be open when running a macro that accesses it, the code first tries to reference the currently open instance of Outlook. If it can't find one, the code creates and uses a new instance of Outlook.

Sub doc2outlookemail( )
Dim sDocText As String
Dim oOutlook As Outlook.Application
Dim oMailItem  As Outlook.MailItem
' Get currently running Outlook, or create new instance
On Error Resume Next
Set oOutlook = GetObject(Class:="Outlook.Application")
If Err.Number = 429 Then
Set oOutlook = CreateObject(Class:="Outlook.Application")
ElseIf Err.Number <> 0 Then
MsgBox "Error: " & Err.Number & vbCr & Err.Description
Exit Sub
End If
sDocText = ActiveDocument.Content.Text
' Replace each paragraph break with two paragraph breaks
sDocText = Replace(sDocText, Chr(13), String(2, Chr(13))
Set oMailItem = oOutlook.CreateItem(olMailItem)
oMailItem.Body = sDocText
oMailItem.Display
' Clean up references to Outlook objects
Set oMailItem = Nothing
Set oOutlook = Nothing
End Sub

The code leaves the email open and unaddressed. Just fill in the recipient's address and click the Send button.

4.15.3 Hacking the Hack

Rather than sending the entire contents of a Word document, you may want to send just the outline as a plain-text email. To do so, first switch to Outline view in your document and select the outline level you want included in the email. Only the text visible from Outline view will be included in the email.

Now make a minor adjustment to the earlier macro, as shown in bold:

Sub SendOutlineOnly( )
Dim sDocText As String
Dim oOutlook As Outlook.Application
Dim oMailItem  As Outlook.MailItem
' Get currently running Outlook, or create new instance
On Error Resume Next
Set oOutlook = GetObject(Class:="Outlook.Application")
If Err.Number = 429 Then
Set oOutlook = CreateObject(Class:="Outlook.Application")
ElseIf Err.Number <> 0 Then
MsgBox "Error: " & Err.Number & vbCr & Err.Description
Exit Sub
End If
' Just want the outline
ActiveDocument.Content.TextRetrievalMode.ViewType = wdOutlineView
sDocText = ActiveDocument.Content.Text
' Replace each paragraph break with two paragraph breaks
sDocText = Replace(sDocText, Chr(13), String(2, Chr(13)))
Set oMailItem = oOutlook.CreateItem(olMailItem)
oMailItem.Body = sDocText
oMailItem.Display
' Clean up references to Outlook objects
Set oMailItem = Nothing
Set oOutlook = Nothing
End Sub