Inserting one document into another

M

morten.slogedal

Hi

I need help with getting some functions to a VBA scrit for Word.

I want to use a dialog box to choose an option, and when that option i
selected, I want to insert an external document into the current
document.

What function(s) do I use for this?

- Morten
 
D

Doug Robbins - Word MVP

Better to give a bit more detailed information. Is it always going to be
the same document, or will the user be selecting the document in some way in
the dialog box? Where abouts in the document is the other document to be
inserted?

To answer the question as you have asked it, you would use the FormattedText
property of the Range of the document that you want to insert.





--
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
 
M

morten.slogedal

Thank you for fast response!

More detailed information;

- I have a .DOT template that holds the VBA script and some default
text.
- Within the text I have a bookmark, and on this bookmark I want to
insert an external .doc file with some other text.
- These .doc files varies as you choose an "option" from the UserForm.
- I also want to include a file on a "New page" (page 2) of the
current document from an external .doc file.

Code:
----------------
If Popup.optEierskifte.Value = True Then
ActiveDocument.Bookmarks("bkBrevinnhold").Range.Text = "test"
End If
----------------

So, basicly I need:

1. The function to include all contents of the external .doc file
instead of "test" within the template bookmark.
2. The function to insert an external .doc file to the end of the
template, as a new page within the same template.

An example would be nice :D

Thanks in advance!
 
D

Doug Robbins - Word MVP

Dim docrange as Range
Dim Target as Document, Source as Document
Set Target = ActiveDocument
Set Source = Documents.Open("Drive:\Path\Filename")
Set docrange=Source.Range
Target.Bookmarks("bkBrevinnhold").Range.Text = docrange.FormattedText
Source.Close wdDoNotSaveChanges

For the other:

With Target.Range
.Collapse wdCollapseEnd
.InsertBreak Type" = wdPageBreak
.Text = docrange.FormattedTExt
--
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
 
M

morten.slogedal

Sweet!

Very nice, that helped alot. Now it inserts correctly in the first
part.

But I would like to keep the formatting on the inserted file, as I
have tables and different fonts/setups on the inserted document.
As it is now It inserts as clean-text as a "copy&paste" filtered thru
notepad, hehe..

So inserting all this with the parameter to keep formattings and
tables etc as all I need now =)

If you would know how to do this, I would appriciate a response to
this too.

Thank you anyway for your help, saved me alot of time today! /bow
 
D

Doug Robbins - Word MVP

Try

With Target.Range
.Collapse wdCollapseEnd
.InsertFile Filename: = "Drive:\Path\Filename"
End With

I am never sure to which range the .FormattedText attribute should be
applied. It won't hurt to apply it to both.

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

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