Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 25

Thread: Solved: Save Word Pages or Sections as Single Individual Documents

  1. #1
    VBAX Regular
    Joined
    Jun 2004
    Posts
    14
    Location

    Solved: Save Word Pages or Sections as Single Individual Documents

    Is there a way to save every page of a document as individual documents? I need to send different pages of a report to different people! Help

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hi, Tricia! Welcome!

    What separates them? Page breaks? Section breaks?
    ~Anne Troy

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    See if Cindy Meister's method is helpful, and let us know how it works out, or tell us what's wrong. Make sure you do any testing on a COPY of your file.

    http://homepage.swissonline.ch/cindy...q2.htm#SepFile
    ~Anne Troy

  4. #4
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Ok, here's a small method that will separate a document based on sections, so make sure you have section breaks in position. The subdocuments will be saved just where the main document is, they have the same name + a number for each section:

    [VBA]
    Sub BreakIt()
    Dim MainDoc As Document, SubDoc As Document, SectionNo%, sPath$

    Set MainDoc = ActiveDocument
    sPath = MainDoc.Path
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"

    For SectionNo = 1 To ActiveDocument.Sections.Count
    ActiveDocument.Sections(SectionNo).Range.Copy
    Set SubDoc = Application.Documents.Add
    SubDoc.Range.Paste
    SubDoc.SaveAs sPath & Left(MainDoc.Name, Len(MainDoc.Name) - 4) & _
    SectionNo & ".doc"
    SubDoc.Close
    Next SectionNo
    Set SubDoc = Nothing
    Set MainDoc = Nothing
    End Sub

    [/VBA]

  5. #5
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Why does my code look so messy?? I did not add the indention on the lines beginning with SubDoc!! Could it be the that SubDoc contains the keyword Sub???

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Must be a fluke. I'll have mark007 have a look.
    ~Anne Troy

  7. #7
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Spot on, is the fact the line starts with the word Sub. I can fix it though. Will sort it now!

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  8. #8
    VBAX Regular
    Joined
    Jun 2004
    Posts
    14
    Location
    Can this work using pages and not sections? There are no section breaks in this Document. Help!

  9. #9
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Are there manual page breaks, Tricia? Or are the pages broken by use of formatting styles?

    Also, if there ARE manual page breaks, you can quickly turn them into section breaks using Find/Replace. Just hit Ctrl+H and then the More and Special buttons to find the way to do that. It would be quicker to do that than to rewrite the code (Maybe!). LOL

    Let me know if you have difficulty...
    ~Anne Troy

  10. #10
    VBAX Regular
    Joined
    Jun 2004
    Posts
    14
    Location
    They are not manual. The report is actually EFT advices from our accounting software that I want to e-mail to people. Any more sugestions? please

  11. #11
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Well...in order to break the documents up, we need to know where they stop and start. For instance, is there some text at the top of the document that's unique? Suppose it says "Financial Report for ... " at the top of every page. Then, you can simply do a find/replace. Find "Financial Report for" and replace it with a section break and the same words. Understand?

    Then, once you have a section break for each document, you should be able to use the macro.

    If you don't understand or can't work it out, let us know the basic layout of your file. We can't guess.
    ~Anne Troy

  12. #12
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Any luck, Tricia?
    ~Anne Troy

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    No need to bother messing around with Sections if all you want is Pages.

    It is possible, using either Gotos, or the Browser, to move through a document a page at a time. It is possible in code to Select the current page using the built-in bookmark, "\Page".

    That should be all you need to know. Here's some code based on it ..

    [vba]Sub SavePages()

    Dim MainDoc As Document, PageDoc As Document
    Dim lPageNo As Long
    Dim strNameStem As String

    Set MainDoc = ActiveDocument
    strNameStem = Left(MainDoc.FullName, Len(MainDoc.FullName) - 4)

    lPageNo = 1
    Application.Browser.Target = wdBrowsePage
    Application.ScreenUpdating = False
    MainDoc.Range(0, 0).Select

    Do
    Selection.Bookmarks("\Page").Select
    Selection.Copy

    Set PageDoc = Application.Documents.Add
    PageDoc.Range.Paste
    PageDoc.SaveAs strNameStem & " - Page " & lPageNo & ".doc"
    PageDoc.Close

    If lPageNo = MainDoc.Range.Information(wdNumberOfPagesInDocument) Then _
    Exit Do
    Application.Browser.Next
    lPageNo = lPageNo + 1
    Loop

    Application.ScreenUpdating = True
    Set PageDoc = Nothing
    Set MainDoc = Nothing

    End Sub[/vba]

    I must just say that when I tested this the screen was flashing an awful lot - I'll play with it and get it sorted. Meanwhile it should do the job for you.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Well, I don't understand why Application.ScreenUpdating isn't suppressing things, but this runs a little cleaner ..

    [vba]Sub SavePages()

    Dim MainDoc As Document, PageDoc As Document
    Dim lPageNo As Long
    Dim strNameStem As String

    Set MainDoc = ActiveDocument
    strNameStem = Left(MainDoc.FullName, Len(MainDoc.FullName) - 4)

    lPageNo = 1
    Application.ScreenUpdating = False

    MainDoc.Range(0, 0).Select

    Do
    Selection.Bookmarks("\Page").Select
    Selection.Copy

    Set PageDoc = Application.Documents.Add(Visible:=False)
    PageDoc.Range.Paste
    PageDoc.SaveAs strNameStem & " - Page " & lPageNo & ".doc"
    PageDoc.Close

    If lPageNo = MainDoc.Range.Information(wdNumberOfPagesInDocument) Then _
    Exit Do
    Selection.GoToNext wdGoToPage
    lPageNo = lPageNo + 1
    Loop

    Application.ScreenUpdating = True
    Set PageDoc = Nothing
    Set MainDoc = Nothing

    End Sub[/vba]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  15. #15
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    So, instructions:

    1. Open the document.
    2. Hit Alt+F11 to get to the VB Editor (VBE).
    3. From the menu, choose Insert-Module.
    4. Paste the code above into the window at right.
    5. Hit the save diskette icon.
    6. Close the VBE with the X.
    7. Go to Tools-Macro-Macros and double-click on "SavePages".
    If I have that right, we can make it a kbase entry.
    ~Anne Troy

  16. #16
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    You have it almost right

    I wouldn't include #5 - not without comment anyway. For all sorts of reasons the User might not want to save the Document at that point, but they need to understand that the code is part of the document and if they don't save it, it won't be there next time they open the document. Depending on what the code is they might want to put it in a Template rather than the Document.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  17. #17
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Thanks, Tony!
    ~Anne Troy

  18. #18
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Tony...so this code needs manual page breaks or not?

    Sorry. I want to add it to the kbase...
    I'll put your name on it, if it's cool with you.
    ~Anne Troy

  19. #19
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I'm cool.

    No manual page breaks, or anything else special needed; it works on any document as is. The built-in bookmark gives you the current page (including any trailing pagebreak); only downside I see is that it only works with the Selection - you can't use it on a different Range.

    Don't know what happenned to the half-submission I did last weekend, but I've got an almost complete version of that to submit as well - I got a bit sidetracked (hard to believe, I know) writing some instructions and will probably end up with an article about navigating the VBE as well.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  20. #20
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Great. I've got another question I'm about to post.
    ~Anne Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •