談論多筆Word檔案該如何一次性“自動”轉換成PDF檔案!!

如題:談論多筆Word檔案該如何一次性“自動”轉換成PDF檔案!!
這次期望使用Word的環境,開啟Word檔案、列印成PDF;重複執行!!
至於“自動化”的需求,就借用Word的巨集功能來完成。

有了這些運作概念後,接下來,我們來看看要如何做!

★ 環境需求:

  • MS Office Word
  • 需要安裝列表機:Microsoft Print to PDF

★ 使用【巨集】功能,錄製“開啟Word檔案、列印成PDF”等動作,以便產生相關的程式碼。
    你會得到下面的程式碼

ChangeFileOpenDirectory "D:\PertonChang\Desktop\靈糧神學院\107暑\新約綜覽(二)\"
Documents.Open fileName:="05-以弗所書信息與應用T.docx", ConfirmConversions:=False, _
     ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
     PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
     WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:="", _
     DocumentDirection:=wdLeftToRight

ActivePrinter = "Microsoft Print to PDF"
 
Application.PrintOut fileName:="", Range:=wdPrintAllDocument, Item:= _
     wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
     wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
     PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
     PrintZoomPaperHeight:=0
    
ActiveWindow.Close

★ 程式碼的調整
   因為,需要轉換的檔案有很多,所以調整現有的程式碼,方便處理、查看與維護。

Sub Word2Pdf()
     Dim dir As String
     Dim fileName As String
     Dim pdfName As String
    
     Call OpenWordFile(dir, fileName, pdfName)
End Sub

Sub OpenWordFile(dir, fileName, pdfName)
     ChangeFileOpenDirectory dir
     Documents.Open fileName:=fileName, ConfirmConversions:=False, _
         ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
         PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
         WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:="", _
         DocumentDirection:=wdLeftToRight
        
     Call Print2Pdf(pdfName)
        
     ActiveWindow.Close
End Sub

Sub Print2Pdf(pdfName)
     ActivePrinter = "Microsoft Print to PDF"

    Application.PrintOut fileName:="", Range:=wdPrintAllDocument, Item:= _
         wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
         wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
         PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
         PrintZoomPaperHeight:=0, OutputFileName:=pdfName
End Sub

** 上述程式碼“紅字”部份,是“增補”的,為了可以指定 PDF 檔案名稱

★ 利用 Excel 來條列出所有的 Word 檔案清單,並且產生相關檔案轉換的程式碼

★ 修改函數 Word2Pdf() 內容
修改前。。。
Sub Word2Pdf()
     Dim dir As String
     Dim fileName As String
     Dim pdfName As String
    
     Call OpenWordFile(dir, fileName, pdfName)
End Sub

修改成。。。。。。

Sub Word2Pdf()
    Dim dir As String

    dir = "D:\PertonChang\Desktop\靈糧神學院\107暑\新約綜覽(二)\"

    Call OpenWordFile(dir, "01-羅馬書信息與應用T.docx", "01-羅馬書信息與應用T.pdf")
    Call OpenWordFile(dir, "01-羅馬書寫作背景T.docx", "01-羅馬書寫作背景T.pdf")
    Call OpenWordFile(dir, "02-哥林多前書信息與應用(詳綱)T(更新版).docx", "02-哥林多前書信息與應用(詳綱)T(更新版).pdf")
    Call OpenWordFile(dir, "02-哥林多前書寫作背景T.docx", "02-哥林多前書寫作背景T.pdf")
    Call OpenWordFile(dir, "03-哥林多後書信息與應用T.docx", "03-哥林多後書信息與應用T.pdf")
    Call OpenWordFile(dir, "03-哥林多後書寫作背景T.docx", "03-哥林多後書寫作背景T.pdf")
    Call OpenWordFile(dir, "04-加拉太書信息與應用T.docx", "04-加拉太書信息與應用T.pdf")
    Call OpenWordFile(dir, "04-加拉太書寫作背景(精簡版)T.docx", "04-加拉太書寫作背景(精簡版)T.pdf")
    Call OpenWordFile(dir, "05-以弗所書信息與應用T.docx", "05-以弗所書信息與應用T.pdf")
    Call OpenWordFile(dir, "05-以弗所書寫作背景(精簡)T.docx", "05-以弗所書寫作背景(精簡)T.pdf")
    Call OpenWordFile(dir, "06-腓立比書信息與應用T.docx", "06-腓立比書信息與應用T.pdf")
    ‘  更多的清單。。。
End Sub

★ 執行巨集,按下 Alt + F8
    點選 Word2Pdf,執行
    接下來,全多是 電腦 的事情,等它完成所有的檔案轉換作業喲!!

★ 最後的完工動作 — 儲存 Word 的巨集程式碼
    想要將你辛苦完成的作品留下,就要將程式碼儲存起來。
    記得:檔案副檔名 = docm,不是 docx
    例如:Word convert to Pdf.docm

文章範例檔案:http://bit.ly/2N7T43U

First Posted:2019/08/14

沒有留言: