Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with outlook message from access

Status
Not open for further replies.

idono

Technical User
Jan 16, 2002
71
US
I have an Access application that I want to send an email with. I can get it to work but only if outlook is already open. If it is not I get an internal application error. Below is the code:

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim txtRecipient As String
Dim stappname As String
Dim myNameSpace

On Error GoTo HandleErr

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
'Set myNameSpace = objOutlook.GetNamespace("MAPI")

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

On the last line is where I get the error. I am curious if this is a system setting or a missed reference. Can anyone shed some light on this subject.

Thanks,
Doug
 
Does olMailItem have the proper value when you test it in the immediate window?
Rob
[flowerface]
 
I'm not sure what the proper value would be, but it is set to 0.
 
Yeah, that is the proper value. My code for doing the same is:
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set mi = ol.CreateItem(olMailItem)

In other words, identical besides the different way of creating the application object. You may want to experiment with both forms.
Rob
[flowerface]
 
Ok, get this. If I cycle through the code using F8 when I get to Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
if I leave the yellow highlight on it for a while it will work. Ever heard of this?
 
The last comment I made is not the true issue. The problem is in the namespace statement. When stepping through the code I can stop on the olmailitem statement with a breakpoint then click in the code on the namespace statement and the cpu processes for a bit and the message will send. Its starting something but I must activate it from the debugger. Does that make any sense?
 
I got it to work, but it requires that I activate the namespace object somehow. Below is what I did.

' Create the Outlook session.
Set objOutlook = New Outlook.Application 'CreateObject("Outlook.Application")
Set myNameSpace = objOutlook.GetNamespace("MAPI")

Debug.Print myNameSpace
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

The debug.print statement was enough to active the namespace. If anyone can explain this I will be glad to listen.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top