problem sending email with vba

  • Thread starter moshe via AccessMonster.com
  • Start date
M

moshe via AccessMonster.com

I have a form and when the user enters new information I want to send them an
email.
I tried several ways to create the email and finely got the best of them, but
if outlook is closed I get an error. so I added code the shell function
should open outlook but I don't want outlook to get the focus? "shell
"outlook.exe",vbhide" don't seems to working it always opens in normal window.
also if outlook is already open I don't want it to be opened twice, or it
should close after it sends the email. so by that way outlook will not stay
open twice, I tried to close it down by using sendkeys but it closed my
database instead of outlook, or if someone could tell me how to send the
email without outlook open would be great, my code is as follow all comments
greatly appreciated.

Public Sub SendMessage(DisplayMsg As Boolean)
 Dim  objOutlook As Outlook.Application
 Dim objOutlookMsg As  Outlook.MailItem
 
  ' Create the Outlook session.
 Set  objOutlook =CreateObject("Outlook.Application")
 shell  "outlook.exe", vbHide 'this line I added to the samples
 ' Create the  message.
 Set objOutlookMsg =  objOutlook.CreateItem(olMailItem)
 With  objOutlookMsg
 .To = "moshe"
 .Subject = "This is an Automation test with Microsoft  Outlook"
 .body = "This is the body of  the message." & vbCrLf & vbCrLf
 .Importance = olImportanceHigh 'High  importance
 .Send 'here is where I get the error
 End With
 Set objOutlook = Nothing
 
 End Sub
 
B

Baz

You don't need to shell Outlook in order to automate it. What error were
you getting that made you think you needed to do that?

Since you are early-binding Outlook, CreateObject is not appropriate, this
would be better:

Set objOutlook = New Outlook.Application

You don't say what version of Outlook you are using, but I'm guessing that
it's earlier than Outlook 2002. From Outlook 2002 onwards, your users will
be bombarded with Outlook security warnings which you cannot turn off.
There are much better ways of sending email, such as this:

Dim CDOMessage As Object
Dim CDOConf As Object
Dim CDOFlds As Object


Set CDOConf = CreateObject("CDO.Configuration")
Set CDOFlds = CDOConf.Fields
CDOFlds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
CDOFlds("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "my
SMTP server name"
CDOFlds("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
25
CDOFlds.Update
Set CDOMessage = CreateObject("CDO.Message")
Set CDOMessage.Configuration = CDOConf
With CDOMessage
.From = strFrom
.To = strEmail
.Subject = "TEST TEST Please Delete TEST"
.TextBody = strBody
.Send
End With
 
M

moshe via AccessMonster.com

thanks baz
You don't need to shell Outlook in order to automate it. What error were
you getting that made you think you needed to do that? i got err: 387

Since you are early-binding Outlook, CreateObject is not appropriate, this
would be better:

Set objOutlook = New Outlook.Application and i still got the error

You don't say what version of Outlook you are using, but I'm guessing that
it's earlier than Outlook 2002. From Outlook 2002 onwards, your users will
be bombarded with Outlook security warnings which you cannot turn off.
I'm using access 2007 and outlook 2007 but i didn't got any warning i
wondered that everybody is writing about the annoying messages
There are much better ways of sending email, such as this:

Dim CDOMessage As Object
Dim CDOConf As Object
Dim CDOFlds As Object

Set CDOConf = CreateObject("CDO.Configuration")
Set CDOFlds = CDOConf.Fields
CDOFlds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
CDOFlds("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "my
SMTP server name"
CDOFlds("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
25
CDOFlds.Update
Set CDOMessage = CreateObject("CDO.Message")
Set CDOMessage.Configuration = CDOConf
With CDOMessage
.From = strFrom
.To = strEmail
.Subject = "TEST TEST Please Delete TEST"
.TextBody = strBody
.Send
End With
to other viewers before you use this code take a look at:
http://www.paulsadowski.com/WSH/cdo.htm
he covers fully that way of sending emails so you shouldn't have any problem
I have a form and when the user enters new information I want to send them an
email.
[quoted text clipped - 28 lines]
 
B

Baz

moshe via AccessMonster.com said:
thanks baz

i got err: 387

The message would be helpful, strangely enough I have never quite managed to
memorise all the possible error numbers and their meanings!
and i still got the error

I didn't say it would get rid of the error (how could I when I didn't know
what the error was?), I was just telling you the correct way to do it.
I'm using access 2007 and outlook 2007 but i didn't got any warning i
wondered that everybody is writing about the annoying messages

OK, Outlook 2007 is a bit smarter than 2002 and 2003, under suitable
circumstances it doesn't throw the warnings.
to other viewers before you use this code take a look at:
http://www.paulsadowski.com/WSH/cdo.htm
he covers fully that way of sending emails so you shouldn't have any
problem

Whatever. The code works just fine as I posted it.
 
M

moshe via AccessMonster.com

Baz said:
The message would be helpful, strangely enough I have never quite managed to
memorise all the possible error numbers and their meanings!
Sorry It's error "287" Description "Application-defined or Object-defined
error".
and i still got the error

I didn't say it would get rid of the error (how could I when I didn't know
what the error was?), I was just telling you the correct way to do it.
I'm using access 2007 and outlook 2007 but i didn't got any warning i
wondered that everybody is writing about the annoying messages

OK, Outlook 2007 is a bit smarter than 2002 and 2003, under suitable
circumstances it doesn't throw the warnings.
[quoted text clipped - 23 lines]
http://www.paulsadowski.com/WSH/cdo.htm
he covers fully that way of sending emails so you shouldn't have any problem

Whatever. The code works just fine as I posted it.
But it will only work if the smtp server don't require authentication. and
SSL but I'm using Gmail. so i Needed to enter my user name and password and
that ssl is required.

Anyway Thanks again baz for giving me this code. because without it I would
never got even close to the right way to send emails. after getting your code
i got an error MSG can not sign in to the server so i searched Google with
part of the code and i got there the way to send emails from a secure server
like Google.
 

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

Similar Threads


Top