trying to use this code to email worksheets in pdf but getting error?, not sure how to make this do 2 sheets?
Sub Button2_Click()
Dim sh As Worksheet
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
TempFilePath = Environ$("temp") & "\"
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set OutApp = CreateObject("Outlook.Application") This is the ERROR
OutApp.Session.Logon
For Each sh In ThisWorkbook.Worksheets
If sh.Range("a1").Value Like "?*@?*.?*" Then
TempFileName = TempFilePath & sh.Name & ".pdf"
'We Publish to PDF
On Error Resume Next
sh.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=TempFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
On Error GoTo 0
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = sh.Range("a1").Value
.CC = ""
.BCC = ""
.Subject = "hi there"
.Body = "hi there."
.Attachments.Add TempFileName
'.Send 'or use .Display
.display
On Error GoTo 0
End With
Set OutMail = Nothing
If Dir(TempFileName) <> "" Then Kill TempFileName
End If
Next sh
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Bookmarks