PDA

View Full Version : Solved: Save Word Pages or Sections as Single Individual Documents



Tricia
06-03-2004, 01:54 PM
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:dunno

Anne Troy
06-03-2004, 01:55 PM
Hi, Tricia! Welcome!

What separates them? Page breaks? Section breaks?

Anne Troy
06-03-2004, 01:58 PM
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/cindymeister/mergfaq2.htm#SepFile

Steiner
06-03-2004, 11:04 PM
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:


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

Steiner
06-03-2004, 11:07 PM
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???

Anne Troy
06-03-2004, 11:13 PM
Must be a fluke. I'll have mark007 have a look. :)

mark007
06-04-2004, 01:02 AM
Spot on, is the fact the line starts with the word Sub. I can fix it though. Will sort it now!

:)

Tricia
06-04-2004, 07:54 AM
Can this work using pages and not sections? There are no section breaks in this Document. Help!

Anne Troy
06-04-2004, 07:55 AM
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...

Tricia
06-04-2004, 09:49 AM
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

Anne Troy
06-04-2004, 09:57 AM
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
06-04-2004, 02:09 PM
Any luck, Tricia?

TonyJollans
06-04-2004, 03:50 PM
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 ..

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

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.

TonyJollans
06-04-2004, 04:00 PM
Well, I don't understand why Application.ScreenUpdating isn't suppressing things, but this runs a little cleaner ..

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

Anne Troy
06-04-2004, 04:04 PM
So, instructions:


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

TonyJollans
06-04-2004, 04:30 PM
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.

Anne Troy
06-04-2004, 04:35 PM
Thanks, Tony!

Anne Troy
06-06-2004, 03:01 PM
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.

TonyJollans
06-06-2004, 04:14 PM
I'm cool.: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.

Anne Troy
06-06-2004, 04:27 PM
Great. I've got another question I'm about to post.

Anne Troy
06-09-2004, 01:35 PM
Tricia, did this work for you? If so, we'd like to mark it solved.
Do you need more help? Just say so!
:)

Tricia
06-16-2004, 01:12 PM
That code did work Thank you very Much!!!!!!!!

Tricia
06-24-2004, 09:07 AM
I know this question is solved but I have an additional question on it. If using the breakit macro above is there a way to save the new files as pdfs and not word docs?

Anne Troy
06-24-2004, 09:26 AM
That's not a problem, Tricia. Do you have PDF writer as a choice under File-Print? If you confirm, perhaps someone can edit the code to auto-print to PDF. Also, do you know that if you create a shortcut to your PDF writer, you can simply drag LISTS of Word docs to it? I've never tried it with PDF writer, but it works with all other printers. You might want to try doing it with 2 or 3 Word docs first...

Tricia
06-24-2004, 09:58 AM
I do have adobe as a choice. The drag and drop thing kind of works with the pdf writer. It creates them but then you have to save each individual file. Since I could be doing over 100 documents at a time a very automated version would be good.