我有一个宏,可以根据我插入工作表的数据生成 PDF。我需要生成的 PDF 数量每周都不同。我单击按钮,它会运行以下代码。它将数据从活动选项卡复制到模板工作表,然后将该模板导出为 PDF。
Sub makePDFS()
Sheets("Do Not Touch").Visible = True
Dim activeTab As Worksheet
Dim templateTab As Worksheet
Dim fieldsTab As Worksheet
Dim invoicestab As Worksheet
Dim fullFilePath As String
Set activeTab = Worksheets("Do Not Touch")
Set templateTab = Worksheets("Template")
Set fieldsTab = Worksheets("Fields")
Set invoicestab = Worksheets("Bulk Invoices")
Dim targetRangeToReplace As String
targetRangeToReplace = "A3:H29"
Dim colFileName As Integer
colFileName = 34
Dim colFilePath As Integer
colFilePath = 35
Dim replaceRange As Range
Set replaceRange = activeTab.Range(targetRangeToReplace)
Dim templateRange As Range
Set templateRange = templateTab.Range(targetRangeToReplace)
For r = 5 To invoicestab.Cells(Rows.Count, 1).End(xlUp).Row
templateRange.Copy replaceRange
For Each field In fieldsTab.Range(fieldsTab.Cells(2, 1), fieldsTab.Cells(fieldsTab.Cells(Rows.Count, 1).End(xlUp).Row, 1))
'field.Value
'field.Offset(0, 1).Value
For Each cell In replaceRange
cell.Value = Replace(cell.Value, field.Value, invoicestab.Cells(r, field.Offset(0, 1).Value).Value)
Next cell
Next field
'save to pdf
fullFilePath = "J:\Invoices\" & invoicestab.Cells(r, colFileName).Value
activeTab.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
fullFilePath, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
invoicestab.Cells(r, colFilePath).Value = fullFilePath
Next r
Sheets("Do Not Touch").Visible = False
End Sub
我遇到的问题是,在循环过程中生成所有 PDF 后,它总是会出现一个空白行。当它尝试为这个空白行生成 PDF 时,我收到以下错误消息:运行时错误“-2147024773 (8007007b)”:文档未保存。
我怀疑,因为该行为空,所以没有有效的文件路径,但我无论如何也想不出一旦到达空白行就如何停止循环。
有人知道怎么做吗?