How to capture programatically emailing a Word 2003 doc?

J

James

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
 
T

Tony Jollans

The easy bit:

ActiveDocument.SaveAs FileName:=whatever, _
FileFormat:=wdFormatFilteredHTML

AFAIK, you cannot completely automate the sending of e-mail through Word but
you can do it with Outlook. I posted this for someone else the other day
and noted that, if Outlook wasn't already running, the Inspector was needed

Dim appOL 'As Outlook.Application
Dim E_Mail 'As Outlook.MailItem
Dim Needed 'As Outlook.Inspector

Set appOL = CreateObject("Outlook.Application")
Set E_Mail = appOL.CreateItem(0) 'olMailItem)
Set Needed = E_Mail.GetInspector
E_Mail.Recipients.Add "(e-mail address removed)"
E_Mail.Subject = "Something meaningful"
E_Mail.Attachments.Add whatever_you_saved_it_as
E_Mail.Send

Set Needed = Nothing
Set E_Mail = Nothing
Set appOL = Nothing

You will get messages from the Object Model Guard about another application
trying to send e-mail; if this turns out to be a problem, investigate
ClickYes (just google for it).
 
J

James

The easy bit:

ActiveDocument.SaveAs FileName:=whatever, _
FileFormat:=wdFormatFilteredHTML

AFAIK, you cannot completely automate the sending of e-mail through Word but
you can do it with Outlook. I posted this for someone else the other day
and noted that, if Outlook wasn't already running, the Inspector was needed

Dim appOL 'As Outlook.Application
Dim E_Mail 'As Outlook.MailItem
Dim Needed 'As Outlook.Inspector

Set appOL = CreateObject("Outlook.Application")
Set E_Mail = appOL.CreateItem(0) 'olMailItem)
Set Needed = E_Mail.GetInspector
E_Mail.Recipients.Add "(e-mail address removed)"
E_Mail.Subject = "Something meaningful"
E_Mail.Attachments.Add whatever_you_saved_it_as
E_Mail.Send

Set Needed = Nothing
Set E_Mail = Nothing
Set appOL = Nothing

You will get messages from the Object Model Guard about another application
trying to send e-mail; if this turns out to be a problem, investigate
ClickYes (just google for it).

--
Enjoy,
Tony







- Show quoted text -

This would send as attachment, no? I need it in the body of the
message.
 
T

Tony Jollans

I don't really know how to do this and there may well be a better way but,
hopefully, this will put you on the right track.

After saving the file as HTML, close it, read it as text and apply it to the
e-mail's HTMLBody property. You may have to make sure Outlook is set up
with HTML format as default - and I don't know how to do that
programmatically. Anyway here's some amended code that may work - let me
know.

FileName = "C:\whatever.htm"

ActiveDocument.SaveAs FileName, wdFormatHTML
ActiveDocument.Close

Set fso = CreateObject("Scripting.FileSystemObject")
Set objHTML = fso.GetFile(FileName).OpenAsTextStream(1, -2)
strHTML = objHTML.ReadAll
objHTML.Close
Set objHTML = Nothing
Set fso = Nothing

Dim appOL 'As Outlook.Application
Dim E_Mail 'As Outlook.MailItem
Dim Needed 'As Outlook.Inspector

Set appOL = CreateObject("Outlook.Application")
Set E_Mail = appOL.CreateItem(0) 'olMailItem)
Set Needed = E_Mail.GetInspector
E_Mail.Recipients.Add "(e-mail address removed)"
E_Mail.Subject = "Something meaningful"
'E_Mail.Attachments.Add whatever_you_saved_it_as
E_Mail.HTMLBody = strHTML
E_Mail.Send

Set Needed = Nothing
Set E_Mail = Nothing
Set appOL = Nothing
 

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