Formatted e-mails

B

ben

If I create a document with pictures/graphs, etc.. and click on the e-mail
button, and then fill in "send", "subject" and hit send it will send the
e-mail formatted as HTML with all the pictures included in the document.

However if I use the following code (taken from word.mpvs.org) to do so:


Dim bStarted As Boolean
Dim oOutlookApp As Object
Dim oItem As Object
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(0)

With oItem
'Set the recipient for the new email
.To = "(e-mail address removed)"
'Set the recipient for a copy
'Set the subject
.Subject = "Tux-Test"
'The content of the document is used as the body for the email
.Body = ActiveDocument.Content

.Send
End With

If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing

This sends as PLAIN TEXT and plain text only, all images/graphs are removed.
Is there no way I can send it as HTML W/O making it an attachment. That would
defeat the entire purpose of creating this. (Essentially a newsletter for
customers.)
 
D

Doug Robbins - Word MVP

The following is a modification of the code used in my article "Mail Merge
to E-mail with Attachments" at
http://word.mvps.org/FAQs/MailMerge/MergeWithAttachments.htm
that will send the documents in HTML format

Sub emailmergewithattachments()
'To create the email messages in
Dim source As Document, Maillist As Document, TempDoc As Document
Dim DataRange As Range
Dim i As Long, j As Long
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, Title As String

Set source = ActiveDocument

' Check if Outlook is running. If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
'oOutlookApp.DefaultProfileName
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

' Open the catalog mailmerge document
With Dialogs(wdDialogFileOpen)
.Show
End With
Set Maillist = ActiveDocument

' Show an input box asking the user for the subject to be inserted into the
email messages
message = "Enter the subject to be used for each email message." ' Set
prompt.
Title = " Email Subject Input" ' Set title.
' Display message, title
mysubject = InputBox(message, Title)

' Iterate through the Sections of the Source document and the rows of the
catalog mailmerge document,
' extracting the information to be included in each email.
For j = 1 To source.Sections.Count - 1
source.Sections(j).Range.Copy
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.Subject = mysubject
.BodyFormat = olFormatHTML
.Display
Set objDoc = .GetInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.Paste
Set DataRange = Maillist.Tables(1).Cell(j, 1).Range
DataRange.End = DataRange.End - 1
.To = DataRange
.cc = "(e-mail address removed); (e-mail address removed)"
For i = 2 To Maillist.Tables(1).Columns.Count
Set DataRange = Maillist.Tables(1).Cell(j, i).Range
DataRange.End = DataRange.End - 1
.Attachments.Add Trim(DataRange.Text), olByValue, 1
Next i
.Send
End With
Set oItem = Nothing
Next j
Maillist.Close wdDoNotSaveChanges

' Close Outlook if it was started by this macro.
If bStarted Then
oOutlookApp.Quit
End If

MsgBox source.Sections.Count - 1 & " messages have been sent."

'Clean up
Set oOutlookApp = Nothing

End Sub

You should be able to get what you need from this.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

Based on Doug's code example the revised version of your original code would
be as follows; however whether you choose to mail merge or to send
individual messages, it should be borne in mind that Word document format
and html e-mail format are entirely different from one another and some
elements of your document may not appear to the recipient as you intended.

At the very least you should create your newsletter in Web layout view.
Remember too that you have no control whatsoever how your message is viewed
by the recipient, who may choose to view e-mails only as plain text. Sending
as attachment - preferably in PFD format may be a better plan.


Dim bStarted As Boolean
Dim oOutlookApp As Object
Dim oItem As Object
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.Copy
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(0)
With oItem
.BodyFormat = olFormatHTML
.Display
Set objDoc = .GetInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.Paste
.to = "(e-mail address removed)"
.Subject = "Tux-Test"
.Send
End With
If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top