Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

VBA code for merging word files

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mvpreq - 07 Nov 2006 23:21 GMT
Please note this a followup question to my previous post of Nov 1, 2006,
where Doug Robbins MVP had suggested the following VBA code for merging data
at specific bookmarks. I need more help that is why I am posting a fresh
question again, so please forgive me for this.  I have tried following code
and it causes only one particular book mark to be inserted multiple times
instead of each similar named bookmark to be inserted in it's respective
bookmark. For example the file in the the second row FileA1 has 5 bookmarks
with common bookmark names as fileB1. When I run this macro, the file at row
2: FileA1 gets five copies of the bookmark no. 2 and none of the other
bookmarks get respective text pasted.  

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 11/5/2006 by Deepak
Dim FileListDoc As Document
Dim FileList As Table
Dim FileA As Range
Dim FileB As Range
Dim DocA As Document
Dim DocB As Document
Dim bmName As String
Dim i As Long
Dim j As Long
Set FileListDoc = Documents.Open("C:\Documents and Settings\Administrator\My
Documents\Word Programs\test folder for merge\FileList.doc")
Set FileList = FileListDoc.Tables(1)
For i = 2 To FileList.Rows.Count
   Set FileA1 = FileList.Cell(i, 1).Range
   FileA1.End = FileA1.End - 1
   Set DocA = Documents.Open(FileA1.Text)
   Set FileB1 = FileList.Cell(i, 2).Range
   FileB1.End = FileB1.End - 1
   Set DocB = Documents.Open(FileB1.Text)
   For j = 1 To DocA.Bookmarks.Count
       bmName = DocA.Bookmarks(i).Name
       DocA.Bookmarks(i).Range.Text = DocB.Bookmarks(bmName).Range.Text
   Next j
   DocA.Save
   DocA.Close
   DocB.Close wdDoNotSaveChanges
Next i
'
End Sub


File list.doc is reproduced as below:

Table1
DocA                   DocB
C:\merge\FileA1    C:\merge\FileB1
C:\merge\FileA2    C:\merge\FileB2

I have a main document, say DocA in which I have created bookmarks where I
need to insert specific information from another file say DocB. DocB has
similar named bookmarks and the information which needs to be assimilated in
the main document, DocA. These are text blocks or other alpha-numeric
characters. How to import this information at it's proper place in the main
document automatically. What steps I need to take to do this automatically
for each pair of files, and can multiple pair of files be done with one
command.

Thanks

Deepak
Doug Robbins - Word MVP - 08 Nov 2006 02:33 GMT
Sorry, as I said, I had not tested this.  Looking at it again, the following
lines of code:

   For j = 1 To DocA.Bookmarks.Count
       bmName = DocA.Bookmarks(i).Name
       DocA.Bookmarks(i).Range.Text = DocB.Bookmarks(bmName).Range.Text
   Next j

should be:

   For j = 1 To DocA.Bookmarks.Count
       bmName = DocA.Bookmarks(j)Name
       DocA.Bookmarks(j).Range.Text = DocB.Bookmarks(bmName).Range.Text
   Next j

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Please note this a followup question to my previous post of Nov 1, 2006,
> where Doug Robbins MVP had suggested the following VBA code for merging
[quoted text clipped - 67 lines]
>
> Deepak
mvpreq - 13 Nov 2006 00:28 GMT
Thanks Doug:

Sorry for delay in response, it works perfectly. Thanks for all your time
and efforts.

Deepak

> Sorry, as I said, I had not tested this.  Looking at it again, the following
> lines of code:
[quoted text clipped - 82 lines]
> >
> > Deepak
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.