Insert pages from one document into another using VBA

O

Office Developer

Hi

I have document 1 opened and on the click of a button I would like to insert
pages from document 2 at specific places in document 1. I know that we can
insert a whole document into another document using insertfile method, I
don't know how to only copy and insert a specific page from 1 document into
another using VBA.

Thanks.
 
C

Charles Kenyon

You can't because a page doesn't really have meaning to Word. But, you can
bookmark text (which can be a page) in document 2 and import that text using
the insertfile method or an IncludeText field.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
S

sparky191

Heres another way.

Open a doc 1 then doc2. Activate doc2 goto the page you want. Select
it, then activate the doc1 and insert where the cursor is. Heres a
rough guide of how it works. Note you should reference the docs
properly as objects. I've just done it via windows because I'm lazy
here.

Sub Macro1()

Windows(2).Activate
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
Selection.Bookmarks("\Page").Select
Selection.Copy

Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
End Sub
 
H

Helmut Weber

Hi Sparky,

let me support Charles, though he doesn't need it.
Very strictly speaking,
you cannot insert a page from another document,
only the contents of a page.

Which may lead to quite a bit of confusion,
if pagebreaks are automatic and paragraphs
span over page borders.

By the way, don't mix up windows and documents.
A document may have two windows.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
C

Charles Kenyon

The thing is that normally you cannot reliably predict what Word will
consider to be a page. It can change from computer to computer. Bookmarks
are where you put them.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
S

sparky191

Theres an echo round here... : )
By the way, don't mix up windows and documents. A document may have two windows.

I take your point but like I said..."Note you should reference the docs
properly as objects. I've just done it via windows because I'm lazy
here."
you cannot insert a page from another document,
only the contents of a page.

Like Charles said "You can't because a page doesn't really have meaning
to Word."

But to be honest I prefer

"The thing is that normally you cannot reliably predict what Word will
consider to be a page. It can change from computer to computer.
Bookmarks
are where you put them. "

Hard to know what best suits the OP "Office Developer" unless you have
a better idea of the content is. Sometimes I store content in tables
in a 2nd doc for use as an ini file or data store. Mainly because I
know the people editing the content have poor word skills and find it
easier to deal with tables than bookmarks.

However the point HAS already been made, that a "page" in word is not
helpful for this task. It is possible to do it though.

When coding anything you should always try to make your code robust
enough to handle the unexpected events. Like the page has been edited
and the content is now on page 22 and not 21. Or the the 2nd document
has been renamed or moved and can't be found.
 
O

Office Developer

Thanks to all of you, especially to Charles. I was able to bookmark text in
document 2 and then import text using insertfile method and it worked.

But I have another issue now, basically how to insert the bookmark text from
document 2 to document 1 in a separate page.
I have to do this process several times(doc1 is linked to a excel worksheet
and whenever the excel sheet data changes, doc1 also changes and we have to
run this process again to insert the bookmark text).
First time I copy the bookmark text from doc 2 to doc1, the bookmark also
gets copied(I copy the bookmark text into a blank page in doc1). Second time
I first delete the bookmark's range in doc1 and then use insertfile to copy
the bookmark text and also to re-create the bookmark in doc1.
It seems to work fine, but then the page formatting gets messed up. If doc 1
is a 5 pages document, it becomes 4 or 6 or sometimes remain as a 5 pages
document depending on the bookmark text.
I want to insert the bookmark text in a separate page irrespective of the
text size and want doc1 to remain as a 5 pages document.
I tried inserting page breaks etc but was not getting a consistent result.
Hope I clearly explained the problem.

Thanks.
 

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