The focus is lost in the Sub Mail_ActiveSheet_PDF_Outlook which is creating
some PDF files and creating an email with the PDF attachemnts.
After the email is created, Excel is blinking in the taskbar and does not
proces the next report set till I click on Excel.
In case it is relevant, the Sub ProcessReportSetnn is loading a progress bar
and then calling a Sub that generates some reports.
Sub CreateAndEmailReports()
Call ProcessReportSet01
Sheets("Report").Select
Call Mail_ActiveSheet_PDF_Outlook
Call ProcessReportSet02
Sheets("Report").Select
Call Mail_ActiveSheet_PDF_Outlook
Call ProcessReportSet03
Sheets("Report").Select
Call Mail_ActiveSheet_PDF_Outlook
End Sub
Sub Mail_ActiveSheet_PDF_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim FilenameStr As String
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
FilenameStr = Application.DefaultFilePath & "\" & _
Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf"
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FilenameStr, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"See the attached PDF file with the last figures" & vbNewLine & _
vbNewLine & "Regards Tom"
On Error Resume Next
With OutMail
.To = "(e-mail address removed)"
.CC = ""
.BCC = ""
.Subject = "Subject line"
.Body = strbody
.Attachments.Add FilenameStr
.Send 'or use .Display
End With
On Error GoTo 0
'Delete the pdf you send
Kill FilenameStr
Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "PDF add-in Not Installed"
End If
End Sub
Sub ProcessReportSet01()
UserForm01.LabelProgress.Width = 0
UserForm01.Show
End Sub