inserting only first page from several files into a single file

S

shankar.stat

hi all,
this is what I need to do:

say I have a folder with several files, either doc or rtf files, each
file many pages;

I need to be able to have a single file which will contain only page
one from each individual file;

from previous threads, I have the code which would insert all docs into
single file;

Dim xFile As String
Documents.Add
' Get files one at a time.
xFile = Dir$("c:\TakeThese\*.doc")
Do While xFile <> ""
' Insert first file
Selection.InsertFile FileName:="c:\TakeThese\" & xFile
Selection.InsertBreak Type:=wdSectionBreakNextPage
' Get next file
xFile = Dir$()
Loop
MsgBox "Done."

however I need only page-1 from each file.. the selection.InsertFile is
now allowing me to choose particular page; I searched all over but I
did not find any help;

any help would be greatly appreciated;
 
D

Dave Lett

Hi,

I think you can use something like the following:

Dim xFile As String
Dim oDocHolder As Document
Dim oRngHolder As Range
Dim oDocToInsert As Document
Dim oRngPage As Range
Set oDocHolder = Documents.Add
' Get files one at a time.
xFile = Dir$("C:\Take These\*.doc")
Do While xFile <> ""
' Open a file
Set oDocToInsert = Documents.Open(FileName:="c:\Take These\" & xFile)
Set oRngPage = oDocToInsert.Bookmarks("\Page").Range
Set oRngHolder = oDocHolder.Range
With oRngHolder
.Collapse Direction:=wdCollapseEnd
.FormattedText = oRngPage.FormattedText
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdSectionBreakNextPage
End With
oDocToInsert.Close
' Get next file
xFile = Dir$()
Loop
MsgBox "Done."

HTH,
Dave
 
S

shankar.stat

hi Dave,
thanks for your reply;
I tried using your code but I am not sure where to tell the macro that
it needs to insert only page 1;
my attempts created a new document with several page breaks but without
the content from individual files;

I was playing around with
Set oRngPage = oDocToInsert.Bookmarks("\Page").Range

but I was not able to get it to work;

thanks in advance.
 
D

Dave Lett

Did you try running the routine without modifying it? It worked, as is, on
my machine.

The line
Set oRngPage = oDocToInsert.Bookmarks("\Page").Range
is telling Word to only reference the first page. You don't have to modify
that line at all.

HTH,
Dave
 
S

shankar.stat

hi Dave,
I ran the code as it is..and I got a document with all pages from all
documents..

the files I have in test folder are .RTF files, not sure if thats
causing any trouble..
also the content of these files are tables and graphs..

do you think these factors make a difference?

thanks a lot;
 
D

Doug Robbins - Word MVP

Try

Dim xFile As String
Dim Source As Document
Dim Target As Document
Set Target = Documents.Add
' Get files one at a time.
xFile = Dir$("c:\TakeThese\*.doc")
Do While xFile <> ""
' Insert first file
Set Source = Documents.Open(xFile)
Source.Range.Select
Source.Range.Collapse wdCollapseStart
Target.Range.InsertAfter Source.Bookmarks("\page").Range
Target.Range.Collapse wdcollapse.End
Target.Range.InsertBreak wdSectionBreakNextPage
' Get next file
xFile = Dir$()
Loop
MsgBox "Done."

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

shankar.stat

hi Doug,
thanks for your response;
I tried your suggestion, but I still got a document with all pages
included...

thanks.
Shankar
 
D

Doug Robbins - Word MVP

The following only inserts the first page of each document for me:

Dim xFile As String
Dim Source As Document
Dim Target As Document
Set Target = Documents.Add
' Get files one at a time.
xFile = Dir$("c:\Troy\*.doc")
Do While xFile <> ""
' Insert first file
Set Source = Documents.Open("c:\troy\" & xFile)
Source.Range.Select
Source.Range.Collapse wdCollapseStart
Source.Range.Paragraphs(1).PageBreakBefore = True
Target.Range.InsertAfter Source.Bookmarks("\page").Range.FormattedText
Source.Close wdDoNotSaveChanges
' Get next file
xFile = Dir$()
Loop
MsgBox "Done."


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