Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete pages from large word document

Status
Not open for further replies.

jskang

Programmer
Feb 18, 2003
50
GB
In my word document, I got 120 pages and I want to split up into different chapters and create individual word files. For example, chapter 1 is on page from 10 to 18 out of 120 pages, I want to delete page 19 to 120 and p1 to 9. Then save chapter1.doc How to do this? I wouldn't highlight the pages from 19 to 120. It's time consuming. There must be a quick way. Any ideas?

Many thanks for your help

JSK
 
Highlight the pages you want... for example 1-9 and cut and paste them into a new word doc then save it as whatever you want. In doing this you don't have to save the changes to the original and can keep it in its original form just in case. Do this for each chapter.

Bill
 
Well, highlight p19-120 is very very slow because of some picture attachments and mainly long texts. Is there another way on the word toolbar that say delete pages from ### to ###? Similiar to Adobe Acrobat PDF.

JSK
 
Alternatively, you could extract what pages you want, and put them into a new document. This would leave the original alone. Then save the new doc, with the extracted pages. The following does this - but I left out any save routine for the new document. It sets bookmarks for the start and end pages, selects everything in between, copies it to a new doc, then deletes the bookmarks - as you will probably want to run it again.

Code:
Sub ExtractPages()

Dim rStart As String
Dim rEnd As String
Dim r As Range
Dim aDoc1 As Document
Dim aDoc2 As Document

    Set aDoc1 = ActiveDocument
        rStart = InputBox("Start page?")
        rEnd = InputBox("End page?")
   
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=rStart
        With aDoc1.Bookmarks
            .Add Range:=Selection.Range, Name:="Start"
        End With
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=rEnd
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
        With aDoc1.Bookmarks
            .Add Range:=Selection.Range, Name:="End"
        End With
    Set r = aDoc1.Range(Start:=aDoc1.Bookmarks("Start").Start, _
        End:=aDoc1.Bookmarks("End").End)
    r.Select
            Selection.Copy
    Documents.Add DocumentType:=wdNewBlankDocument
        Set aDoc2 = ActiveDocument
            Selection.Paste
    aDoc1.Activate
        Selection.Collapse
        aDoc1.Bookmarks("End").Delete
        aDoc1.Bookmarks("Start").Delete
        Set r = Nothing
        Set aDoc1 = Nothing
    aDoc2.Activate
        Set aDoc2 = Nothing
End Sub



Gerry
 
The code shown below will accomplish the task in seconds. It will save each section as a separate document named chap<#>, incrementing the number as it goes. You will need to modify the following line in the code:
Code:
ChangeFileOpenDirectory "C:\My Documents\"
But, first:

You need section breaks (ALT+i, b, ALT+n, ENTER) at the end of every chapter, including the last chapter (last page), AND NO WHERE ELSE.

If needed, you can check your document QUICKLY for existing section breaks by:
ALT+CTRL+Home (Select Browse Object) then
click 5th icon on top row (Browse by Section)
CTRL+PageDown to jump from one section to section

Code:
Sub BreakOnSection()
      ' Used to set criteria for moving through the document by section.
      Application.Browser.Target = wdBrowseSection

      'A mailmerge document ends with a section break next page.
      'Subtracting one from the section count stop error message.
      For i = 1 To ((ActiveDocument.Sections.Count) - 1)
      
         'Select and copy the section text to the clipboard
         ActiveDocument.Bookmarks("\Section").Range.Copy

         'Create a new document to paste text from clipboard.
         Documents.Add
         Selection.Paste

      ' Removes the break that is copied at the end of the section, if any.
         Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
         Selection.Delete Unit:=wdCharacter, Count:=1
   ChangeFileOpenDirectory "C:\My Documents\"
         DocNum = DocNum + 1
        ActiveDocument.SaveAs FileName:="chap" & DocNum & ".doc"
        ActiveDocument.Close
         ' Move the selection to the next section in the document
        Application.Browser.Next
      Next i
      ActiveDocument.Close savechanges:=wdDoNotSaveChanges
   End Sub
Macro originated from which also includes macro for BreakOnPage.
 
dcompto's solution is better as it will do the whole thing in one operation, but depends on his stated emphasis that:

1. there are section breaks between chapters;
2. that there NO OTHER section breaks

It would not work properly if #2 is not exactly adhered to - so no continuous section breaks for protected areas, or any other reason.

Using page start and end could do the whole document if you used a userform to collect all the ranges (page 4 to 23, page 24 to 31, page 32 to 48 etc. - whatever), and then processed them in sequence. I did not assume that there was section breaks between chapters - although certainly good document management would do that. If there are, and no others, dcompto's solution is better, and probably faster.



Gerry
 
Many thanks to fumei and dcompto.

I managed to run fumei's program. That was good. But dcompto's program didn't work. I run dcompto's program and the compile error message say variable not defined (For i=1 ...) I don't know what to do next. Can you help this?

Thank you

JSK

 
In dcompto's specific code chunk there is no declaration of "i".

Just put:

Dim i

into the declaration area just below the name of the procedure.

Again though, this is no a working solution unless you have section breaks in every proper location, and NO others.



Gerry
 
You say highlighting is slow. Is that because you are clicking and dragging? A quicker way to highlight a big range is to click the start of the copy range, scroll to the end, then shift click the end of the range to copy. It saves having to watch the screen slowly paint everything out.

Then copy and paste as normal.
 
Gerry,

I add Dim i but the next compile error message say variable not defined (DocNum = DocNum + 1)

What I do now?

JSK
 
Sorry, guys, I don't know what would be the problem. The code I submitted works perfectly for me without adding "Dim i".

I have Windows 2000, Office 2000, and the code I submitted was cut and pasted directly from VBE.

I've now gone back and retested it and it works like a dream for me.

Before I ran the macro, I placed a "Section Break Next Page" at the end of each chapter and one at the end of the document. In addition, as stated before, if you want the documents saved someplace other than C:\My Documents, you would have to modify the following line of code accordingly:

ChangeFileOpenDirectory "C:\My Documents\"

 
That's brillant, I got Gerry's program working now. This is exactly what i am looking for. I prefer Gerry's program which is very fast and save time.

Thanks for the hard work.

JSK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top