Email

E

Ernst Guckel

Hello,

We are having a bit of dificulty with the following code. It works when
Ourlook is open but not when closed. We recieve an 'Application defined'
error on the line MyMail.Send... References to Outlook are checked. Any help
would be great.

Public Sub SendEMail(strMailTo As String, strFile As String, _
strSubject As String, Optional strFileDesc As String, Optional strBody
As String, _
Optional strBCC As String)

Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim strTag As String
Dim strName As String
Dim strEmail As String

strTag = DLookup("Tag", TABLE_ORGDATA)
strName = DLookup("OrganizationName", TABLE_ORGDATA)
strEmail = DLookup("Email", TABLE_ORGDATA)

' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application

'This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)

' This addresses it
MyMail.To = strTag

'This addresses BCC
MyMail.BCC = strBCC
'MsgBox strBCC

'This gives it a subject
MyMail.Subject = strSubject

'This gives it the body
MyMail.Body = strBody

'This attaches a file
If strFile <> "" Then
MyMail.Attachments.Add strFile, olByValue, 1, strFileDesc
End If

'This sends it!
MyMail.Send

'Cleanup after ourselves
Set MyMail = Nothing

End Sub
 
D

David H

What do you mean by Outlook being opened? Office automation is such that the
application need never be opened to work.

What happens when you replace .send with .display? Is the MailItem always
displayed? Do you get the same error?
 
R

ryguy7272

This will force Outlook's MailItem to open...

Private Sub cmdOutlook_Click()
On Error GoTo StartError
Dim objOutlook As Object
Dim objItem As Object
'Create a Microsoft Outlook object.
Set objOutlook = CreateObject("Outlook.Application")
'Create and open a new contact form for input.
Set objItem = objOutlook.CreateItem(olMailItem)

objItem.Display
'Quit Microsoft Outlook.
Set objOutlook = Nothing

Exit Sub
StartError:
MsgBox "Error: " & Err & " " & Error
Exit Sub

End Sub

HTH,
Ryan---
 
E

Ernst Guckel

by open I mean the Outlook application is open. The mail item is created
fine. If I close Outlook i recieve the error.
 
D

David H

If you do not have Outlook open AT ALL, What happens? An MS Office
application does not have to be open for automation (the controlling of an
application from another) to work.

I did notice that you're not explicitly destroying the MyOutlook object as
in Set MyOutlook = Nothing. When you're working with automation if you don't
destroy the object, you can end up with the multiple instances of the process
running which might be the problem. At any rate, it isn't desireable. Be
certain to add in error handling to ensure that if an error occurs, that the
object variables are destroyed.

(Always close what you open, always destroy what you create.)
 
E

Ernst Guckel

I understand that. I understand Office automation. What I am saying is that
the email never gets sent. Access gives me an error 'Application defined' at
the line MyEmail.Send. If I open Outlook prior to running the code then it
works fine. Hense the problem I should not have to open Outlook. ...

Ernst.
 
D

David H

Precisely.

1) Have you done what I mentioned in my prior posts about explicitly
destroying the application object?

2) What happens when you do not open Outlook at all? Does it still work or
does it crap out?

3) Do you see the email sitting in the Outbox waiting to be sent?

4) Although you want it to be sent, do you still get the same error when you
substitute .Display for .Send?

The idea behind all of this is to try and narrow down what the issue is.
 
E

Ernst Guckel

Ok...
1) Have you done what I mentioned in my prior posts about explicitly
destroying the application object?

It has the same effect. Still get the error.
2) What happens when you do not open Outlook at all? Does it still work or
does it crap out?

I get the error.
3) Do you see the email sitting in the Outbox waiting to be sent?
No.

4) Although you want it to be sent, do you still get the same error when you
substitute .Display for .Send?

If i use Display instead of send i get the New email' window like expected.
If I then click on send it sends fine. Weird??


Any other ideas?
Thanks for the help

Ernst.
 

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