#1
|
|||
|
|||
Need macro to merge files in Word and preserve formatting
I have tried macros found on the web but they don't work right. I need to merge multiple files in Word 2010, insert a page break after each file, and especially preserve all the formatting and images on each original file. This will merge the files but no page break and formatting messed up:
Sub MergeDocs() Dim rng As Range Dim MainDoc As Document Dim strFile As String Const strFolder = "V:\1 My Documents\cjd\Recipes\2011\MergeTest\" 'change to suit Set MainDoc = Documents.Add strFile = Dir$(strFolder & "*.docx") ' can change to .docx Do Until strFile = "" Set rng = MainDoc.Range rng.Collapse wdCollapseEnd rng.InsertFile strFolder & strFile strFile = Dir$() Loop End Sub Other variations, such as merge as images, have runtime errors. Any suggestions? Thank you |
#2
|
||||
|
||||
Hi Carolyn,
The main issues you will have to deal with when combining two Word documents are: 1. Page layouts (eg paper size, margins, headers & footers) that differ; and 2. The way the various document creators might have defined and used paragraph and character Styles. The first of these can largely be overcome by adding Section breaks to the source documents and copying the documents with those Section breaks to the new document. The second is more problematic, as a given Word document can have only one definition of a given Style. In practice, this means any material inserted from the source documents will adopt the Style definitions applying in the target document. With Word 2003 & later, however, you can get around this via Copy & Paste, and telling Word to retain the source formatting. Other potential issues relate to page numbering and the use of bookmarks, Tables of Contents, footnotes & endnotes. All of these issues can be dealt with, but none of them permits a straightforward copy/paste or InsertFile.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul,
How can we add section break option in the above code. Many Thanks! Regards Tejas |
#4
|
||||
|
||||
As Paul suggests, this is not at all straightforward. It also works better if you start with the first document and not a new document based on the normal template (or at least create your new document using the first document as a template and replace the document range with the content of your first document).
Inserting a section break is easy enough, you just add it to rng before you insert the file, then reset rng to the end of the document again. Or you could use http://www.gmayor.com/Boiler.htm
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Formatting macro | Ulodesk | Word Tables | 9 | 04-10-2012 06:37 PM |
macro to pull data from multiple files | psrs0810 | Excel | 2 | 10-25-2010 01:49 PM |
Cut and paste a range of cells and preserve formatting | StarWeaver | Excel | 1 | 03-02-2010 01:41 PM |
Macro to export document sections to individual txt files? | MJMR999 | Excel Programming | 0 | 02-18-2010 12:49 PM |
Help-overwriting files-could it be macro virus? | Timpotty | Word | 0 | 03-06-2009 04:28 PM |