intermittent problem with outlook automation (1 Viewer)

joebater

Registered User.
Local time
Today, 17:54
Joined
Aug 19, 2002
Messages
25
Hi there

I've written a fairly basic outlook automation function to create an email containing the text of a textbox to a contact (held in another table). Intermitently (i.e. when my boos came over for a demonstration) I get errors on the line :
With out.CreateItem(olMailItem)
telling me there is an internal application error. Can anyone spot anything obviously wrong in this?

Very many thanks in advance...
blue skies
j.

'create a reference to an object and assign the object variable
'to an Outlook application, send the question and kill the outlook
'application
Dim out As Object
Set out = CreateObject("Outlook.Application")

With out.CreateItem(olMailItem)
.Recipients.Add (recContact("ContactEmail"))
'delete my company name
.Subject = "Question from ********** UK Ltd"
txtQuestion.SetFocus
.Body = txtQuestion.Text
.Send
End With
Set out = Nothing
MsgBox "Sent", vbOKOnly + vbInformation, "New Question"
 

Travis

Registered User.
Local time
Today, 09:54
Joined
Dec 17, 1999
Messages
1,332
You can create an Item and use the With at the same time. Create the MailItem first and use the With on the MailItem Object

Code:
Dim out As Object 
Dim outmailItem as Object
Set out = CreateObject("Outlook.Application") 
Set outmailItem = out.CreateItem(olMailItem) 

With outmailItem
  .Recipients.Add (recContact("ContactEmail")) 
  'delete my company name 
  .Subject = "Question from ********** UK Ltd" 
  txtQuestion.SetFocus 
  .Body = txtQuestion.Text 
  .Send 
End With 
Set outmailItem = Nothing
Set out = Nothing 

MsgBox "Sent", vbOKOnly + vbInformation, "New Question"
 

joebater

Registered User.
Local time
Today, 17:54
Joined
Aug 19, 2002
Messages
25
Hi Travis

Thanks for your response. The changes certainly make the whole thing work faster which surely means that the problems are resolved.

But.... (there's always one!)
I don't understand what the difference means. Is there another thread or explanation I can look through?

thanks for your help.

blue skies
j.
 

Travis

Registered User.
Local time
Today, 09:54
Joined
Dec 17, 1999
Messages
1,332
Trying to use the With statement in the way that you originally had it (if it worked) would be like saying the following.

Dim out As Object

'Create an Instance of Outlook
Set out = CreateObject("Outlook.Application")

'Create an Instance of an Outlook Mail Item
With out.CreateItem(olMailItem)

'Create an Instance of an Outlook Mail Item and Add Recipients
.Recipients.Add (recContact("ContactEmail"))

'Create an Instance of an Outlook Mail Item and change the subject
'delete my company name
.Subject = "Question from ********** UK Ltd"

'Set the focus to the txtQuestion Control (which you don't need to do, just leave the .Text off of the Reference
txtQuestion.SetFocus

'Create an Instance of an Outlook Mail Item and set the Body of the Email
.Body = txtQuestion.Text

'Create an Instance of an Outlook Mail Item and send it
.Send
End With

'Destroy the instance of Outlook
Set out = Nothing

MsgBox "Sent", vbOKOnly + vbInformation, "New Question"

This would create 5 instances of an Email Object. They would all be destroyed once Outlook is destroyed.

It is best to Create a Variable to hold the Instance of an Object.
 

Users who are viewing this thread

Top Bottom