【VBA】將多個word文件合並到一個文件中


【VBA】將多個word文件合並到一個文件中
━━━━━━━━━━━━━━━━━━━━━━━━━
前幾天答辯,論文是用word分章寫的,每章一個word文件。
今天在線提交論文,被告知必須將所有文件合並成一個才能提交,怒。

於是就寫了個小程序來做這個事情。
不是很完美,但是夠用了。
代碼如下:

Option Explicit
Sub 
mergeFiles()
    
Dim filename As String
    Dim 
doc As Document

    Set 
doc ThisDocument
    '清除當前文檔中的內容
    
doc.Content.Delete

    '遍歷目錄中全部doc文件,調用mergeDoc過程處理
    
ChDir ("C:\test\")
    
filename Dir("*.doc")
    
Do While Len(filename)
        
mergeDoc filename, doc
        filename Dir
    Loop
End Sub

Sub 
mergeDoc(filename As String, doc As Document)
    
Dim app As Application
    Dim newDoc As Document
    Dim 
sec As Section

    Set app Application
    '打開要添加的doc文件
    
Set newDoc app.Documents.Open(filename)
    
'在當前文檔中新建一節
    
Set sec doc.Sections.Add
    '將doc文件的內容拷貝到剪貼板中
    
newDoc.Content.Copy
    '將doc文件的頁面設置拷貝到當前節
    
sec.PageSetup newDoc.PageSetup
    '將帶格式的內容從剪貼板拷貝到當前文檔
    
sec.Range.PasteAndFormat wdFormatOriginalFormatting
    '關閉doc文檔
    
newDoc.Close
End Sub


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM