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 file pairs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mvpreq - 02 Nov 2006 04:35 GMT
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 - 02 Nov 2006 07:50 GMT
The following has not been tested, but I think it will do what you want,
assuming that you have the list of file pairs in a two column table in a
document, with the first pair of files in the second row of that table  It
assumes that the full path and filename of each file is in the table.

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
' Open the document containing the list of file pairs.  Modify the path and
filename as appropriate.
Set FileListDoc = Documents.Open("C:\FileList.doc")
Set FileList = FileListDoc.Tables(1)
For i = 2 To FileList.Rows.Count
   Set FileA = FileList.Cell(i, 1).Range
   FileA.End = FileA.End - 1
   Set DocA = Documents.Open(FileA.Text)
   Set FileB = FileList.Cell(i, 2).Range
   FileB.End = FileB.End - 1
   Set DocB = Documents.Open(FileB.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

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

>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
[quoted text clipped - 10 lines]
>
> Deepak
mvpreq - 05 Nov 2006 22:06 GMT
Dear Doug:

I tried the VBA code suggested by you but I may have done some mistakes as
nothing happens - text at bookmarks does not get pasted in the main document.
You might be able to spot some mistakes due to your experience. Files do get
opened and closed, and I do not get any error messages. Below is a record of
what I did: if you can spot some mistakes please let me know. Thanks all for
your time.

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

Is there any place where I need to write the bookmark name supposing the
bookmark names are jungle1, jungle2, jungle3, jungle4,jungle5 (Same in both
file pairs)

File list.doc is reproduced as below:

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

Deepak
macropod - 02 Nov 2006 08:08 GMT
Hi Deepak,

You may not need vba at all - check out the INCLUDETEXT field, which is
designed for this!

Cheers

Signature

macropod
[MVP - Microsoft Word]

> 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
[quoted text clipped - 8 lines]
>
> Deepak
mvpreq - 03 Nov 2006 06:17 GMT
Thanks Doug and Macropod for your timely reponses:

I will try the VBA code as suggested by Doug over the coming weekend and
let you know how it worked. Secondly can you please give me some more
information, step wise how to use INCLUDETEXT field command, both of you
please keep in mind I am not an IT professional.

Deepak

> Hi Deepak,
>
[quoted text clipped - 17 lines]
> >
> > Deepak
Shauna Kelly - 05 Nov 2006 00:54 GMT
Hi Deepak

Insert > Field. Click IncludeText in the list at the left, then complete the
form on the right.

For more information, look up IncludeText in Help (ie ordinary Word help,
not VBA help). You'll find a full description of all the switches there.

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

> Thanks Doug and Macropod for your timely reponses:
>
[quoted text clipped - 30 lines]
>> >
>> > Deepak
mvpreq - 13 Nov 2006 00:31 GMT
Dear Shauna:

Thanks For all your helpful suggestions.

Deepak

> Hi Deepak
>
[quoted text clipped - 43 lines]
> >> >
> >> > Deepak

Rate this thread:






 
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.