Automatically Extract last 50 pages of mutliple documents

I feel this to be a extremely difficult problem. I have searched through Expert Exhange and numerous other website and forums. It is extremely urgent as my client needs this project completed.
I have several thousand .rtf files that I need to copy the last 50 pages of each file into another file so as to end up with several thousand files of 50 pages each, but only the last 50 pages. The files are various sizes. If I were after the first 50 pages, or if they were all identical size, this would be a no brainer. This needs to be automated to run through all several thousand files.
LVL 2
sscottincanyonAsked:
Who is Participating?
 
billmercerCommented:
This should be easy to do as a macro. You can use the macro recording feature to create most of it, and then just insert a few tweaks to make it more flexible.

Basically, you create a macro that goes to the very end of the document, then backs up 50 pages, starts a selection, jumps to the beginning of the document, and deletes the selection. At this point you then have a document with just the last fifty pages.
Then you just save the document with a modified filename.
Assign this macro to a button on a toolbar, and then open each file and click the button, open and click, open and click, etc.

Here's a quick example...

Sub SaveLast50()

    TruncatedFileName = "Last50PagesOf" & ActiveDocument.Name
    Selection.EndKey Unit:=wdStory
    For i = 1 To 49
      Application.Browser.Previous
    Next i
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
    Selection.Delete
    ActiveDocument.SaveAs FileName:=TruncatedFileName
End Sub

To make this more automatic, it's not too difficult to add a "for each" loop to the macro so that it will process every file in a folder.
0
 
mish33Commented:
Use i.e. MS Word to paginate whole document and cut last 50 pages to a new doc, repeat for each doc.
0
 
sscottincanyonAuthor Commented:
Thanks for the help. The macro works great on a single document. I guess I am still not quite getting it. I have not been able to get the "for each" loop to work within the macro.  I have tried starting with

 strDoc = Dir(Path & "*.Doc")
    Do While strDoc <> ""

 and have tried various combinations to complete the loop, but it just isn't working. Any ideas? This is critical as I have over 14,000 documents and even the open, click, open, click will just take too long.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
sscottincanyonAuthor Commented:
Here is what I have so far and the error I am getting back.

Sub SaveLast24()

Dim Path As String
Dim strDoc As String
Dim Doc As Document

 Path = "c:\ahg_files_orig\test_group"
 strDoc = Dir(Path & "*.rtf")
 
 
   Do While Doc <> ""
   Set Doc = Documents.Open(FileName:=Path & Doc)

   
   Selection.EndKey Unit:=wdStory
   For i = 1 To 23
     Application.Browser.Previous
   Next i
   Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
   Selection.Delete
   ActiveDocument.Save
   
   Doc.Close
   strDoc = Dir
Loop
End Sub


Error when running:
Run-time error 91
Object variable or With block variable not wet

0
 
sscottincanyonAuthor Commented:
Bill Mercer:
Thank you for the guidance on creating this VB Macro, with a little tweaking and adding in a loop control it is humming right along. Now all I have to do is convert all the documents to use arial instead of the awful font that it is defaulted with.

Again I really appreciate the help and the stellar job!!!

THANK YOU!!!!

Shon
0
 
billmercerCommented:
Glad to hear it worked for you. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.