I have been working on this for two days and cannot get the formatting details to come through on the pdf being attached to an email. All of the data is there but it is useless without the formatting
Option Explicit
Sub CreateAndEmailPDF()
Dim wsSource As Worksheet, wsTemp As Worksheet
Dim selectedCells As Range, cell As Range
Dim tempRow As Long
Dim outlookApp As Object, outlookMail As Object
Dim emailAddresses As String
Dim pdfFilePath As String
' Set the source worksheet
Set wsSource = Worksheets("Training View")
' Prompt user to select cells in Column A
On Error Resume Next
Set selectedCells = Application.InputBox("Select cells in Column A", Type:=8)
On Error GoTo 0
If selectedCells Is Nothing Then Exit Sub ' Exit if no cells selected
' Add a temporary worksheet
Set wsTemp = Worksheets.Add
wsTemp.Name = "Training Data"
' Copy headers from H5:S5, skipping hidden columns (Q and R)
wsSource.Range("H5:P5").Copy
wsTemp.Range("C1").PasteSpecial Paste:=xlPasteAll
wsSource.Range("S5").Copy
wsTemp.Range("L1").PasteSpecial Paste:=xlPasteAll
' Retain column widths
Dim col As Long
For col = 8 To 17
If wsSource.Columns(col).Hidden = False Then
wsTemp.Columns(col - 7).ColumnWidth = wsSource.Columns(col).ColumnWidth
End If
Next col
' Set the initial row for data in the temp sheet
tempRow = 2
emailAddresses = ""
For Each cell In selectedCells
If cell.Column = 1 Then
' Collect email addresses from Column G
If emailAddresses = "" Then
emailAddresses = wsSource.Cells(cell.Row, 7).Value
Else
emailAddresses = emailAddresses & ";" & wsSource.Cells(cell.Row, 7).Value
End If
' Copy values and formatting from selected rows, ignoring hidden columns
wsTemp.Cells(tempRow, 1).Value = wsSource.Cells(cell.Row, 3).Value ' Column C
wsTemp.Cells(tempRow, 2).Value = wsSource.Cells(cell.Row, 1).Value ' Column A
wsSource.Range("H" & cell.Row & ":P" & cell.Row).Copy
wsTemp.Cells(tempRow, 3).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
wsSource.Range("S" & cell.Row).Copy
wsTemp.Cells(tempRow, 12).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
tempRow = tempRow + 1
End If
Next cell
' Define the file path for the PDF
pdfFilePath = ThisWorkbook.Path & "\Training_Data.pdf"
' Fit sheet to one page width in landscape mode
With wsTemp.PageSetup
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
' Export the temp sheet as a PDF
wsTemp.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfFilePath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
' Set up Outlook and create email
Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)
With outlookMail
.To = emailAddresses
.Subject = "Training Data PDF"
.Body = "Attached is the Training Data PDF."
.Attachments.Add pdfFilePath
.Display ' Show email for user review
End With
' Delete the temp sheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
' Clean up
Application.CutCopyMode = False
'MsgBox "PDF created and email draft is ready!"
End Sub
Option Explicit
Sub CreateAndEmailPDF()
Dim wsSource As Worksheet, wsTemp As Worksheet
Dim selectedCells As Range, cell As Range
Dim tempRow As Long
Dim outlookApp As Object, outlookMail As Object
Dim emailAddresses As String
Dim pdfFilePath As String
' Set the source worksheet
Set wsSource = Worksheets("Training View")
' Prompt user to select cells in Column A
On Error Resume Next
Set selectedCells = Application.InputBox("Select cells in Column A", Type:=8)
On Error GoTo 0
If selectedCells Is Nothing Then Exit Sub ' Exit if no cells selected
' Add a temporary worksheet
Set wsTemp = Worksheets.Add
wsTemp.Name = "Training Data"
' Copy headers from H5:S5, skipping hidden columns (Q and R)
wsSource.Range("H5:P5").Copy
wsTemp.Range("C1").PasteSpecial Paste:=xlPasteAll
wsSource.Range("S5").Copy
wsTemp.Range("L1").PasteSpecial Paste:=xlPasteAll
' Retain column widths
Dim col As Long
For col = 8 To 17
If wsSource.Columns(col).Hidden = False Then
wsTemp.Columns(col - 7).ColumnWidth = wsSource.Columns(col).ColumnWidth
End If
Next col
' Set the initial row for data in the temp sheet
tempRow = 2
emailAddresses = ""
For Each cell In selectedCells
If cell.Column = 1 Then
' Collect email addresses from Column G
If emailAddresses = "" Then
emailAddresses = wsSource.Cells(cell.Row, 7).Value
Else
emailAddresses = emailAddresses & ";" & wsSource.Cells(cell.Row, 7).Value
End If
' Copy values and formatting from selected rows, ignoring hidden columns
wsTemp.Cells(tempRow, 1).Value = wsSource.Cells(cell.Row, 3).Value ' Column C
wsTemp.Cells(tempRow, 2).Value = wsSource.Cells(cell.Row, 1).Value ' Column A
wsSource.Range("H" & cell.Row & ":P" & cell.Row).Copy
wsTemp.Cells(tempRow, 3).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
wsSource.Range("S" & cell.Row).Copy
wsTemp.Cells(tempRow, 12).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
tempRow = tempRow + 1
End If
Next cell
' Define the file path for the PDF
pdfFilePath = ThisWorkbook.Path & "\Training_Data.pdf"
' Fit sheet to one page width in landscape mode
With wsTemp.PageSetup
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
' Export the temp sheet as a PDF
wsTemp.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfFilePath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
' Set up Outlook and create email
Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)
With outlookMail
.To = emailAddresses
.Subject = "Training Data PDF"
.Body = "Attached is the Training Data PDF."
.Attachments.Add pdfFilePath
.Display ' Show email for user review
End With
' Delete the temp sheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
' Clean up
Application.CutCopyMode = False
'MsgBox "PDF created and email draft is ready!"
End Sub