Forum Discussion
VBA code making pdfs and emailing in outlook blows up Outlook
To add a delay after each email is sent using VBA to prevent Outlook from crashing, you can use the Application.Wait method. This method pauses the execution of the code for a specified amount of time. Here is how you can modify your code to include a delay:
vbaCopy code
Sub CreatePersonSpecificPDFs()
' ... (previous code)
' ---------------------------------
' Export then Email PDFs
' ---------------------------------
Dim DelayBetweenEmails As Double
DelayBetweenEmails = 2 ' Set the delay in seconds (adjust as needed)
For iSheet = 1 To iSheetsFound
' ... (previous code)
' Attach the single person-specific PDF file to an email.
With oMailItem
.To = "**Email removed for security* @aol.com)"
.Subject = "Time Sheet"
.Body = "Please find attached your timesheet. Respond to this email with your acceptance."
.Attachments.Add sFileSpec
.Send
End With
' Add a delay after each email
Application.Wait Now + TimeValue("00:00:" & DelayBetweenEmails)
' ... (previous code)
Next iSheet
End SubIn this modification, a delay of 2 seconds (DelayBetweenEmails = 2) is added after each email is sent. You can adjust the value of DelayBetweenEmails as needed to find a suitable delay time that prevents Outlook from crashing.
Please note that adding delays in loops may slow down the overall execution of the code, but it can help prevent issues with Outlook when sending multiple emails in quick succession. Adjust the delay time based on your system's performance and requirements.
- juscuz419Jan 18, 2024Copper Contributor
Thanks for the input. I have a delay working but there are still some issues. Im working with some folks in Mr Excel on those issues. Thanks again