简体   繁体   English

将Excel工作表打印成单个PDF

[英]Print Excel sheet into single PDF

I have an Excel sheet that is populated with data from a database. 我有一个Excel表格,其中填充了来自数据库的数据。 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. 我想将输出放入单个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. 问题是,每次我导出新数据时,PDF都会被覆盖,而不是添加到pdf中。 Below is the code I use to export the sheet. 以下是我用于导出工作表的代码。

    Sheets("FORM8").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "F:\Reg\Form8\F8.pdf", OpenAfterPublish:=False

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM