首页 > 解决方案 > 循环“批量导出”崩溃 - 处理器或代码错误?

问题描述

为什么 Excel 不能循环遍历大型数据集?!

我有 2 种不同的文档形式,需要成百上千的导出为 PDF。我从互联网上提取了批量导出脚本并对其进行了修改以供我使用,因此它可以根据“批量 PDF 打印机”工作表上选中的复选框来处理这些表单中的任何一种。

一切运行良好 - 对于循环访问的前 10-15 个工作簿,然后它崩溃了。每个 Excel 文档都会冻结(无响应),宏当前访问的页面会部分打开,没有可见的数据或单元格。此时“发布”消息框也可能会冻结。一旦它报告缺少内存错误-但我无法重复此操作。Excel不应该删除未使用的缓存以免内存超载吗?如果它有一段时间不能正常运行,我会怀疑它是一个循环。我听说没有办法在“缓存转储”或类似性质的东西中编写脚本。它是糟糕的代码,还是我对我的处理器要求太多了?

Sub Convert2PDF()
'Update the checkbox linked formulas on the GUI workbook
Sheet1.Range("A2").Formula = Sheet1.Range("A2").Formula
Sheet1.Range("B2").Formula = Sheet1.Range("B2").Formula
Sheet1.Range("C2").Formula = Sheet1.Range("C2").Formula

Dim strFolder As String
Dim strXLFile As String
Dim strPDFFile As String
Dim wbk As Workbook
Dim lngPos As Long
' set folder
strFolder = ThisWorkbook.Path & "\putfileshere" & "\"
Application.ScreenUpdating = False
' Get first filename
strXLFile = Dir(strFolder & "*.xls*")
' Loop through Excel workbooks in folder
Do While strXLFile <> ""
    ' Open workbook
    Set wbk = Workbooks.Open(Filename:=strFolder & strXLFile)
    ' Assemble the PDF filename
    lngPos = InStrRev(strXLFile, ".")
    strPDFFile = Left(strXLFile, lngPos) & "pdf"
    ' Export to PDF
    'Do the next 8 lines crash the Macro because they recalculate for every sheet? Page1, Page2, Page3 value are the same for all workbooks processed in a batch
            Dim Page1 As String
            Dim Page2 As String
            Dim Page3 As String
            Dim Page4 As String
                Page1 = ThisWorkbook.Sheets("Batch PDF Printer").Range("A2")
                Page2 = ThisWorkbook.Sheets("Batch PDF Printer").Range("B2")
                Page3 = ThisWorkbook.Sheets("Batch PDF Printer").Range("C2")

            If ThisWorkbook.Sheets("Batch PDF Printer").Range("C2") = "" Then 
                wbk.Sheets(Array(Page1, Page2)).Select
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                    ThisWorkbook.Path & "\pdfsgohere" & "\" & wbk.Name, _
                    Quality:=xlQualityStandard, IncludeDocProperties:=False, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=False

'run process for format option 2
            Else:
                wbk.Sheets(Array(Page1, Page2, Page3)).Select
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                    ThisWorkbook.Path & "\pdfsgohere" & "\" & wbk.Name, _
                    Quality:=xlQualityStandard, IncludeDocProperties:=False, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=False
            'Tried killing the finished document to improve function
                Dim xFullName As String
                xFullName = Application.ActiveWorkbook.FullName
                ActiveWorkbook.Saved = True
                Application.ActiveWorkbook.ChangeFileAccess xlReadOnly
                Kill xFullName
                Application.ActiveWorkbook.Close False

            End If
    ' Close workbook - didn't seem to help (can't do it when the workbook is gone)
       'wbk.Close SaveChanges:=False

    ' Get next filename
    strXLFile = Dir
Loop
Application.ScreenUpdating = True
MsgBox "All Done"

谢谢您的帮助。这几天我一直在想办法解决这个问题。

标签: excelvbapdfbatch-processing

解决方案


如果您在导出文件中有链接图像。

导出的链接图像会在内核中留下一点或字节,这些会累积并最终破坏 excel。

我在 Internet 上仅找到 1 个地方找到此解决方案,但我无法再次找到它,但它通过删除链接图像让我从 200 到 1000 个 VBA 宏循环。

VBA 代码中的任何内容都无济于事,我使用了暂停、保存工作簿以清除内存、禁用事件等...

我在这里写了我的问题的答案:https ://stackoverflow.com/a/53600884/10069870

如果您的导出文件中没有链接图像,请忽略 :)


推荐阅读