Hi Tom

I looked at this website, tried a few of them - i got the task creation figured out...works great too....

but i have two questions and am hoping you can help

first this code below i got from you last year and works great to create a worksheet in pdf and email it out....that part works awesome...here are the two issues..


if i print a regular pdf i have codes to print only the rows with data, that part works great, but when i do the email i cant figure out how to have it run the footer and only print the rows with data - i first thought i would run a print - so it remembers the footer info that was last printed....but that didn't work - then tried extracting the code for printing footer to within the code for the email but cant figure out how i can have it run the footer macro and run the data info of what rows have data...
below is the code and at the top is the added part for footer...

Sub SendPDFSales()
' Create PDF of active sheet and send as attachment.
   '''''''''''''''''''''''''' this is what i added for the footer and data macro
   Application.Run "SetFooterService"
   Dim lastRow1 As Long, i As Long
   For i = 3 To 1301
     If Len(Trim(Cells(i, "N").Value)) > 0 Then
         lastRow1 = i
    End If
   ActiveSheet.PageSetup.PrintArea = "B5:N" & lastRow1
   ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
   End With
  ''''''''''''''''''' end

  ' below is the email code that works fine.... just need to know if its possible to add above coding and if so how...
   Dim Sh1 As Worksheet
   Dim strPath As String, strFName As String
   Dim OutApp As Object, OutMail As Object
       Set bk1 = ThisWorkbook
       Set Sh1 = bk1.Worksheets("salesrpt")
    'Create PDF of active sheet only
   strPath = Environ$("temp") & "\" 'Or any other path, but include trailing "\"
   strFFName = Sh1.Range("Q22")
   strFName = Sh1.Range("Q22") & " - " & ".pdf"

   strEname = Sh1.Range("P11")
   'strPName = Sh1.Range("K29")

   strOName = Sh1.Range("Q21")
   'strOCName = Sh1.Range("A3")
   strCCName = Sh1.Range("Q20")

   strDPName = Sh1.Range("PrintDateSP").Value
   strXName = Sh1.Range("Q23")

   End With
       ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
       strPath & strFName, Quality:=xlQualityStandard, _
       IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    'Set up outlook
   Set OutApp = CreateObject("Outlook.Application")
   Set OutMail = OutApp.CreateItem(0)
    'Create message
   On Error Resume Next

   With OutMail
       .To = strEname 'Insert required address here ########
       .CC = strCCName
       .BCC = ""
       .Subject = strFFName

       .Body = vbNewLine & vbNewLine & _
       "Please find attached the file for the " & strFFName & vbNewLine & _
       "reflecting our request dated, " & Format(strDPName, "mmmm dd yyyy") & vbNewLine & vbNewLine & _
       "Please review and should there be any questions please call." & vbNewLine & vbNewLine & vbNewLine & _
       "Thank you!" & vbNewLine & vbNewLine & _
        strOName & vbNewLine & _
        "Service Representative" & vbNewLine & _
       .Attachments.Add strPath & strFName
       .Display   'Use only during debugging ##############################
       '.Send      'Uncomment to send e-mail ##############################
   End With
    'Delete any temp files created
   Kill strPath & strFName
   On Error GoTo 0
   Set OutMail = Nothing
   Set OutApp = Nothing

  Application.ScreenUpdating = True
End Sub

Second Issue:

is there a way i can call up specific sheets, ie (service request, billback and parts) if i wanted to send the worksheet i am on, which is the fax cover, then add anyone of the three i just mentioned....?

thanks Tom



what gets generated as the final PDF is a product of the method you use to make that PDF.  The ExportAsFixedFormat  command is provided by an addin that Microsoft makes available.   I have found information on the net that you can do multiple sheets by selecting them as a group before issuing the  activesheet.ExportAsFixedFormat  command.   

It sounds like you want you PDF to look like the printed out results.  I have no idea what Microsoft addin does, but if you are not getting the footer, then I would say it doesn't support that but if you look at the argument list for that command:

Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

you see there are several settings.  You should probably research what the possible values are and see if one of them will cause the PDF to be formatted as if it were printed (including print settings such as footers and so forth).  

In excel 2013 and beyond (I guess) there is a built in command to print a PDF.  If you have Excel 2013 or later, I would suggest playing with this command to see if you can get what you want. [you could get the code for doing it this way by turning on the macro recorded while you manually produced the PDF.  (this is an assumption; I haven't tested it).  

before the PDF addin, you could print to pdf by using a print driver.  Various ones existed.  In any event, this approach as I recall printed out the document but sent it to a virtual printer which captured it as a PDF file.  This would or should have then been formatted as if you had printed it and then scanned it in.  This might be the way you need to go.  

So I don't have a canned answer for you since I don't have a need to produce spreadsheets as PDF documents.

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.