Excel中的VBA无法在循环后通过PDFCreator打印

Excel中的VBA无法在循环后通过PDFCreator打印,pdf,excel,vba,Pdf,Excel,Vba,我已经编写了一些代码,从选项卡1上的3列中获取信息1000行数据,填充第二个选项卡上的数据,根据帐户信息地址等生成一个表单信函。代码输入Do While,当代码继续循环时,问题发生在PDF发送到队列后。该错误仅在运行代码时发生,在单步执行代码时没有问题 我在2003年和2007年都尝试过这个方法,结果相似,2003年将打印3个文件,而我在2007年将打印多达6个文件 我还尝试添加一个手动延迟 Private Declare Sub Sleep Lib "kernel32" (ByVal dwMi

我已经编写了一些代码,从选项卡1上的3列中获取信息1000行数据,填充第二个选项卡上的数据,根据帐户信息地址等生成一个表单信函。代码输入Do While,当代码继续循环时,问题发生在PDF发送到队列后。该错误仅在运行代码时发生,在单步执行代码时没有问题

我在2003年和2007年都尝试过这个方法,结果相似,2003年将打印3个文件,而我在2007年将打印多达6个文件

我还尝试添加一个手动延迟

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
作业进入打印队列后

 Sleep 3000 
我还添加了一个Do-Until循环,以等待打印作业降至零,但没有成功

完整的代码是:

Sub PlaceData()

Dim accountNumber As String
Dim partyID As String
Dim ClientAddress As String
Dim bRestart As Boolean
Dim totalAccounts As Long
Dim pdfjob As PDFCreator.clsPDFCreator
Dim dataPage As Worksheet
Dim letterPage As Worksheet
Dim CB As Workbook 'CB = ClientBook

Set CB = ThisWorkbook
Set dataPage = CB.Sheets("Data")
Set letterPage = CB.Sheets("Letter")


'will iterate through the account numbers down

therow = 1
'where the loop starts
totalAccounts = dataPage.Cells(Rows.Count, 1).End(xlUp).Row

Do While therow < totalAccounts
therow = therow + 1

'for the form letter

letterPage.Range("F4").FormulaR1C1 = dataPage.Range("A" & therow)
letterPage.Range("F5").FormulaR1C1 = dataPage.Range("C" & therow)
letterPage.Range("B10").FormulaR1C1 = dataPage.Range("B" & therow)


'accountnumber minus one digit for the file name
accountNumber = letterPage.Range("F4").Text
accountNumberShort = Mid(accountNumber, 1, 8)

On Error GoTo EarlyExit
Application.ScreenUpdating = False
Set pdfjob = New PDFCreator.clsPDFCreator
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator

'Check if PDFCreator is already running and attempt to kill the process if so

Do
    bRestart = False
    Set pdfjob = New PDFCreator.clsPDFCreator
    If pdfjob.cStart("/NoProcessingAtStartup") = False Then
        'PDF Creator is already running.  Kill the existing process
        Shell "taskkill /f /im PDFCreator.exe", vbHide
        DoEvents
        Set pdfjob = Nothing
        bRestart = True
    End If
Loop Until bRestart = False

With pdfjob
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = sPDFPath
    .cOption("AutosaveFilename") = accountNumberShort
    .cOption("AutosaveFormat") = 0    ' 0 = PDF
    .cClearCache
End With

        'Delete the PDF if it already exists
        If Dir(sPDFPath & accountNumberShort) = accountNumberShort Then Kill (sPDFPath & accountNumberShort)

'Print the document to PDF

        letterPage.PrintOut copies:=1, ActivePrinter:="PDFCreator"

        'Wait until the print job has entered the print queue
        Do Until pdfjob.cCountOfPrintjobs = 1
            DoEvents
        Loop
        pdfjob.cPrinterStop = False

 'Wait until PDF creator is finished then release the objects

        Do Until pdfjob.cCountOfPrintjobs = 0
            DoEvents
        Loop

Loop

'where the loop will end and where the issue is (I think)

'cleanups

Cleanup:
'Release objects and terminate PDFCreator
Set pdfjob = Nothing
Shell "taskkill /f /im PDFCreator.exe", vbHide
On Error GoTo 0
Application.ScreenUpdating = True
Exit Sub

EarlyExit:
'Inform user of error, and go to cleanup section
MsgBox "There was an error encountered.  PDFCreator has" & vbCrLf & _
       "has been terminated.  Please try again.", _
       vbCritical + vbOKOnly, "Error"
Resume Cleanup

Set CB = Nothing
Set dataPage = Nothing
Set letterPage = Nothing
Set pdfjob = Nothing

End Sub

感谢您的任何意见和建议,

我将在完成PDFCreator或PDFC的工作后添加正确的处理方法,因为不正确的处理方法可能会导致PDFC剩余过程、资源等的重复运行出现问题。。PDFC附带的所有代码示例都至少实现了disposition,即它们至少调用clsPDFCreator.cClose方法

例如,检查示例文件C:\Program Files x86\PDFCreator\COM\VB6\Sample1\Form1.frm如果将PDFC安装到不同的目录中,请调整路径。在示例中找到的代码:

 If noStart = False Then
   PDFCreator1.cClose   ' sending cleanup&exit request to PDFCreator
   While PDFCreator1.cProgramIsRunning ' loop while PDFCreator is not found finished
     DoEvents
     Sleep 100 ' polling interval 100 ms
   Wend
 End If

 Set PDFCreator1 = Nothing
 Set pErr = Nothing
 Set opt = Nothing

其他装运的样本有时会显示一些额外的技巧,但没有一个遗漏处理部分。

这决不是解决方案,但在杀死PDFCreator帮助的同时杀死spoolsv.exe吗?@jonlester no,这对我的问题没有帮助。spoolsv.exe看起来不会像我看到的那样出现问题。我想我会对延迟进行更多的实验,因为我认为代码不允许进程真正启动。在一些非常实验性的调试之后,那里有一只甲虫。我想我已经找到了罪魁祸首这些行是它一直挂起的:Set pdfjob=New PDFCreator.clsPDFCreator如果pdfjob.cStart/NoProcessingAtStartup=False那么