Splitting a Word Document

Jeff
NewLounger
Posts: 1
Joined: 23 Apr 2010, 17:32

Splitting a Word Document

Post by Jeff »

I posted this on Woody's as well, but have gotten no replies as yet. What I would like to do is split a 200+ word document into 200+ separate documents each representing a page from the former larger document. Any ideas?
Thanks in advance.

User avatar
John Gray
PlatinumLounger
Posts: 5405
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Splitting a Word Document

Post by John Gray »

I won't make the joke that splitting a 200-word document into 200 pages would only give one word per page...

It sounds like an easy job for those who understand macros in Word.
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

User avatar
HansV
Administrator
Posts: 78397
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Splitting a Word Document

Post by HansV »

Hi Jeff,

Welcome to Eileen's Lounge!

Here is a macro you can use. It's best to store it in a module in another document or in your Normal.dot(m), for the macro will close the active document.
Make sure that the document that you want to split is the active document when you run the macro.

Code: Select all

Sub SplitPages()
  Dim lngPage As Long
  Dim docCur As Document
  Dim docNew As Document
  Dim strName As String
  Dim lngPos As Long
  Application.ScreenUpdating = False
  Set docCur = ActiveDocument
  ' Get name of document
  strName = docCur.FullName
  ' Remove extension
  lngPos = InStrRev(strName, ".doc")
  If lngPos > 0 Then
    strName = Left(strName, lngPos - 1)
  End If
  ' Go to start of document
  Selection.HomeKey Unit:=wdStory
  Do While docCur.Content.End > 1
    docCur.Bookmarks("\page").Range.Cut
    lngPage = lngPage + 1
    Set docNew = Documents.Add
    docNew.Content.Paste
    docNew.SaveAs FileName:=strName & "_Page" & lngPage
    docNew.Close SaveChanges:=False
    docCur.Activate
  Loop
  Application.ScreenUpdating = True
  ' Optional
  docCur.Close SaveChanges:=False
End Sub
Best wishes,
Hans

jlkirk
2StarLounger
Posts: 168
Joined: 25 Apr 2010, 14:12

Re: Splitting a Word Document

Post by jlkirk »

Thanks Hans. How do I store it in my Normal.dotm file? I didn't know I had one!
Thanks again

User avatar
HansV
Administrator
Posts: 78397
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Splitting a Word Document

Post by HansV »

The default template is named Normal.dot in Word 2003 and before, and Normal.dotm in Word 2007 or later.
To store a macro there:
  1. Press Alt+F11 to activate the Visual Basic Editor.
  2. Click on Normal in the project explorer on the left hand side.
    x101.png
  3. Select Insert | Module to create a new code module.
  4. Copy/paste the code into the module.
Macros in Normal.dot(m) will be available in all documents.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

jlkirk
2StarLounger
Posts: 168
Joined: 25 Apr 2010, 14:12

Re: Splitting a Word Document

Post by jlkirk »

Thaks again Hans. By the way, where are the new documents stored?

User avatar
HansV
Administrator
Posts: 78397
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Splitting a Word Document

Post by HansV »

Hi Jeff,

The documents are stored in the same folder as the original document, and if the original document is named for example Report.doc, the separate documents will be named Report_Page1.doc, Report_Page2.doc etc.
Best wishes,
Hans

jlkirk
2StarLounger
Posts: 168
Joined: 25 Apr 2010, 14:12

Re: Splitting a Word Document

Post by jlkirk »

Thaks again. Great macro!

User avatar
HansV
Administrator
Posts: 78397
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Splitting a Word Document

Post by HansV »

Could you mention in your thread in the Windows Secrets Lounge (Breaking Up a Document) that the problem has been solved? Thanks in advance!
Best wishes,
Hans

jlkirk
2StarLounger
Posts: 168
Joined: 25 Apr 2010, 14:12

Re: Splitting a Word Document

Post by jlkirk »

Abvsolutely!