Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / General MS Word Questions / December 2007

Tip: Looking for answers? Try searching our database.

How to capture programatically emailing a Word 2003 doc?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Namgaw - 06 Dec 2007 22:30 GMT
I am running Word 2003 (Outlook 2003 for those interested) and I need
to programatically save a document to HTML-filtered format then use
the File>Send To>Mail Recipient to send it off, also programatically.
Can anyone give me a start on this, particularely on the mail portion?
TIA
Peter Jamieson - 07 Dec 2007 13:33 GMT
Here's some code that does a "one mailmerge per data source record, saving
the document in filtered HTML format then using the Outlook object model to
send it. Strip out the bits you don't need. I haven't tested it much so
there may well be problems, but it's a start.

Sub EmailOneHTMLPagePerSourceRecWithBody()
' By Peter Jamieson, 2006
Dim bOutlookStarted As Boolean
Dim bTerminateMerge As Boolean
Dim intSourceRecord As Integer
Dim objMailItem As Outlook.MailItem
Dim objMerge As Word.MailMerge
Dim objOutlook As Outlook.Application
Dim strMailCC As String
Dim strMailSubject As String
Dim strMailTo As String
Dim strMailBody As String
Dim strOutputDocumentName As String

bOutlookStarted = False
bTerminateMerge = False

' Set up a reference to the
' Activedocument, partly because
' the ActiveDocument changes as you
' merge each record

Set objMerge = ActiveDocument.MailMerge

' Start Outlook as necessary

On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err <> 0 Then
 Set objOutlook = CreateObject("Outlook.Application")
 bOutlookStarted = True
End If

With objMerge

' If no data source has been defined,
' do it here using OpenDataSource.
' But if it is already defined in the
' document, you should not need to
' define it here.

' .OpenDataSource _
' Name:="whatever"

intSourceRecord = 1

Do Until bTerminateMerge
 .DataSource.ActiveRecord = intSourceRecord

 ' if we have gone past the end
 ' (and possibly, if there are no records)
 ' then the Activerecord will not be what
 ' we have just tried to set it to

 If .DataSource.ActiveRecord <> intSourceRecord Then
   bTerminateMerge = True
   ' the record exists
 Else

   ' while we are looking at the
   ' correct activerecord,
   ' create the mail subject, body, "to" and "cc"
   ' Just some sample code here - replace it with
   ' whatever you need. But ensure that the field names
   ' match the ones in your data source exactly - uppercase/lowercase
   ' differences are significant here

   strMailSubject = _
     "Results for " & _
     objMerge.DataSource.DataFields("Firstname") & _
     " " & objMerge.DataSource.DataFields("Lastname")

   ' Use a simple sample
   strMailBody = "<HTML><BODY><TABLE
BORDER=5><TR><TD>k</TD><TD>t</TD></TR></TABLE></BODY></HTML>"
   strMailTo = objMerge.DataSource.DataFields("Emailaddress")
   strMailCC = objMerge.DataSource.DataFields("CC")

   ' create the document path name
   ' In this case it can be the same for every recipient,
   ' but if you want to retain copies of the
   ' document, you can use info. in the data source

   ' this is an example - insert your
   ' own pathname here

   strOutputDocumentName = "c:\a\results.htm"

   ' strOutputDocumentName = _
   '  "c:\mymergeletters\_" & _
   '  .DataSource.DataFields("Lastname").Value & _
   '  " letter.doc"
   .DataSource.FirstRecord = intSourceRecord
   .DataSource.LastRecord = intSourceRecord
   .Destination = wdSendToNewDocument
   .Execute

   ' The Activedocument is always the
   ' output document

   ' Add any parameters you need to these calls
   ActiveDocument.SaveAs strOutputDocumentName, wdFormatFilteredHTML
   ActiveDocument.Close

   ' Now create a new Mail Item

   Set objMailItem = objOutlook.CreateItem(olMailItem)
   With objMailItem
     .BodyFormat = olFormatHTML
     .Subject = strMailSubject
     .HTMLBody = strMailBody
     .To = strMailTo
     .CC = strMailCC
     .Attachments.Add strOutputDocumentName, olByValue, 1
     .Save
     .Send
    End With
    Set objMailItem = Nothing

   intSourceRecord = intSourceRecord + 1
 End If
Loop
End With

' Close Outlook if appropriate

If bOutlookStarted Then
 objOutlook.Quit
End If

Set objOutlook = Nothing
Set objMerge = Nothing

End Sub

Signature

Peter Jamieson
http://tips.pjmsn.me.uk

>I am running Word 2003 (Outlook 2003 for those interested) and I need
> to programatically save a document to HTML-filtered format then use
> the File>Send To>Mail Recipient to send it off, also programatically.
> Can anyone give me a start on this, particularely on the mail portion?
> TIA
Namgaw - 07 Dec 2007 21:09 GMT
On Dec 7, 8:33 am, "Peter Jamieson" <p...@KillmapSpjjnet.demon.co.uk>
wrote:
> Here's some code that does a "one mailmerge per data source record, saving
> the document in filtered HTML format then using the Outlook object model to
[quoted text clipped - 146 lines]
>
> - Show quoted text -

How about not via an attachment?
Peter Jamieson - 08 Dec 2007 09:32 GMT
Then I guess you would have to set

.HTMLBody

to be the filtered text that you saved. I haven't tried that myself, but you
could :-)

Frankly, I wouldn't hope for too much when trying to build an HTML mail with
the Outlook object model. AFAICS whatever you put in the HTMLBody has to be
converted into a MIME message anyway, and that probably means it's not going
to end up looking the way you expect.

Signature

Peter Jamieson
http://tips.pjmsn.me.uk

> On Dec 7, 8:33 am, "Peter Jamieson" <p...@KillmapSpjjnet.demon.co.uk>
> wrote:
[quoted text clipped - 152 lines]
>
> How about not via an attachment?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.