Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-04-2011, 01:17 PM
Carolin Carolin is offline Need macro to merge files in Word and preserve formatting Windows XP Need macro to merge files in Word and preserve formatting Office 2010 32bit
Novice
Need macro to merge files in Word and preserve formatting
 
Join Date: Feb 2011
Posts: 1
Carolin is on a distinguished road
Question 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
Reply With Quote
  #2  
Old 02-04-2011, 10:40 PM
macropod's Avatar
macropod macropod is offline Need macro to merge files in Word and preserve formatting Windows 7 32bit Need macro to merge files in Word and preserve formatting Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 12-14-2014, 03:30 AM
tejaspareek tejaspareek is offline Need macro to merge files in Word and preserve formatting Windows 7 64bit Need macro to merge files in Word and preserve formatting Office 2010 64bit
Novice
 
Join Date: Oct 2014
Posts: 13
tejaspareek is on a distinguished road
Default

Hi Paul,

How can we add section break option in the above code.

Many Thanks!
Regards
Tejas
Reply With Quote
  #4  
Old 12-14-2014, 04:01 AM
gmayor's Avatar
gmayor gmayor is offline Need macro to merge files in Word and preserve formatting Windows 7 64bit Need macro to merge files in Word and preserve formatting Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting macro Ulodesk Word Tables 9 04-10-2012 06:37 PM
Need macro to merge files in Word and preserve formatting macro to pull data from multiple files psrs0810 Excel 2 10-25-2010 01:49 PM
Need macro to merge files in Word and preserve formatting 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft