Link to home
Start Free TrialLog in
Avatar of redpoppy
redpoppy

asked on

Outlook Automation Error

MS Access 2010, Outlook 2003, Windows Server 2003
Hi Experts,
I'm using Outlook Automation to output e-mails from our Ms Access database (running in .mdb mode) - emails are sent to the drafts box rather than being sent straight away.
Users access the d/base via Terminal Services, we have a main Outlook PST file on the server shared using Outlook Folders. Most of the time this works well but just recently we've been having a problem when trying to set a refernce to the drafts folder. Here is an extract from the routine:
-----------------------------------------------------------
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objNameSpace As Outlook.NameSpace
Dim objDestFolder As Object
270   Set objOutlook = CreateObject("Outlook.Application", "localhost")
      ' store drafts folder reference
280   Set objNameSpace = objOutlook.GetNamespace("MAPI")
290   Set objDestFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)
-----------------------------------------------------

The failure is happening at line 290, the error message is "The object requested was not found" with error number -2044460783. The error is intermittent - most of the time it's fine.
Can anyone help with trying to sort this out?
Avatar of Barry Cunney
Barry Cunney
Flag of Ireland image

Check your Exchange Server logs to see if the mail server is going off line for a few seconds or something like that which means the Drafts folder is not available intermiitently.

You could maybe put error handling in the routine so that if there is an error doing the
Set objDestFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)
it does a Resume and tries again
Avatar of redpoppy
redpoppy

ASKER

Hi bcunney, thanks for your reply. If I put some extra error trapping in the routine, could I check for "Err.Number = -2044460783"? It seems a bit of a strange error number and I can't find any info about it.
Avatar of Helen Feddema
That is one of those phony error numbers that sometimes appear.  The number won't be the same every time, so it is no use checking for it.  Instead, try something like the technique in this code, which creates a new folder if not found:

Public Sub CreateOLFolder()

On Error Resume Next

   Dim appOutlook As New Outlook.Application
   Dim fldProcessed As Outlook.Folder
   Dim fldInbox As Outlook.Folder
   Dim nms As Outlook.NameSpace
   
   Set nms = appOutlook.GetNamespace("MAPI")
   Set fldInbox = nms.GetDefaultFolder(olFolderInbox)
   Set fldProcessed = fldInbox.Folders("Processed Web Data Messages")
   If fldProcessed Is Nothing Then
      Set fldProcessed = fldInbox.Folders.Add("Processed Web Data Messages")
   End If
   
End Sub

Open in new window


Instead of adding a folder, as in the sample code, you could just retry setting the reference, if needed creating a loop to try over and over again until the reference is set correctly.
You can then turn the error handler back on, after the reference is set.
redpoppy,

Can you post your full code please?

If the emails are being saved to the "Drafts" folder, and not being sent immediately, then your command to send the email is probably just:
   YourOutlookEmailMessageObject.Save

It needs to be:

'Saves the email temporarily if .Send is issued immediately afterward.
' If the line below exists by itself, the email stays in the Drafts folder,
' (and is not sent).
 YourOutlookEmailMessageObject.Save
'Sends the email out immediately
 YourOutlookEmailMessageObject.Send


You can see this ubiquitous code for reference:
http://support.microsoft.com/kb/161088

;-)

JeffCoachman
Helen - thanks for your reply. I'd favour trying to re-set the reference a couple of times - but I don't understand how I could trap the error if the error number is garbage - is there a way of doing that?

Jeff - thanks for your reply. Here is the piece of code where the generated e-mail is either stored in the drafts folder or sent immediately depending upon the value of the parameter "draft":
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objNameSpace As Outlook.NameSpace
Dim objDestFolder As Object
Set objOutlook = CreateObject("Outlook.Application", "localhost")
      ' store drafts folder reference
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objDestFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
      If Not draft Then
            .send
             ebox = "Outbox"
     Else
             .Move objDestFolder
             ebox = "Drafts"
    End If
End With

Open in new window

Is that the entire code?
I don't see where you are declaring "ebox" or "Drafts"

In any event, my post above illustrate the issue.

If you just have:
.save
    ...the email is stored in drafts and not sent.

If you have
.save
.send
...the email is sent immediately.

I cannot vouch for the code you are using there, only that the code I posted works.
(Sends the email immediately, no need to specify any folders in code.)

Again, examine the code in the link for a full example...

;-)

JeffCoachman
Jeff -
I understand the situation under which the e-mails will be sent immediately but my problem is that I want them saved into the "drafts" folder and NOT sent immediately.

I didn't post the whole routine, just the relevant section. The error is happening on this line:
Set objDestFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)
This line is executed when the "draft" parameter is set to true.
...Then I apologize for not understanding...

..then you can reverse what I posted and use this to save the emails to Drafts:

.save


Now in the case that I am still not understanding, ...you can continue with the other experts(s).
;-)

Sorry if I confused the issue.

JeffCoachman
Since your issue is random it is not going to be the code so much as something to do with the infrastructure.  One possibility is to switch to late binding for example replace the constants:

olFolderDrafts with 16
olMailItem with 0

I'm not saying it WILL help but it will not hurt and may change things!  If it does in fact change things then there is a lot more to be to done to address all similar issues but for now see if it helps with line 290 error!

Chris

Chris
Chris - thanks for that, I'll give it a try. It might be a few days before I can report back, depending upon whether or not the error happens.
Hi Chris, I've tried that code out but the error is still happening. It seems to be when there's a sequence of e-mails to be sent, e.g: a weekly report sent to about 50 recipients - it seems OK for the first 10 or so then falls over on the same line " Set objDestFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)"
I'm just wondering if it's possible to set up "objDestFolder" and "objNameSpace" in global variables and then just allocate them once when the database opens, or perhaps do a check to see if they contain a value before setting them?
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Chris - thanks for that - I'll give it a try and get back to you....
You could also drop the message:

static objOutlook As Object
'Dim objOutlookMsg As Outlook.MailItem
Static objNameSpace As Object
static objDestFolder As Object

if objOutlook is nothing then _
    Set objOutlook = CreateObject("Outlook.Application", "localhost")
      ' store drafts folder reference
if objNameSpace is nothing then _
    Set objNameSpace = objOutlook.GetNamespace("MAPI")
if objDestFolder is nothing then _
    Set objDestFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)
with objOutlook.CreateItem(olMailItem)
      If Not draft Then
            .send
             ebox = "Outbox"
     Else
             .Move objDestFolder
             ebox = "Drafts"
    End If
End With

Open in new window


Chris
Hi Chris - that seems to have cracked it! It's also speeded up sending the emails when they're being sent in bulk, so that's an added bonus. Thanks for your help, much appreciated