VB code for sending mail with Outlook 97 and 2002

M

Mohammad Nurullah

Hello,

I have a VB application which uses outlook object for
sending mails. The piece of code below is running fine
with MS Outlook 97 but same set of code is not working
with MS Outlook 2002. The problem is the contents of
template file which appears in Outlook 97 does not appear
in Outlook 2002. In the code below, Mail.oft is a simple
template while 8000.doc contains some message. If this
code is executed on a machine having Outlook 97, a mail
with text from 8000.doc is saved in Inbox but when I
execute on a machine having Outlook 2002, a mail with
blank message tetx is saved.

-----------------------------------------------------------
-----------------------------------------------------------
-------------
Sub main()
Call CreateNewMailMessage
End Sub
Public Function CreateNewMailMessage()

Dim MailApp As New Outlook.Application
Dim Message As Outlook.MailItem
Dim sMessageClass As String
Dim sSentOnBehalfOf As String
Dim sSentTo As String
Dim I As Integer
Dim lErrNum As Long
Dim bOutlookCreated As Boolean

On Error Resume Next

Set MailApp = GetObject(, "Outlook.Application")
lErrNum = Err.Number
On Error GoTo CreateNewMailMessageError
If lErrNum <> 0 Then
Set MailApp = CreateObject("Outlook.Application")
bOutlookCreated = True
End If

Set Message = MailApp.CreateItemFromTemplate
("c:\Templates\Mail.oft")

With Message

.FormDescription.Template = "c:\Templates\8000.doc"
sMessageClass = .MessageClass
.GetInspector.HideFormPage ("P.2")
.MessageClass = sMessageClass
.Save
.SentOnBehalfOfName = "(e-mail address removed)"
.To = "(e-mail address removed)"
Set CreateNewMailMessage = Message
.Display


End With

If bOutlookCreated Then
MailApp.Quit
Set MailApp = Nothing
End If

Exit Function

CreateNewMailMessageError:

MsgBox Err.Description & " in CreateNewMailMessage(). ",
vbCritical, "Utils"
If Not MailApp Is Nothing Then
If Not Message Is Nothing Then
Message.Close olDiscard
Message.Delete
Set Message = Nothing
End If
If bOutlookCreated Then
MailApp.Quit
Set MailApp = Nothing
End If
End If

End Function
 

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