2016-05-23 24 views
0

我試圖PDF數組與此代碼合併到一個:結合PDF文件使用VBA

Option Explicit 

Sub Fusion_PDFs(ByVal name As String, ByRef pdfs() As Variant) 

Dim oPDDoc() As Object 
Dim oPDDocFinal As Object 
Dim Num As Long 
Dim i As Integer 

    Set oPDDocFinal = CreateObject("AcroExch.PDDoc") 
    oPDDocFinal.Open (pdfs(0)) 

    ReDim oPDDoc(UBound(pdfs)) 

    For i = LBound(pdfs) + 1 To UBound(pdfs) 

     Set oPDDoc(i) = CreateObject("AcroExch.PDDoc") 
     oPDDoc(i).Open (pdfs(i)) 

    Next i 

    For i = LBound(oPDDoc) To UBound(oPDDoc) 

     Num = oPDDocFinal.GetNumPages() - 1 

     oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True 

    Next i 


    oPDDocFinal.Save 1, ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" 

    'Application.DisplayAlerts = False 

    For i = LBound(oPDDoc) To UBound(oPDDoc) 

     oPDDoc(i).Close 
     Set oPDDoc(i) = Nothing 

    Next i 

    oPDDocFinal.Close 
    Set oPDDocFinal = Nothing 

    'Application.DisplayAlerts = True 

End Sub 

我從其中包含PDF文件的X路徑中的另一個函數的字符串數組。我已經驗證了這個數組,並且沒有任何問題,問題出在這個代碼上。但是我做了一個測試版本,然後將其重寫爲與我的項目一起工作,並且測試版本完美運行。代碼仍然非常相似,我在創建和融合部分上沒有任何改變。

我首先打開一個oPDDocFinal,它是我的數組「pdfs」(pdfs(0))中的第一個pdf,然後循環其餘的pdf數組以創建一個PDDoc數組。最後我循環這個PDDoc數組,將所有這些pdf與oPDDocFinal一起合併。

但我得到了在這條線的錯誤:

oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True 

我得到了以下錯誤(我試圖從法語翻譯):

執行錯誤 '91':

對象變量或與集團變量undefined

我沒有修改這部分代碼,它正在我的測試腳本,但現在我得到這個錯誤。你知道我該如何解決我的問題?

感謝您的關注。

+0

我很好奇知道'i'的價值在那一點。我的第一個懷疑是'oPDDoc(I)'出界,而不是一個對象。 – Tim

+0

好吧,你碰到了什麼。我嘗試在MsgBox中顯示所有這些變量(i,num和oPDDoc.GetNumPages()),並在這一新行上遞送錯誤。我發現我的oPDDoc是問題,它似乎沒有被定義......但是我在oPDDoc.Open的參數中輸出的路徑是正確的,我用MsgBox逐個測試了它們。有什麼方法可以測試開放函數是否成功?順便說一句,謝謝你的回答! –

+0

根據[文檔](http://help.adobe.com/livedocs/acrobat_sdk/9/Acrobat9_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Acrobat9_HTMLHelp&file=IAC_API_OLE_Objects.103.100.html),「如果失敗,open'返回-1。嘗試檢查返回值是這樣的:'intResult = oPDDocFinal.Open(pdfs(0))' – Tim

回答

1

好吧,我發現我的錯誤:

我的第一個循環,我從1開始,所以我把PDF文件(1)oPDDoc(1),但在0這樣oPDDoc(0)沒有按我的第一個循環開始」 t存在。

我定會這樣,而現在它的工作原理:

Option Explicit 

Sub Fusion_PDFs(ByVal name As String, ByRef pdfs() As Variant) 

Dim oPDDoc() As Object 
Dim oPDDocFinal As Object 
Dim Num As Long 
Dim i As Integer 

    Set oPDDocFinal = CreateObject("AcroExch.PDDoc") 
    oPDDocFinal.Open (pdfs(0)) 

    ReDim oPDDoc(UBound(pdfs)) 

    For i = LBound(pdfs) + 1 To UBound(pdfs) 

     Set oPDDoc(i - 1) = CreateObject("AcroExch.PDDoc") 
     oPDDoc(i - 1).Open (pdfs(i)) 

    Next i 


    For i = LBound(oPDDoc) To UBound(oPDDoc) - 1 

     Num = oPDDocFinal.GetNumPages() - 1 

     oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True 

    Next i 


    oPDDocFinal.Save 1, ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" 

    'Application.DisplayAlerts = False 

    'For i = LBound(oPDDoc) To UBound(oPDDoc) - 1 
    ' 
    ' oPDDoc(i).Close 
    ' Set oPDDoc(i) = Nothing 
    ' 
    'Next i 
    ' 
    'oPDDocFinal.Close 
    'Set oPDDocFinal = Nothing 

    'Application.DisplayAlerts = True 

End Sub 

感謝所有爲您的關注!

0

事情嘗試: -

  • 難道它不工作對環境有AcroExch和Word相同版本的安裝
  • 兩種環境下可以看到的PDF文件?
  • 是否存在oPDDocFinal的爭用,意思是某人或其他人打開它(This thread意味着它應該關閉以進行更新)。
  • 在調試,確實oPDDoc(我)有一個值
  • 應該把它放在括號內 - oPDDocFinal.InsertPages(Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True)

我也相信你可以在一個單一循環更容易調試。

Dim oPDDoc  As Object 
Dim oPDDocFinal As Object 
Dim Num   As Long 
Dim i   As Integer 

'Initialise objects 
Set oPDDocFinal = CreateObject("AcroExch.PDDoc") 
Set oPDDoc = CreateObject("AcroExch.PDDoc") 

'Save a working copy 
oPDDocFinal.Open (pdfs(0)) 
oPDDocFinal.Save 1, ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" 
oPDDocFinal.Close 

'Reference the working copy 
pdfs(0) = ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" 

'for all but the first item in the pdfs array 
For i = LBound(pdfs) + 1 To UBound(pdfs) 

    'Open the working copy 
    oPDDocFinal.Open (pdfs(0)) 

    'Open the additional PDF 
    oPDDoc.Open (pdfs(i)) 

    'Get the page count of the working copy 
    Num = oPDDocFinal.GetNumPages() - 1 

    'Insert the additional PDF at the end of the working copy 
    oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True 

    'Close the additional PDF 
    oPDDoc.Close 

    'Save and close the working copy PDF 
    oPDDocFinal.Save 
    oPDDocFinal.Close 

Next i 

'Release objects 
Set oPDDocFinal = Nothing 
Set oPDDoc = Nothing 

這將是一個重量級循環,但應作爲調試的起點。我還應該補充我沒有AcroExch。以上是理論。

+1

謝謝你的回答,我解決了我的問題,你似乎也注意到了這個問題。無論如何,我會回答我自己的帖子,以避免任何混淆。 –

+1

@MaximeOzenne我很高興你能解決這個問題。我仍然會推薦使用單個循環,並隨時關閉和釋放('Set oPDDocFinal = Nothing')資源,以幫助穩定性和調試。 –