Work with RTF and Word


We often send emails with RTF and Word document as attachments by ANSMTP. We can use AddAttachment method to attach RTF and Word document to message very easily. But here we will discuss how to convert RTF or Word document to html file and send the html email to end users without attachments.

First of all, we need to use Word Object(Please make sure Microsoft Word is installed on your machine) to convert the RTF or Doc to html file, secondly we can use ImportMailEx method to import html and embedded pictures to the message. The following vbscript demonstrates how to convert *.rtf to html and send message out.

On Error Resume Next

Dim oWordApp, oWordDocuments, oWordDoc, oConverters, oConverter
Dim source, target
source = "c:\document.rtf"
target = "c:\document.htm"

Err.Clear
Set oWordApp = CreateObject("Word.Application")
If Err.Number <> 0 Then
	WScript.Echo Err.Description
	WScript.Quit()
End If

oWordApp.Visible = False
Set oWordDocuments = oWordApp.Documents

Const wdFormatHTML = 8

Err.Clear
Set oWordDoc = oWordDocuments.Open( source )
If Err.Number <> 0 Then
	WScript.Echo Err.Description
	oWordApp.Quit()
	WScript.Quit()
End If

Err.Clear
oWordDoc.SaveAs target, wdFormatHTML     

oWordDoc.Close() 
oWordApp.Quit()

If Err.Number <> 0 Then
	WScript.Echo Err.Description
	WScript.Quit()
End If

Dim oSmtp
Set oSmtp = CreateObject("AOSMTP.Mail")
oSmtp.FromAddr = "test@mydomain.com"
oSmtp.ServerAddr = "myserver"

oSmtp.FromAddr = "test@mydomain.com"
oSmtp.AddRecipient "tester", "test@mydomain.com", 0

oSmtp.Subject = "Test"
oSmtp.BodyFormat = 1
If oSmtp.ImportMailEx( target ) <> 0 Then
	WScript.Echo "import mail failed" 
	WScript.Quit()
End If

If oSmtp.SendMail() <> 0 Then
	WScript.Echo oSmtp.GetLastErrDescription()
Else
	WScript.Echo "Message delivered"
End If

Note*: if you use Word Object in ASP/ASP.NET application, there are some issues with it, more detail, please refer to http://support.microsoft.com/kb/257757.


2001-2007 © Copyright AdminSystem Software Limited. All rights reserved.