2012-01-30 114 views
0

我已經編寫了一些代碼,用於從選項卡1(1000行數據)的3列中獲取信息以在第二個選項卡上填充數據(以基於帳戶信息地址等)。代碼輸入Do While,並在代碼繼續循環時將PDF發送到隊列後發生問題。只有在運行代碼時纔會出現該錯誤,而單步執行代碼時則不會出現問題。Excel中的VBA無法通過PDFCreator在循環後打印

我在2003年和2007年相似的結果試過這個(2003年將打印3個文件,我已經得到了2007年到打印多達6檔)

我也嘗試添加手動延遲與

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

和作業後進入打印隊列

Sleep 3000 

我還添加了一個做,直到循環等待打印作業,深入到零,但沒有成功。

完整的代碼是:

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 

感謝您的任何輸入或建議,

+0

絕不是這是一個解決方案,但在殺死PDFCreator幫助的同時殺死spoolsv.exe? – jon 2012-01-30 23:12:33

+0

@jonlester沒有看到幫助我的問題。 spoolsv.exe看起來不會像我看到的一樣發生。我想我會試驗更多的延遲,因爲我認爲代碼只是在一些非常實驗性的調試(那裏有一個甲蟲)後才允許該進程真正開始 – jamesC 2012-01-31 20:06:19

+0

。我認爲我已經發現這些行的主要罪魁禍首,它們一直掛起:Set pdfjob = New PDFCreator.clsPDFCreator If pdfjob.cStart(「/ NoProcessingAtStartup」)= False Then – jamesC 2012-01-31 22:07:41

回答

0

我想補充的正確處置的PDFCreator(PDFC)後,已完成其工作,因爲不正確的處理會導致重複運行PDFC(剩餘流程,資源等)中的問題。隨PDFC一起發運的所有代碼樣本至少在最少的情況下處置,即它們至少調用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 

其他運送的樣品有時會顯示一些額外的技巧,但沒有丟失處理部分。