用合併列印來建立獨立文件

不懂怎用 Mail Merge (合併列印), 請參看官方教學 (這是 Office 2010 版)

當完成合併列印後, 但又不想列印出來, 想分別儲存成為獨立文件, 怎樣做呢? 原來 Word 並沒有這項功能 但可以用 Marco 來實現。



Sub BreakOnSection()
'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("\Section").Range.Copy

'Create a new document to paste text from clipboard.
Documents.Add
Selection.Paste

'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

ChangeFileOpenDirectory "C:\"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

原文連結


備注:

  1. 它是以分節符號來區分文件, 所以如果文件中原先就有分節符號, 咁就唔用得呢個方法啦。
  2. 改儲存目的地: ChangeFileOpenDirectory "C:\", 因為最好不要直接儲存去 C:\ 裡面, 可以開新資料夾 , 例如 "C:\MergeResult\" 會較好。
  3. 改新文件名, ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc", 不改就會儲成test_1.doc, test_2.doc 咁樣。




留言

這個網誌中的熱門文章