MS Access to send e-mail. How?

L

.Len B

I am trying to use Access to automate the sending of a number
of messages.I have the following code (stolen from MS KB161088 and
modified) which fails with error code -1594867707 (a0f04005) when
executing the .Send method. Googling the hex error gave zero results
and Googling the decimal error yielded runtime errors with differing
numbers, none of which were relevant.

Clicking on Help from the error dialog brings up help on error
440 - Automation.

The text of the error is: Outlook does not recognize one or more
names. Googling this yields a Microsoft support page suggesting
that the address book is corrupt. I can successfully send e-mail
directly from OL and the sole test recipient is my own email
address which does appear in my address book.

Dim objOut As Outlook.Application
Dim objOutMsg As Outlook.MailItem
Dim strRecipient As String, strSubject As String
Dim strBody As String
Dim blnDisplayMsg As Boolean

blnDisplayMsg = Me.chkDisplay 'do we display the email
first
Set objOut = CreateObject("Outlook.Application") 'Create Outlook session
....
Set objOutMsg = objOut.CreateItem(olMailItem) 'create Outlook message
With objOutMsg
.recipients.Add strRecipient 'To field
.subject = strSubject 'Subject field
.body = strBody 'Body field
If blnDisplayMsg Then 'display before sending
.Display
Else 'just send it
.Save
===> .Send
End If
End With 'done with this message
'erase just sent msg from memory
Set objOutMsg = Nothing

If I send the logic through the .Display method instead, OL fires
and the fields are correctly populated. Clicking on the [Send]
button has the expected result. However, requiring the user to
click send on multiple messages somewhat defeats the purpose.

I hope I am asking in the right forum.
Where to from here?
 
K

Ken Slovak - [MVP - Outlook]

Try a few things. Declare and instantiate a NameSpace object:

Dim oNS As Outlook.NameSpace
Set oNs = objOut.GetNameSpace("MAPI")
oNS.Logon "", "", False, False

Recipients.Add is a function. Declare a Recipient object and assign that
from the return of Recipients.Add. Call Recipient.Resolve and then check
Recipient.Resolved after that. Or you can call Recipients.ResolveAll and
check Recipient.Resolved after that.

You don't mention your Outlook version, so it's impossible to say whether
you also will hit the Outlook object model guard once you get past your
current problem. Take a look at
http://www.outlookcode.com/article.aspx?id=52 for information on that.

Finally, this isn't really a programming group although the group name is
deceptive. A better group would be the program_vba Outlook group.




.Len B said:
I am trying to use Access to automate the sending of a number
of messages.I have the following code (stolen from MS KB161088 and
modified) which fails with error code -1594867707 (a0f04005) when
executing the .Send method. Googling the hex error gave zero results
and Googling the decimal error yielded runtime errors with differing
numbers, none of which were relevant.

Clicking on Help from the error dialog brings up help on error
440 - Automation.

The text of the error is: Outlook does not recognize one or more
names. Googling this yields a Microsoft support page suggesting
that the address book is corrupt. I can successfully send e-mail
directly from OL and the sole test recipient is my own email
address which does appear in my address book.

Dim objOut As Outlook.Application
Dim objOutMsg As Outlook.MailItem
Dim strRecipient As String, strSubject As String
Dim strBody As String
Dim blnDisplayMsg As Boolean

blnDisplayMsg = Me.chkDisplay 'do we display the email
first
Set objOut = CreateObject("Outlook.Application") 'Create Outlook session
...
Set objOutMsg = objOut.CreateItem(olMailItem) 'create Outlook message
With objOutMsg
.recipients.Add strRecipient 'To field
.subject = strSubject 'Subject field
.body = strBody 'Body field
If blnDisplayMsg Then 'display before sending
.Display
Else 'just send it
.Save
===> .Send
End If
End With 'done with this message
'erase just sent msg from memory
Set objOutMsg = Nothing

If I send the logic through the .Display method instead, OL fires
and the fields are correctly populated. Clicking on the [Send]
button has the expected result. However, requiring the user to
click send on multiple messages somewhat defeats the purpose.

I hope I am asking in the right forum.
Where to from here?
 
L

.Len B

Thanks for that Ken.
My Outlook version is 2003 using POP3 through my ISP for Development.
Target Outlook version is 2003 using Exchange Server. If that prompts
any comments/warnings, I'd appreciate knowing any gotchas to look for.

I'll try program_vba Outlook group as you suggest but FYI here's the
outcome of your suggestions.

I added the namespace code. It still gives "Outlook does not recognize"
errors for each message but the error numbers differ for each message.
Oddly, each message has the same 5 final hex digits - 04005.

Your comments concerning Recipients.Resolve(All) and .Resolved seem to
imply that the recipients should all be already listed in the address
book. This may not be the case; in fact it is likely that few will be
listed. Does this mean they need to be added up front? I haven't tried
that suggestion yet.

I had already hit the problem of the Outlook object model guard but I
didn't mention it because it is still minor in the scheme of things.
Thanks for the link. I wouldn't have known what to search for. It sure
is a pain during development because it continually steals the focus
when single stepping. (I have the code in a separate monitor and I don't
always notice an unexpected popup on the primary screen.)
--
Len
______________________________________________________
remove nothing for valid email address.
| Try a few things. Declare and instantiate a NameSpace object:
|
| Dim oNS As Outlook.NameSpace
| Set oNs = objOut.GetNameSpace("MAPI")
| oNS.Logon "", "", False, False
|
| Recipients.Add is a function. Declare a Recipient object and assign
that
| from the return of Recipients.Add. Call Recipient.Resolve and then
check
| Recipient.Resolved after that. Or you can call Recipients.ResolveAll
and
| check Recipient.Resolved after that.
|
| You don't mention your Outlook version, so it's impossible to say
whether
| you also will hit the Outlook object model guard once you get past your
| current problem. Take a look at
| http://www.outlookcode.com/article.aspx?id=52 for information on that.
|
| Finally, this isn't really a programming group although the group name
is
| deceptive. A better group would be the program_vba Outlook group.
|
| --
| Ken Slovak
| [MVP - Outlook]
| http://www.slovaktech.com
| Author: Professional Programming Outlook 2007.
| Reminder Manager, Extended Reminders, Attachment Options.
| http://www.slovaktech.com/products.htm
|
|
| | >I am trying to use Access to automate the sending of a number
| > of messages.I have the following code (stolen from MS KB161088 and
| > modified) which fails with error code -1594867707 (a0f04005) when
| > executing the .Send method. Googling the hex error gave zero results
| > and Googling the decimal error yielded runtime errors with differing
| > numbers, none of which were relevant.
| >
| > Clicking on Help from the error dialog brings up help on error
| > 440 - Automation.
| >
| > The text of the error is: Outlook does not recognize one or more
| > names. Googling this yields a Microsoft support page suggesting
| > that the address book is corrupt. I can successfully send e-mail
| > directly from OL and the sole test recipient is my own email
| > address which does appear in my address book.
| >
| > Dim objOut As Outlook.Application
| > Dim objOutMsg As Outlook.MailItem
| > Dim strRecipient As String, strSubject As String
| > Dim strBody As String
| > Dim blnDisplayMsg As Boolean
| >
| > blnDisplayMsg = Me.chkDisplay 'do we display the email
| > first
| > Set objOut = CreateObject("Outlook.Application") 'Create Outlook
session
| > ...
| > Set objOutMsg = objOut.CreateItem(olMailItem) 'create Outlook
message
| > With objOutMsg
| > .recipients.Add strRecipient 'To field
| > .subject = strSubject 'Subject field
| > .body = strBody 'Body field
| > If blnDisplayMsg Then 'display before sending
| > .Display
| > Else 'just send it
| > .Save
| > ===> .Send
| > End If
| > End With 'done with this message
| > 'erase just sent msg from memory
| > Set objOutMsg = Nothing
| >
| > If I send the logic through the .Display method instead, OL fires
| > and the fields are correctly populated. Clicking on the [Send]
| > button has the expected result. However, requiring the user to
| > click send on multiple messages somewhat defeats the purpose.
| >
| > I hope I am asking in the right forum.
| > Where to from here?
| >
| > --
| > Len
 
K

Ken Slovak - [MVP - Outlook]

Recipients do not have to be in the address book or any contacts folder to
resolve. It's a matter of format, not actual address. However, with Exchange
it often seems that emails populated in code might not send unless the
recipient addresses are resolved, the exact reason I suggested that.
 

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