0

I have an Excel sheet that is populated with data from a database. As I loop through the data, I write to the same sheet and print it. I want to put the output into a single PDF. I'm basically printing the same sheet over and over with different data. The problem is that instead of adding to the pdf, the pdf gets overwritten each time I export new data. Below is the code I use to export the sheet.

    Sheets("FORM8").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "F:\Reg\Form8\F8.pdf", OpenAfterPublish:=False
Tadpole
  • 15
  • 5
  • Possible duplicate of [Add print area content (appending) at the end of initial existed pdf file in a new page section](https://stackoverflow.com/questions/46327666/add-print-area-content-appending-at-the-end-of-initial-existed-pdf-file-in-a-n) – blurfus Jun 26 '18 at 20:08
  • You just want to print all of your sheets as one PDF rather than individual PDF files? (if you do individual you need to throw a time stamp onto the end of the filename to prevent overwrite) – Wookies-Will-Code Jun 26 '18 at 20:17
  • I just edited the description to be a little clearer. I want one pdf. – Tadpole Jun 26 '18 at 20:27
  • So put all of the data into the worksheet first, and then do your export. – Ken White Jun 27 '18 at 01:54
  • See comment below. – Tadpole Jun 27 '18 at 14:08

1 Answers1

0

You can append the current date and epoch time to the file name to ensure that the file is unique and created into your directory.

Dim dateName As String
Dim outputFile As String
Dim FileName As String
Dim epoch As Long

dateName = Format(Date, "mm-dd-yyyy")
epoch = DateDiff("s", #1/1/1970#, Now())

FileName = dateName + Str(epoch)

outputFile = "F:\Reg\Form8\F8" + FileName + ".pdf"

Sheets("FORM8").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    outputFile, OpenAfterPublish:=False
Ian-Fogelman
  • 1,595
  • 1
  • 9
  • 15
  • I am creating a single PDF of many reports. A report is a single Excel sheet. It is the same sheet used over and over. I loop through the data, update the sheet and export the sheet to a PDF. I want to export that sheet into the same PDF file. I want to add to the PDF file, not replace it or create unique PDFs. – Tadpole Jun 27 '18 at 14:09