2014-03-12 167 views
0

挑戰:要打印多個Word文檔(從2到10的文檔最大值)到單個PDF打印多個Word文檔到PDF

可用數據:要打印的文件和它們的位置

使用的邏輯:循環遍歷具有所有文檔路徑的查詢的記錄集

碰壁:找不到最好的方法來完成是什麼?

Dim WordObj As Object 
Set WordObj = CreateObject("Word.Application") 
Dim db As DAO.Database 
Dim rs As DAO.Recordset 

Set db = CurrentDb 
Dim strSql As String 
strSql = QryLinkstoDocs 

Set rs = db.OpenRecordset(strSql, dbOpenSnapshot) 
Dim fileName As String 
Do While Not rs.EOF 
    fileName = rs.Fields(0) 'This field has the link to the files 
    WordObj.Documents.Open filename 

    'WordObj.PrintOut Background:=False 'This works for single file 

    WordObj.PrintToFile "C:\Temp.pdf", collate = True 
    rs.MoveNext 
Loop 
WordObj.Quit 
Set WordObj = Nothing 
+0

如果你可以擺動成本,我真的很喜歡Aspose。我很確定OpenXML也可以做到這一點,而且這是免費的。我更喜歡Aspose,但我沒有給OpenXML很多時間。 – mmeasor

回答

0

該做的工作:

基本上我創建了一個新的Word文檔,並插入每個文件的內容到它,並在結束時,打印單doc轉爲PDF。

Do While Not rs.EOF  
     fName = rs.Fields(0) 

     If FileExists(fName) Then 
      oApp.Selection.InsertFile _ 
      FileName:=CStr(fName), Range:="" 
      oApp.Selection.InsertBreak 
     Else 
      Debug.Print "The File is missing" 
     End If 
     rs.MoveNext 
Loop 
oApp.Activedocument.PrintOut Background:=False 
-1

我一直都喜歡pdftk (pdf toolkit)。我沒有使用它,因爲他們添加了GUI和增加的功能,但是有一個非常簡單的命令行界面,允許您組合,分離等PDF文件。您可以在Access中編寫一個簡單的腳本,生成要合併的文件列表,然後發送要執行的命令行代碼。

從他們自述的例子:

Join in1.pdf and in2.pdf into a new PDF, out1.pdf 
    pdftk in1.pdf in2.pdf cat output out1.pdf 
or (using handles): 
    pdftk A=in1.pdf B=in2.pdf cat A B output out1.pdf 
or (using wildcards): 
    pdftk *.pdf cat output combined.pdf