A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Formatting Long Documents
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Inserting many files into one document



 
 
Thread Tools Display Modes
  #11  
Old February 25th, 2008, 11:58 PM posted to microsoft.public.word.formatting.longdocs
Ricki Miles
external usenet poster
 
Posts: 70
Default Inserting many files into one document

Jay, thank you so much for the information. I tried it and it works
perfectly. Where would I put in code for a hard page break between each
document?

Thanks again,

Ricki

"Jay Freedman" wrote in message
...
Yes, that's not hard. The idea is to combine the macro that lists the
files with
the macro that puts them together:

Sub CollectDocumentsIntoOne()
Dim DestDoc As Document
Dim oRg As Range
Dim MyPath As String
Dim MyName As String

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If

Set DestDoc = Documents.Add
DestDoc.Save ' pop up SaveAs dialog

Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd

'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.doc")
Do While MyName ""
On Error Resume Next
oRg.InsertFile FileName:=MyPath & MyName, _
ConfirmConversions:=False, Link:=False
If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If
MyName = Dir
Loop

End Sub

This time you don't have to set up any file names or paths in the macro.
The
first dialog that appears when you run the macro (with the title "Copy",
unfortunately) lets you choose the folder containing the documents. Then a
second dialog ("Save As") lets you choose a folder and file name for the
combined document.


On Sun, 24 Feb 2008 16:02:28 -0600, "Ricki Miles"
wrote:
Thanks, Jay,

I tried the macro below and it works perfectly.

I do, however, have all the files in the correct order in a folder. Is it
possible to run a macro that loops through the filenames in the folder and
inserts them, rather than using the method below where you need the
filenames in an actual file?

Thanks again - much appreciated,

Ricki

"Jay Freedman" wrote in message
. ..
Suzanne, with apologies to you and Doug, the macro in the second article
isn't
going to help Ricki very much -- it prints the documents to a .prn file.
If all
you want is a printed copy of the combined document, then you can use
the
instructions in http://askbobrankin.com/printing_a_prn_file.html to
print
the
.prn file. If you want an editable document, because you need to do
formatting
such as setting the heading style to "Page break before", then this
macro
is
useless except as an example to someone who understands how to write and
adapt a
macro.

The following macro takes the list of document names and compiles those
documents into one big document. You'll need to insert the real paths
and
file
names into the macro where indicated by the three "set this" comments.
Before
you run this macro, read through the list of document names and make
sure
they're in the order you want them to appear in the final document.

Sub CollectDocumentsIntoOne()
Dim DestDoc As Document
Dim SrcDoc As Document
Dim oRg As Range
Dim NamesFile As String
Dim DestName As String
Dim oPara As Paragraph
Dim MyPath As String
Dim MyName As String

' set this to the full path and name of the file
' that contains the list of document names:
NamesFile = "C:\SomeFolder\Includes.doc"

' set this to the path of the folder (with final backslash)
' that contains the files named in the list:
MyPath = "C:\SomeFolder\Letters\"

' set this to where you want the final document
' to be saved
DestName = "C:\SomeFolder\BigDoc.doc"

Set SrcDoc = Documents.Open(NamesFile)
Set DestDoc = Documents.Add
DestDoc.SaveAs FileName:=DestName

Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd

For Each oPara In SrcDoc.Paragraphs
If Len(oPara.Range.Text) 5 Then
' name must be at least one char plus ".doc¶"
MyName = oPara.Range.Text
MyName = Left$(MyName, Len(MyName) - 1)
On Error Resume Next
oRg.InsertFile FileName:=MyPath & MyName, _
ConfirmConversions:=False, Link:=False
If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If
End If
Next oPara

SrcDoc.Close SaveChanges:=wdDoNotSaveChanges

End Sub



On Sun, 24 Feb 2008 11:37:19 -0600, "Suzanne S. Barnhill"

wrote:

Did you try the macro in the second article?

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

"Ricki Miles" wrote in message
.. .
Hi Doug,

I just tried using the macro in the article below - it inserts the
filenames into a document. What I need is the entire file text
inserted
into a document, then the next one, etc. to build a new document with
the
text of all the individual documents.

I'd appreciate some advice on how to create a macro to do this.
Thanks
again,

Ricki

"Doug Robbins - Word MVP" wrote in message
...
Take a look at the article "Insert into a document the names of all
files
in a selected folder" at:

http://www.word.mvps.org/FAQs/Macros...tFileNames.htm

and

"Print all documents in a given folder to a single print file" at:

http://www.word.mvps.org/FAQs/Macros...DocsInFldr.htm




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

"Ricki Miles" wrote in message
...
Thanks so much for your feedback. The documents should start on new
pages in the finished combined document, so your suggestion of "page
break before" for the heading style is perfect. I would like to
create
a macro that would insert each documents in the order in which they
appear in the folder, but I don't know the VBA language to make it
loop
through. I'd appreciate help with that coding. Thanks again,

Ricki

"Jay Freedman" wrote in message
...
On Sat, 23 Feb 2008 12:33:45 -0600, "Ricki Miles"

wrote:

I am working in Word 2003. I have over one thousand separate
documents
of
one or two pages of text each. There are no graphics, tables, etc.
What is
the best way to combine them into a book consisting of one
document?
I
don't want to use Master Document as I know it has many problems.
I
am
thinking of using Insert File and making the formatting all the
same.
Any
other suggestions?

TIA

Ricki


Not really a separate suggestion, but building on what you're
already
thinking...

Do you want each inserted document to start on a new page? There
are
a
couple of
ways to do that. The best, if each document starts with a heading
formatted with
the same style, is to add "Page break before" paragraph formatting
to
the
definition of that style in the final document. The alternative is
to
insert a
New Page section break between documents as you add them.

If you have a list of the file names of the separate documents, in
the
order
that they should appear, then a macro could take that list as input
and
create
the consolidated document. (If the documents aren't all on one
folder,
the list
would have to include the full path to each one.) This would be a
lot
faster
than building it by hand.

If the documents were all based on the same template and use the
same
style
definitions, you shouldn't have any problems with formatting. If
there
could be
styles with the same names but different sets of properties in
different
documents, though, you may run into trouble. See
http://www.shaunakelly.com/word/styl...xtChanges.html for
the
considerations.


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.



  #12  
Old February 26th, 2008, 01:53 AM posted to microsoft.public.word.formatting.longdocs
Jay Freedman
external usenet poster
 
Posts: 9,488
Default Inserting many files into one document

Somewhere early in the thread we discussed adding "Page break before" to the
paragraph formatting of the heading that starts each page. That's much the
easiest way.

If the pages don't all start with a heading with the same style, then you can
make the macro insert a page break, although that's less reliable (for example,
if the page before the break comes to the bottom of the page, the page break
will cause a blank page). Replace the lines

If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If

with

If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
oRg.InsertBreak Type:=wdPageBreak
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If

One more thing: This will leave a page break and a blank page at the end of the
last included document, which you should delete if you plan to print the
combined document.


On Mon, 25 Feb 2008 17:58:28 -0600, "Ricki Miles" wrote:

Jay, thank you so much for the information. I tried it and it works
perfectly. Where would I put in code for a hard page break between each
document?

Thanks again,

Ricki

"Jay Freedman" wrote in message
.. .
Yes, that's not hard. The idea is to combine the macro that lists the
files with
the macro that puts them together:

Sub CollectDocumentsIntoOne()
Dim DestDoc As Document
Dim oRg As Range
Dim MyPath As String
Dim MyName As String

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If

Set DestDoc = Documents.Add
DestDoc.Save ' pop up SaveAs dialog

Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd

'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.doc")
Do While MyName ""
On Error Resume Next
oRg.InsertFile FileName:=MyPath & MyName, _
ConfirmConversions:=False, Link:=False
If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If
MyName = Dir
Loop

End Sub

This time you don't have to set up any file names or paths in the macro.
The
first dialog that appears when you run the macro (with the title "Copy",
unfortunately) lets you choose the folder containing the documents. Then a
second dialog ("Save As") lets you choose a folder and file name for the
combined document.


On Sun, 24 Feb 2008 16:02:28 -0600, "Ricki Miles"
wrote:
Thanks, Jay,

I tried the macro below and it works perfectly.

I do, however, have all the files in the correct order in a folder. Is it
possible to run a macro that loops through the filenames in the folder and
inserts them, rather than using the method below where you need the
filenames in an actual file?

Thanks again - much appreciated,

Ricki


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
  #13  
Old March 1st, 2008, 10:34 PM posted to microsoft.public.word.formatting.longdocs
Ricki Miles
external usenet poster
 
Posts: 70
Default Inserting many files into one document

Thanks again for all the information,

Ricki

"Jay Freedman" wrote in message
...
Somewhere early in the thread we discussed adding "Page break before" to
the
paragraph formatting of the heading that starts each page. That's much the
easiest way.

If the pages don't all start with a heading with the same style, then you
can
make the macro insert a page break, although that's less reliable (for
example,
if the page before the break comes to the bottom of the page, the page
break
will cause a blank page). Replace the lines

If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If

with

If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
oRg.InsertBreak Type:=wdPageBreak
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If

One more thing: This will leave a page break and a blank page at the end
of the
last included document, which you should delete if you plan to print the
combined document.


On Mon, 25 Feb 2008 17:58:28 -0600, "Ricki Miles"
wrote:

Jay, thank you so much for the information. I tried it and it works
perfectly. Where would I put in code for a hard page break between each
document?

Thanks again,

Ricki

"Jay Freedman" wrote in message
. ..
Yes, that's not hard. The idea is to combine the macro that lists the
files with
the macro that puts them together:

Sub CollectDocumentsIntoOne()
Dim DestDoc As Document
Dim oRg As Range
Dim MyPath As String
Dim MyName As String

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If

Set DestDoc = Documents.Add
DestDoc.Save ' pop up SaveAs dialog

Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd

'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.doc")
Do While MyName ""
On Error Resume Next
oRg.InsertFile FileName:=MyPath & MyName, _
ConfirmConversions:=False, Link:=False
If Err.Number = 0 Then
DestDoc.Save
Set oRg = DestDoc.Range
oRg.Collapse wdCollapseEnd
End If
MyName = Dir
Loop

End Sub

This time you don't have to set up any file names or paths in the macro.
The
first dialog that appears when you run the macro (with the title "Copy",
unfortunately) lets you choose the folder containing the documents. Then
a
second dialog ("Save As") lets you choose a folder and file name for the
combined document.


On Sun, 24 Feb 2008 16:02:28 -0600, "Ricki Miles"
wrote:
Thanks, Jay,

I tried the macro below and it works perfectly.

I do, however, have all the files in the correct order in a folder. Is
it
possible to run a macro that loops through the filenames in the folder
and
inserts them, rather than using the method below where you need the
filenames in an actual file?

Thanks again - much appreciated,

Ricki


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 03:22 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.