Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 07-07-2018, 02:09 AM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default Splitting word doc into batches of 1000 words

Dear all,

I am a professional translator and I often need to divide my word documents into 1000 word batches, specially for personal work organization purposes. So far, I am doing this manually, which is ok if the doc is short but not so if the file contains several 1000s of words, as is often the case. Is there a way I can get Word to do this for me? I guess there is (via a macro) but can you confirm this please? And if so, where can I find this "magic" macro? Although I am not Word newbie, my command of virtual basic and other similar stuff is close to zero.

Thank you all for reading this post and for sharing any ideas.

PS: I should probably add that I run Office for Mac 2016 and (on a virtual machine) Office 365 for Windows.

Pak Notaris
Reply With Quote
  #2  
Old 07-07-2018, 03:56 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,457
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try something along the lines of:
Code:
Sub Splitter()
Dim DocSrc As Document, DocTgt As Document, i As Long, j As Long, Rng As Range, StrTgt As String
Set DocSrc = ActiveDocument
With DocSrc
  Set Rng = .Range(0, 0): j = 1000: StrTgt = Split(.FullName, ".doc")(0) & "_"
  For i = 1 To -Int(-.ComputeStatistics(wdStatisticWords) / j) 
    If .Range(Rng.Start, .Range.End).ComputeStatistics(wdStatisticWords) < j Then _
      j = .Range(Rng.Start, .Range.End).ComputeStatistics(wdStatisticWords)
      If j = 0 Then Exit For
    With Rng
      .MoveEnd wdWord, j
      .End = .Paragraphs.Last.Range.End
      Do While .ComputeStatistics(wdStatisticWords) < j
        .MoveEnd wdParagraph, wdForward
      Loop
      Set DocTgt = Documents.Add(Template:=DocSrc.AttachedTemplate.FullName, Visible:=False)
      With DocTgt
        .Range.FormattedText = Rng.FormattedText
        .SaveAs2 StrTgt & i & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        .Close False
      End With
      .Collapse wdCollapseEnd
      If .End = DocSrc.Range.End Then Exit For
    End With
  Next
End With
End Sub
The macro outputs each split to a new document, with the split occurring at the end of whichever paragraph has the 1000th word in the current block.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 07-10-2018, 04:43 AM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default

Hi Paul,
Sorry for the late reply. Thank you so much for taking the trouble to respond. Could you please enlighten me a bit more as to how should I use this? Once i run it, does it mean my docs will always be split in 1000-word segments? Thank you again for your time. Alex
Reply With Quote
  #4  
Old 07-10-2018, 04:27 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,457
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Any document you run the code on will be split into ~1000 word segments. It's a once-off exercise, though and, if you need to make further splits, re-running the macro will simply regenerate the previous splits (subject to any edits you've made there).

For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
For Mac macro installation & usage instructions, see: https://wordmvp.com/Mac/InstallMacro.html
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 07-10-2018, 10:19 PM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default

Paul, thank you so much for taking the trouble to respond. I will try this as soon as I can and I will get back to you with my results. Thank you again
Reply With Quote
  #6  
Old 07-10-2018, 10:55 PM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default

Paul,
I run this macro on Word for mac and I got the following error message:

Compile Error
Expected end sub.

I have tried a number of ways to overcome this (including making sure that End sub doesn't appear twice) but to no avail. Could you possibly help with this? Thank you so much. Best wishes, Alex
Reply With Quote
  #7  
Old 07-11-2018, 04:38 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,457
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

I'm unable to reproduce that error. Nevertheless, I've tweaked the code - try it now.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #8  
Old 07-11-2018, 05:24 PM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default

Hi again, Paul.

I copy and paste your (now tweaked) macro and I still get the same error I have attached a zip file with a screenshot, which I hope will help. Thank you for your great help, Paul. Much appreciated.
Best wishes, Alex
Attached Files
File Type: zip Screen Shot 2018-07-12 at 08.20.48.zip (317.4 KB, 1 views)
Reply With Quote
  #9  
Old 07-11-2018, 05:31 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,457
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

The problem is that you've embedded the macro I provided inside another (WordSplitter)...
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #10  
Old 07-12-2018, 10:05 PM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default

Hi again, Paul.

The attached doc has 2800 words, give or take. After running the macro it gets split into just one doc (not two or even three as one would expect). Any ideas why? Thank you again for your great help. Alex
Attached Files
File Type: docx ANONYMIZED.docx (50.4 KB, 2 views)
Reply With Quote
  #11  
Old 07-13-2018, 05:38 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,457
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

I've made another tweak. Try it now.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #12  
Old 07-13-2018, 06:32 PM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default

Paul,

I've tried it and it works! Thank you SO MUCH for your help with this. You've made a tedious part of my job a LOT easier. Thank you again. I can see you're based in Canberra. If you ever stop in Bali (where I am based) do drop me a line and I'll buy you a coffee

Best wishes,

Alex
Reply With Quote
  #13  
Old 07-13-2018, 06:33 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 857
Guessed is on a distinguished road
Default

Alex

I was finding Paul's code a bit too complex so I had a go with a different approach which seems to work. Paul's approach of jumping 1000 words removes the iteration of my approach (and probably works faster) but I thought it was simpler to increment by paragraph until we pass the limit.
Code:
Sub Splitter2()
  Dim DocSrc As Document, DocTgt As Document
  Dim lDocLength As Long, i As Long, j As Long, aRng As Range, StrTgt As String

  Set DocSrc = ActiveDocument
  lDocLength = DocSrc.Range.End - 2
  StrTgt = Split(DocSrc.FullName, ".doc")(0) & "_"
  Set aRng = DocSrc.Paragraphs(1).Range
  Do Until aRng.End > lDocLength
    i = i + 1
    Do Until aRng.ComputeStatistics(wdStatisticWords) > 1000 Or aRng.End > lDocLength
      aRng.MoveEnd Unit:=wdParagraph, Count:=1
    Loop
    Set DocTgt = Documents.Add(Template:=DocSrc.FullName, Visible:=False)
    DocTgt.Range.FormattedText = aRng.FormattedText
    DocTgt.SaveAs2 StrTgt & i & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
    DocTgt.Close False
    aRng.Collapse Direction:=wdCollapseEnd
  Loop
  MsgBox "Saved doc count: " & i
End Sub
I would predict all sorts of confusion if a breakpoint arrives in the middle of a table but I haven't done testing for that.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #14  
Old 07-13-2018, 06:45 PM
PakNotaris PakNotaris is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Jul 2018
Posts: 12
PakNotaris is on a distinguished road
Default

Hi Andrew,

Thank you for your contribution I didn't get yours quite to work yet. I created the macro and it seemed to be doing the job (ie no error dialog box) but in the end nothing happened.

By the way: is there a way the macro would split a document into 1000 word chunks ON THE DOCUMENT ITSELF, as opposed to creating x 1,0000-word docs? E.g. by creating a mark of sorts. If the macro could pull this off it'd be next to FANTASTIC

Thank you both.
Reply With Quote
  #15  
Old 07-13-2018, 06:49 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,457
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by PakNotaris View Post
By the way: is there a way the macro would split a document into 1000 word chunks ON THE DOCUMENT ITSELF, as opposed to creating x 1,0000-word docs? E.g. by creating a mark of sorts. If the macro could pull this off it'd be next to FANTASTIC
Other than by inserting page breaks or Section breaks between the segments, it's not clear what you mean. Either of those would be far simpler than the code we've already provided, but things could get complicated if you then go and start editing one of the delimited segments.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find (highlight) two and more words in a list of 75k single words in Word 2010 Usora Word 8 05-29-2018 03:34 AM
Problem with words splitting into 2 lines sharkrfish Word 2 05-21-2015 04:47 PM
Redirect mail in batches in Office for MAC 2011 mervalong Outlook 0 11-08-2011 12:17 AM
Help Splitting and images in word 2007. aligahk06 Drawing and Graphics 1 10-08-2009 12:58 AM
WORD 2003 Need help splitting a HUGE Document dlawson Word 4 04-14-2009 12:22 PM


All times are GMT -7. The time now is 12:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft