copy document to other document

G

gvanaco

Hi,

I have searched a few day's for the next problem.

I have several document in a directory.
With a simple form I would make one big document.
The form part is working, but I can't merge the documents together.

With the code below, I get only the result of the last document.

Public Sub copy_selection_from_file_2()
Dim docBron As Word.Document
Dim docOut As Word.Document
Dim rngIn As Word.Range
Dim rngOut As Word.Range

'
Set docBron = Documents.Open("D:\Data Offline\Word
\copy_test_bron")
Set rngIn = docBron.Content
Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content
rngOut.FormattedText = rngIn.FormattedText
docBron.Close (wdSaveChanges)
docOut.Close (wdSaveChanges)

Set docBron = Documents.Open("D:\Data Offline\Word
\copy_test_bron_1")
Set rngIn = docBron.Content
Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content
rngOut.FormattedText = rngIn.FormattedText
docBron.Close (wdSaveChanges)
docOut.Close (wdSaveChanges)

Set docBron = Documents.Open("D:\Data Offline\Word
\copy_test_bron_2")
Set rngIn = docBron.Content
Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content
rngOut.FormattedText = rngIn.FormattedText
docBron.Close (wdSaveChanges)
docOut.Close (wdSaveChanges)

End Sub


somebody a hint how it could work??
 
J

Jean-Guy Marcil

Hi,

I have searched a few day's for the next problem.

I have several document in a directory.
With a simple form I would make one big document.
The form part is working, but I can't merge the documents together.

With the code below, I get only the result of the last document.

Public Sub copy_selection_from_file_2()
Dim docBron As Word.Document
Dim docOut As Word.Document
Dim rngIn As Word.Range
Dim rngOut As Word.Range

'
Set docBron = Documents.Open("D:\Data Offline\Word
\copy_test_bron")
Set rngIn = docBron.Content
Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content
rngOut.FormattedText = rngIn.FormattedText
docBron.Close (wdSaveChanges)
docOut.Close (wdSaveChanges)

Set docBron = Documents.Open("D:\Data Offline\Word
\copy_test_bron_1")
Set rngIn = docBron.Content
Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content
rngOut.FormattedText = rngIn.FormattedText
docBron.Close (wdSaveChanges)
docOut.Close (wdSaveChanges)

Set docBron = Documents.Open("D:\Data Offline\Word
\copy_test_bron_2")
Set rngIn = docBron.Content
Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content
rngOut.FormattedText = rngIn.FormattedText
docBron.Close (wdSaveChanges)
docOut.Close (wdSaveChanges)

End Sub

Your code overwrites the docOut content with each docIn content... You set a
range to the docOut content, and then overwrite it with the content of the
newly opened document (docBron), and you repeat two more times...

Also, why do you keep closing/opening/saving the docOut document? Once the
variables and objects have been set, let them be!

Finally, there is no need to slow things down by saving the docBron
document. It is not modified by the code, so just close it without saving, it
will speed things up. You can also stop refreshing the screen to speed things
up a bit.

In the code below, I created a function to handle the inserting of the
content... No need to rewrite the exact same code three times... Also, if you
want to add more documents, just add one line in the main
"copy_selection_from_file_2" sub.

Try this (Untested):


Public Sub copy_selection_from_file_2()

Dim docOut As Word.Document
Dim rngOut As Word.Range

Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content

Application.ScreenUpdating = False

InsertContent rngOut, "D:\Data Offline\Word\copy_test_bron"
InsertContent rngOut, "D:\Data Offline\Word\copy_test_bron_1"
InsertContent rngOut, "D:\Data Offline\Word\copy_test_bron_2"

docOut.Close (wdSaveChanges)

Application.ScreenRefresh
Application.ScreenUpdating = True

End Sub

Function InsertContent(ByRef rngOut As Range, _
ByVal strDocIn As String)

Dim docBron As Word.Document
Dim rngIn As Word.Range

Set docBron = Documents.Open(strDocIn)
Set rngIn = docBron.Content
With rngOut
.InsertParagraphAfter
.Collapse wdCollapseEnd
.FormattedText = rngIn.FormattedText
End With
docBron.Close (wdDoNotSaveChanges)

End Function
 
A

aco

Your code overwrites the docOut content with each docIn content... You set a
range to the docOut content, and then overwrite it with the content of the
newly opened document (docBron), and you repeat two more times...

Also, why do you keep closing/opening/saving the docOut document? Once the
variables and objects have been set, let them be!

Finally, there is no need to slow things down by saving the docBron
document. It is not modified by the code, so just close it without saving, it
will speed things up. You can also stop refreshing the screen to speed things
up a bit.

In the code below, I created a function to handle the inserting of the
content... No need to rewrite the exact same code three times... Also, ifyou
want to add more documents, just add one line in the main
"copy_selection_from_file_2" sub.

Try this (Untested):

Public Sub copy_selection_from_file_2()

Dim docOut As Word.Document
Dim rngOut As Word.Range

Set docOut = Documents.Open("D:\Data Offline\Word\copy_test_doel")
Set rngOut = docOut.Content

Application.ScreenUpdating = False

InsertContent rngOut, "D:\Data Offline\Word\copy_test_bron"
InsertContent rngOut, "D:\Data Offline\Word\copy_test_bron_1"
InsertContent rngOut, "D:\Data Offline\Word\copy_test_bron_2"

docOut.Close (wdSaveChanges)

Application.ScreenRefresh
Application.ScreenUpdating = True

End Sub

Function InsertContent(ByRef rngOut As Range, _
    ByVal strDocIn As String)

Dim docBron As Word.Document
Dim rngIn As Word.Range

Set docBron = Documents.Open(strDocIn)
Set rngIn = docBron.Content
With rngOut
    .InsertParagraphAfter
    .Collapse wdCollapseEnd
    .FormattedText = rngIn.FormattedText
End With
docBron.Close (wdDoNotSaveChanges)

End Function- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -


Thanks, it's working very good.
It's more difficult than I tought.

When it was working to copy (merge) from the one document to an other
one, I should make a function to merge more documents.

I gone use the code to merge documents depending from the decision of
a flowchart.
In the end there are maybe sixty documents to merge. various from 50
to 150 pages.
 

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