How can I merge documents?

T

Tim Christopher

Hi all,

I have 2 Word documents (in Word 2007). Document A contains 5 pages while
document B has 1 page. I would like to insert the single page from document B
in between pages 3 and 4 (for example) of document A but I want to do this
using a macro written in VBA in another application (Excel). What
objects/functions would I need to do this?

Thaks in advance,

Tim Christopher
 
P

Pesach Shelnitz

Hi Tim,

In your Excel code, you first need to get a Word Application object. Since
it is not a good idea to open a new instance of Word each time you run such a
macro if one is already opened, I recommend the following code for this.

Const Error_NotRunning = 429
Dim wordApp As Object

On Error Resume Next
Set wordApp = GetObject(, "Word.Application")
If Err.Number = Error_NotRunning Then
Set wordApp = CreateObject("Word.Application")
MsgBox "A new instance of Word was created."
Else
MsgBox "An open instance of Word will be used."
End If
With wordApp
.Visible = True
.Activate
End With

I included pop-up messages to show how this is working, but these can be
deleted. You can also add a Boolean value to let you close a new instance of
Word when the macro finishes.

Next you need to get a Document object for document A (I'll call it docA)
from the wordApp.Documents collection by retrieving it from this collection
if the file is open or by opening the file using the wordApp.Documents.Open
method.

Then, you can use the docA.Bookmarks("\Page") bookmark to navigate page by
page to the end of page 3, and finally call the wordApp.Selection.InsertFile
method to insert document B at that position.

Write back if you need any more help to implement these steps.
 
T

Tim Christopher

I tried but there was no luck. In tried listing all the bookmarks using the
for next loop but there were none in the file. Here is a sample of my code

Sub MergeDocuments()

Dim wrdapp As Word.Application, docSource As Word.Document, docTarget As
Word.Document, bk As Word.Bookmarks, bk1 As Word.Bookmark
Dim col As New Collection, count As Integer

Set wrdapp = CreateObject("Word.Application")

Set docSource = wrdapp.Documents.Open("c:\BillSource.docx")

Set docTarget = wrdapp.Documents.Open("c:\BillTarget.docx")

For Each bk1 In bk

col.Add bk1.Name


'If bk1.Name = "\Page" Then

'MsgBox bk1.Name

'Else

'MsgBox bk1.Name

'End If

Next bk1

'docTarget.Range.InsertFile docSource

docTarget.Close True

wrdapp.Quit

MsgBox col.count

For count = 1 To col.count

MsgBox col.Item(count)

Next count

Set wrdapp = Nothing

End Sub
Using the Bookmarks property seems very confusing!
 
D

Doug Robbins - Word MVP

To get the of each bookmark, you would need to use

For each bk1 in docSource.Bookmarks

\page is a special bookmark the range of which is the range of the page on
which the selection is located.

The following code (which I have not tested completely) should inserts the
text from the docSource document at the beginning of the third page of the
active document

Sub MergeDocuments()

Dim wrdapp As Word.Application
Dim docSource As Word.Document, docTarget As Word.Document
Dim prange as Range
Set wrdapp = CreateObject("Word.Application")

Set docSource = wrdapp.Documents.Open("c:\BillSource.docx")

Set docTarget = wrdapp.Documents.Open("c:\BillTarget.docx")

docTargetActivate
Selection.HomeKey wdStory
Set prange = docTarget.Bookmarks("\page").Range
prange.Collapse wdCollapseEnd 'prange is now at the beginning of the second
page
prange.Select
Set prange = docTarget.Bookmarks("\page").Range
prange.Collapse wdCollapseEnd 'prange is now at the beginning of the third
page
prange.FormattedText = docSource.Range.FormattedText

End Sub
--
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, originally posted via msnews.microsoft.com
 
T

Tim Christopher

Thanks Doug for your information. It seemed to work Ok until I discovered
that one of the documents was Protected from editing so that it did not allow
me to insert the other document. Someone suggested I copy the protected
document and paste it into a new Word file. I tried this using the range.Copy
and range.Paste methods but things like special characters (ticks, logos)
didn't carry over to the copied file. Is there any way to use VBA and
successfully copy ALL of the original formatting (including logos, pictures,
ticks etc ..) from the original document into the new document?

Regards

Tim Christopher
 
D

Doug Robbins - Word MVP

Replace the final command (prange.FormattedText =
docSource.Range.FormattedText) with

Source.Range.Copy
prange.Paste

--
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, originally posted via msnews.microsoft.com
 
T

Tim Christopher

It didn't work Doug. The logo and ticks did not carry over. Could they be
some kind of watermark?
Regards

Tim Christopher
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top