Hi All,
I got some code from you lovely people to print all my tabs to PDF files and attached them in an email to myself. This will save me loads of time. One thing I'm hoping to add though, is for each PDF to be named the same thing as the sheet it was printed from. So if the sheet is called "Packing List", the PDF file that attaches in the email would also be called "Packing List". I'm hoping this is an easy fix for you geniuses but how to do this escapes my feeble brain.
Will be so appreciative if any of you can help me figure out how to do this!
Sub PDFandSend()
Dim ws As Worksheet
Dim sAttach$, fName$, vDir$
Dim i%
Dim a
Const erNum As Long = vbObjectError + 1000
On Error GoTo errHandler
vDir$ = "C:\Temp\Files" & Format(Now, "ddmmyyhhmmss")
MkDir vDir
With ActiveWorkbook
For Each ws In .Worksheets
With ws
If .Visible Then
If WorksheetFunction.CountA(.Cells) > 0 Then
i = i + 1
fName = vDir & "\file2send_" & i & ".pdf"
sAttach = sAttach & fName & "|"
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fName, openafterpublish:=False
End If
End If
End With
Next
End With
If Len(sAttach) Then
With CreateObject("Outlook.Application").CreateItem(0)
.To = "[email protected]"
.Subject = "Test Email #2 for Logistics Documents: "
.Body = "Documents for Global Logistics"
a = Split(Left(sAttach, Len(sAttach) - 1), "|")
For i = 0 To UBound(a)
.Attachments.Add a(i)
Next
.Display
End With
MsgBox "done"
Else
Err.Raise erNum, , "No attachments created"
End If
finish:
On Error Resume Next
CreateObject("Scripting.FileSystemObject").DeleteFolder vDir
Exit Sub
errHandler:
MsgBox Err.Description
Resume finish
End Sub
Bookmarks