P
Peter Hibbs
Access 2003 SP3 + Outlook 2003
I am using the following function kindly supplied by Tom Wickerath a
couple of years ago.
--------------------------------------------------------------------------------
Public Function SendMail(strRecipients As String, strSubject As
String, strBody As String) As String
'Written by Tom Wickerath, May 7, 2006.
'Entry (strRecipients) = Semicolon delimited string of recipients.
' (strSubject) = Required. Message subject.
' (strBody) = Optional. Body of the message.
Dim myObject As Object, myItem As Object
Dim vCount As Long
On Error GoTo ProcError
Set myObject = CreateObject("Outlook.Application")
Set myItem = myObject.CreateItem(0)
With myItem
.Subject = strSubject
.To = strRecipients
If Len(Trim(strBody)) > 0 Then
.Body = strBody
End If
.Display
End With
ExitProc:
Set myItem = Nothing
Set myObject = Nothing
Exit Function
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in SendMail Function..."
SendMail = "A problem was encountered attempting to automate
Outlook."
Resume ExitProc
Resume
End Function
--------------------------------------------------------------------------------
I am calling it like this from a button on a form :-
--------------------------------------------------------------------------------
Private Sub btnEMail_Click()
Dim vDummy As String
vDummy = SendMail("EMail Address here", "Subject", "Test")
End Sub
--------------------------------------------------------------------------------
The "EMail Address here" string is a valid email address.
The problem is that when I run the code, Outlook opens correctly
showing the email address, subject and body text but if I then Send
the email (or cancel the email by exiting Outlook) I get the following
error message on the .Display line.
Run-time error '-2147417851 (80010105) ':
Method 'Display' of object '_MailItem.' failed
Also Outlook is left running in the background, I have to close it
using Windows Task Manager.
The code Compiles with no errors and Compact & Repair and Decompiling
make no difference. I'm sure this worked OK a while ago and I think
the same problem occurs in Access 2000 (I'm still checking that one as
Outlook 2003 is being used as the email client). References look OK,
Outlook 11.0 Object Library is ticked although I don't think it is
necessary for this code.
Has anyone else come across the problem.
TIA
Peter Hibbs.
I am using the following function kindly supplied by Tom Wickerath a
couple of years ago.
--------------------------------------------------------------------------------
Public Function SendMail(strRecipients As String, strSubject As
String, strBody As String) As String
'Written by Tom Wickerath, May 7, 2006.
'Entry (strRecipients) = Semicolon delimited string of recipients.
' (strSubject) = Required. Message subject.
' (strBody) = Optional. Body of the message.
Dim myObject As Object, myItem As Object
Dim vCount As Long
On Error GoTo ProcError
Set myObject = CreateObject("Outlook.Application")
Set myItem = myObject.CreateItem(0)
With myItem
.Subject = strSubject
.To = strRecipients
If Len(Trim(strBody)) > 0 Then
.Body = strBody
End If
.Display
End With
ExitProc:
Set myItem = Nothing
Set myObject = Nothing
Exit Function
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in SendMail Function..."
SendMail = "A problem was encountered attempting to automate
Outlook."
Resume ExitProc
Resume
End Function
--------------------------------------------------------------------------------
I am calling it like this from a button on a form :-
--------------------------------------------------------------------------------
Private Sub btnEMail_Click()
Dim vDummy As String
vDummy = SendMail("EMail Address here", "Subject", "Test")
End Sub
--------------------------------------------------------------------------------
The "EMail Address here" string is a valid email address.
The problem is that when I run the code, Outlook opens correctly
showing the email address, subject and body text but if I then Send
the email (or cancel the email by exiting Outlook) I get the following
error message on the .Display line.
Run-time error '-2147417851 (80010105) ':
Method 'Display' of object '_MailItem.' failed
Also Outlook is left running in the background, I have to close it
using Windows Task Manager.
The code Compiles with no errors and Compact & Repair and Decompiling
make no difference. I'm sure this worked OK a while ago and I think
the same problem occurs in Access 2000 (I'm still checking that one as
Outlook 2003 is being used as the email client). References look OK,
Outlook 11.0 Object Library is ticked although I don't think it is
necessary for this code.
Has anyone else come across the problem.
TIA
Peter Hibbs.