Excel VBScript在打印到另一个PDF之前closures打开的和之前打印的PDF

我正在用一块代码将工作表打印到一个PDF文件中。 打开这个PDF文件,如果我尝试从这个相同的Excel文件再次打印到PDF,我得到一个VB错误:“文档没有保存”和debugging带我在这里的代码:

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而不会崩溃脚本? 我想closures以前的PDF或用不同的文件名创build第二个PDF。 感谢您的build议。 兰迪

我不知道为什么你想有一个循环来计算非隐藏工作表的数量。 另外,您可以在该循环内导出表单。 这可能会解决您的问题:

 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 

还要注意工作簿号码添加到文件名称,因为它试图保存到相同的文件。