I am having intermittant problems with my program failing when calling an email routine. I'm pretty sure I know where and why the error is occuring and (with help from here ) have tried a number of things to overcome the issue. It is very intermittant and may happen perhaps once out of every 10 times it is run. Basically my code copies a couple of worksheets to a new workbook, saves this then calls an email subroutine to create a new email message and attach the saved workbook.
I'm pretty sure that what happens is that sometimes (presumably depending upon PC speed and other processes running) the workbook is still saving when the email routine tries to attach the workbook, when this happens I I don't get the usual debug error, instead I get a runtime error, Application failed from which the only option is 'Exit' which then styops my code part way through.
I can overcome the issue by adding a wait of say 10 secs. before calling the email, however sometimes the code loops through 10-15 times so this adds considerably to the overall runtime.
What I am thinking is can I add a wait on error, something like:
create and save workbook code here:
call domail
Sub domail()
Dim outapp As Object
Dim outmail As Object
On error
Application.Wait Time + TimeSerial(0, 0, 5)
call domail
Set outapp = CreateObject("Outlook.Application")
Set outmail = outapp.CreateItem(olmailitem)
With outmail
.To = sendto
.cc = cc
.bcc = bcc
.subject = subject
.body = body
.attachments.Add (newreport)
.Display 'Or use Display or send
End With
'End If
Set outmail = Nothing
End Sub
I assume I can't call the routine from within itself but I hope this illustrates he kind of solution I am thinking of.
Bookmarks