【问题标题】:Excel VBScript to close the openeded and previously printed PDF before printing to another PDFExcel VBScript 在打印到另一个 PDF 之前关闭打开和先前打印的 PDF
【发布时间】:2016-07-17 00:57:31
【问题描述】:

我正在使用一段代码将工作表打印到一个 PDF 文件中。打开此 PDF 文件后,如果我尝试从同一个 excel 文件再次打印到 PDF,我会收到一个 VB 错误:“文档未保存”并且调试会将我带到代码中:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
    strFilename & " " & wedate_text & " Time", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    True

这里是代码:

Sub PrintAnadarkoTicketsToPDF()
Worksheets("Cover").Visible = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Dim strFilename As String
Dim rngRange As Range
Dim wedate As Date
Dim wedate_text As String
Set rngRange = Worksheets("Cover").Range("A5")
strFilename = rngRange.Value
wedate = Worksheets("Cover").Range("B24").Value
wedate_text = Format$(wedate, "mm.dd.yyyy")
Dim myArray() As Variant
    Dim i As Integer
    Dim j As Integer
    j = 0
    For i = 1 To Sheets.Count
        If Sheets(i).Visible = True Then
            ReDim Preserve myArray(j)
            myArray(j) = i
            j = j + 1
        End If
    Next i
    Sheets(myArray).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
    strFilename & " " & wedate_text & " Time", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    True
Worksheets("Cover").Visible = True
Sheets(1).Select
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
End Sub

我的问题是:如何在不使脚本崩溃的情况下打印第二个 PDF?我想关闭前一个 PDF 或使用不同的文件名创建第二个 PDF。感谢您的建议。 兰迪

【问题讨论】:

  • 你尝试this解决方案了吗?
  • 我猜您没有提供有效的文件名和扩展名,请提供有效的文件名,如下所示“c:\path\filename.pdf”。
  • @Kiran Maroju 正在通过代码中的单元格引用组装文件名。

标签: vba excel pdf vbscript


【解决方案1】:

我不确定您为什么要使用一个循环来计算非隐藏工作表的数量。另外,您可以导出该循环内的工作表。这可能会解决您的问题:

For i = 1 To Sheets.Count
    If Sheets(i).Visible = True Then
        Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, filename:= _
            strFilename & Trim(Str(i)) & " " & wedate_text & " Time", Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
            True
    End If
Next i

还要注意在文件名中添加工作簿编号,因为它试图保存到同一个文件。

【讨论】:

  • 我需要将所有工作表打印到一个 PDF 中。这会为每张纸打印出单独的 PDF。
  • 在这种情况下,您可能希望将数据全部放在一张纸上。您甚至可以创建一个新工作表,将数据放入其中,并在将其导出为单个文件后将其删除。当然,您可以在代码中完成所有这些操作。如果您在工作表上ExportAsFixedFormat,那么您会从中得到一个文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多