Can you add a 5 second delay after each Send statement in the VBA code below.

juscuz419

Board Regular
Joined
Apr 18, 2023
Messages
57
Office Version
  1. 2019
Platform
  1. Windows

Code below Blows up Outlook after 12 emails go out. Any way to add a delay after each email to keep that from happening. Maximum # of emails would be 40.​

Got this code from a user in Microsoft Community last May. It works great until the number of emails exceeds 12 then I get 12 emails sent and email failure notifications from Outlook for the rest.

Sub CreatePersonSpecificPDFs()

' -------------------------------
' Declarations
' -------------------------------

' This sub creates a .pdf file for individual timesheets for the event and "Blindly"
' emails them to the email address found on each timesheet

' Workbook object used 1. to loop through worksheets,
' 2. specify which worksheet to export as PDF.
Dim wsSource As Worksheet

Dim sEmailAddress As String

' File name and location.
Dim sPath As String
Dim sFileName As String
Dim sFileSpec As String

' Array holding "tab names" of person-specific worksheets.
Dim asSheetTabNames() As String

' Count of qualifying worksheets.
Dim iSheetsFound As Long

' Used for looping person-specific worksheets found.
Dim iSheet As Long

' Objects used for sending email via outlook.
Dim oOutlookApp As Object
Dim oMailItem As Object

Dim sMsg As String

' -------------------------------
' Initializations
' -------------------------------

sPath = ThisWorkbook.Path & "\"

iSheetsFound = 0

' -------------------------------
' ID Sheets to Export
' -------------------------------

' Iterate through the worksheets collection looking for person-
' specific worksheets to be exported as a PDF file.
' Load person-specific worksheet TAB names into the array.
For Each wsSource In ThisWorkbook.Worksheets

' Use worksheets' "code names" to determine which worksheets are to be exported.
' A person-specific worksheet has codename like Template# or like Template##
' where # is a wildcard indicating any numeric value.
If wsSource.CodeName Like "Template#" Or wsSource.CodeName Like "Template##" _
Then

iSheetsFound = iSheetsFound + 1

ReDim Preserve asSheetTabNames(iSheetsFound)

asSheetTabNames(iSheetsFound) = wsSource.Name

End If

Next wsSource


' -------------------------------------------------
' Check no Person-specific Sheets Found
' -------------------------------------------------

If iSheetsFound = 0 _
Then
sMsg = "No individual timesheets have been created"
MsgBox sMsg, vbCritical, "Create then email PDFs"
Exit Sub
End If

' ---------------------------------
' Export then Email PDFs
' ---------------------------------

For iSheet = 1 To iSheetsFound

sFileName = asSheetTabNames(iSheet) & ".PDF"

sFileSpec = sPath & sFileName

' Delete the file if it already exists.
On Error Resume Next
Kill sFileSpec
On Error GoTo 0

Set wsSource = ThisWorkbook.Worksheets(asSheetTabNames(iSheet))

wsSource.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sFileSpec, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

' Get email address from the person-specific worksheet.
sEmailAddress = wsSource.Range("EmailAddress").Value

' Set up Outlook objects for sending email.
Set oOutlookApp = CreateObject("Outlook.Application")
Set oMailItem = oOutlookApp.CreateItem(0)

' Attach the single person-specific PDF file to an email.
With oMailItem
.To = "**Email removed for security* @aol.com)"
' .To = sEmailAddress 'Specify the email address of the recipient
.Subject = "Time Sheet"
.Body = "Please find attached your timesheet. Respond to this email with your acceptance."
.Attachments.Add sFileSpec
.Send
End With

Set oOutlookApp = Nothing
Set oMailItem = Nothing

Next iSheet

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
After this line:
Set oMailItem = Nothing


Put this line:
Application.Wait (Now + TimeValue("00:00:05"))
 
Upvote 0
Not sure I am pursuing the right path. I added the line statement suggested and ran a test for 40 emails with attachments. It increased the number of emails sent to 17 before starting to send mail failure reports. Increased the delay a couple of more times, retested, and ended up at 15 seconds. That allowed 34 emails to go out. Oddly, after getting a single failure message, more emails would go out, then a couple more failure notifications followed by a few more emails going out, totaling 34 successes out of the 40 attempts. There were 3 additional emails placed in the Outlook Drafts folder. Could this be an Outlook problem or a network bandwidth problem instead of a code problem? Or is there a better idea out there to accomplish creating a pdf of up to 40 worksheets, opening Outlook, attaching the pdf to an email with an address contained in the worksheet and sending the emails automatically?
 
Upvote 0
There could be several causes, memory, the performance of your computer, the network, etc.

Try again.

Move this line:

Set oOutlookApp = CreateObject("Outlook.Application")

Before this line:
For iSheet = 1 To iSheetsFound

Delete this line:
Set oOutlookApp = Nothing


Try again
 
Upvote 0
There could be several causes, memory, the performance of your computer, the network, etc.

Try again.

Move this line:

Set oOutlookApp = CreateObject("Outlook.Application")

Before this line:
For iSheet = 1 To iSheetsFound

Delete this line:
Set oOutlookApp = Nothing


Try again
Finally got a chance to get back to this. Last suggestion seems to have worked. I sent 40 emails with PDF attachments to myself as a test and got no System Admin failure notices from Outlook. I received all 40 emails. It took a while for all of them to come in but I think that was more because they were coming to my inbox all at once rather than going to the 40 individuals mailboxes. Thanks for the help. I have a couple more "opportunities" I want to explore for this tool. Look forward to posting them.
 
Upvote 0
Im glad to help you. Thanks for the feedback.
😇
 
Upvote 0
Solution

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top