E-Mail Fails to Send (1 Viewer)

1268

Registered User.
Local time
Yesterday, 22:02
Joined
Oct 11, 2012
Messages
44
What I am doing:
1. Automated Process Kicks off 8 p.m. every night
2. Process concludes by sending out and e-mail with attachment.

What is happening:
1. If I cycle thought no issues
2. If I manually kick off the process no issue
3. Each night the entire process runs except the e-mail
4. Receiving error code 91 (object or with not set) on that module
5. I have 2 databases and both fail for the same reason

I don't believe it is a passed variable issue as the file is found updated in the correct spot and the myname is a variable passed through several module which if in error would cause issue much further upstream.

My suspicion is that it is outlook permission related with something to do with waking from sleep but my google skills are failing me. Plan to re-dim proc after each possible failure to find which item is failing specifically but holding out hope it is simple and I am stupid.

Public Function send_e_mail(file As String, myname)
Dim errored As String
Dim proc As String
Dim Report As String
proc = "sendemail"

Sleep 1000
Dim oApp As Outlook.Application
Dim oMail As MailItem

On Error GoTo failure

Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)

oMail.HTMLBody = "Daily Orders & Routing is updated and attached."
oMail.Subject = "Daily Orders has been updated for" & " " & Now()

With oMail
.Attachments.Add (file)
.To = DLookup("[E-mail]", "MyAccounts", "[Company] ='" & myname & "'")
.send
End With

Set oMail = Nothing
Set oApp = Nothing

GoTo exits
failure:
errored = proc & " " & err & " " & err.Description
Call handleerror(errored, proc)
Resume Next
exits:
End Function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:02
Joined
May 7, 2009
Messages
19,247
maybe Outlook is closing too soon.
you need to put a Sleep before
setting the object to Nothing.
 

isladogs

MVP / VIP
Local time
Today, 04:02
Joined
Jan 14, 2017
Messages
18,246
Or use DoEvents to give the CPU time to complete the previous task
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 22:02
Joined
Feb 28, 2001
Messages
27,209
Outlook has a special case related to CreateObject. You need to first test whether there is already an Outlook instance running on your system because you cannot create TWO copies of Outlook. Some sort of special interlock applies. I'm guessing it is perhaps that Outlook wants exclusive file access to the system's main .PST file and it won't share, even with another instance of itself. But that latter is just a guess.

The error you are getting would be consistent with a failure of the line

Code:
Set oApp = CreateObject("Outlook.application")

since oApp would not be set, and the next line (that tries to use oApp) would detect the error as well.

Normally what you have to try to do is a GetObject of Outlook before you try to CreateObject of it. And you need to remember that if you didn't open Outlook (because you found one first), you don't close it either.

As a way to assure that you did (or did not) get things set up correctly, since you don't want to have to be awake to monitor this, you can initialize all of your Dim'd objects to Nothing (Set xxx = Nothing). Then after each step that would do a Set xxx = something for an object, include

Code:
If xxx Is Nothing Then ... (go bitch about it)

That way you could at least make a log entry somewhere to tell you what happened. And you would know what step didn't make it.
 

1268

Registered User.
Local time
Yesterday, 22:02
Joined
Oct 11, 2012
Messages
44
I can try that, anything is worth a shot. It works fine when I run the DB myself though.

I came in this a.m. and all client files were updated / ready to send and the error trap had 1 error 91 for each client e-mail.

Opened the module, hit F5 on the launcher and 30 minutes later had 6 e-mail with attachments.

Only fails when it kicks off from task scheduler.
 

jleach

Registered User.
Local time
Yesterday, 23:02
Joined
Jan 4, 2012
Messages
308
Have you considered using SMTP directly (something like CDO/Exchange or a 3rd party service)? Outlook automation is a PITA in the best of circumstances, and automating it through scheduled tasks is far more trouble than I've found it to be worth.
 

1268

Registered User.
Local time
Yesterday, 22:02
Joined
Oct 11, 2012
Messages
44
Have you considered using SMTP directly (something like CDO/Exchange or a 3rd party service)? Outlook automation is a PITA in the best of circumstances, and automating it through scheduled tasks is far more trouble than I've found it to be worth.
Sure had not but will look into it.

Sent from my SM-G950U using Tapatalk
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 22:02
Joined
Feb 28, 2001
Messages
27,209
The other thing to consider is this: How is the scheduled task set up? Remember that by default, Task Scheduler runs things as SYSTEM but YOU run those same things as a particular user. Some issues have been known to happen because SYSTEM is not allowed to run certain images. (It's a registry security setting that disallows that.) Do you have the scheduled item running as SYSTEM (i.e. do not specify user), as Administrator, or as your specific username?
 

1268

Registered User.
Local time
Yesterday, 22:02
Joined
Oct 11, 2012
Messages
44
The other thing to consider is this: How is the scheduled task set up? Remember that by default, Task Scheduler runs things as SYSTEM but YOU run those same things as a particular user. Some issues have been known to happen because SYSTEM is not allowed to run certain images. (It's a registry security setting that disallows that.) Do you have the scheduled item running as SYSTEM (i.e. do not specify user), as Administrator, or as your specific username?

I think you are on it. After some testing today noted auto process shows as system. I'll have to program around that and all should be well. Now that I know I just put a long sleep on and kick it off as I head out the door. Once I figure it out 100% will post the solve.
 

Users who are viewing this thread

Top Bottom