Hi there,
first ever post on such a forum, so hope you guys can assist.
I am trying to create and then attach a dynamic file to a series of emails. Code below has been cobbled together from other threads and creates the email but without any attachment. Help!
Sub Test1()
Dim i As Long, lastRow As Long
Set from_sheet = Sheets("Data")
Set to_sheet = Sheets("Data")
lastRow = from_sheet.Cells(Rows.Count, "A").End(xlUp).Row
For i = 5 To lastRow
'test if cell is empty
If from_sheet.Range("A" & i).Value <> "" Then
to_sheet.Range("A2").Value = from_sheet.Range("A" & i).Value
Sheets(Array("Letter", "P11D page1", "P11D page2")).Select
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim FName As String
Dim Email As String
FName = Sheets("Letter").Range("g50").Text
Email = Sheets("Data").Range("f2").Text
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FName _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
With OutMail
.To = Email
.CC = ""
.BCC = ""
.Subject = "P11D draft attached"
.Body = "please find attached draft P11D and accompanying letter. This requires you to review and raise any queries within 7 days. If you have any questions please don't hesitate to contact me direct, kind regards, "
.Attachments.Add FName
If Send = True Then
.Send
Else
.display
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next i
End Sub
first ever post on such a forum, so hope you guys can assist.
I am trying to create and then attach a dynamic file to a series of emails. Code below has been cobbled together from other threads and creates the email but without any attachment. Help!
Sub Test1()
Dim i As Long, lastRow As Long
Set from_sheet = Sheets("Data")
Set to_sheet = Sheets("Data")
lastRow = from_sheet.Cells(Rows.Count, "A").End(xlUp).Row
For i = 5 To lastRow
'test if cell is empty
If from_sheet.Range("A" & i).Value <> "" Then
to_sheet.Range("A2").Value = from_sheet.Range("A" & i).Value
Sheets(Array("Letter", "P11D page1", "P11D page2")).Select
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim FName As String
Dim Email As String
FName = Sheets("Letter").Range("g50").Text
Email = Sheets("Data").Range("f2").Text
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FName _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
With OutMail
.To = Email
.CC = ""
.BCC = ""
.Subject = "P11D draft attached"
.Body = "please find attached draft P11D and accompanying letter. This requires you to review and raise any queries within 7 days. If you have any questions please don't hesitate to contact me direct, kind regards, "
.Attachments.Add FName
If Send = True Then
.Send
Else
.display
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End If
Next i
End Sub