#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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 |
#4
|
||||
|
||||
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 [Fmr MS MVP - Word] |
#5
|
|||
|
|||
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
|
#6
|
|||
|
|||
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 |
#7
|
||||
|
||||
I'm unable to reproduce that error. Nevertheless, I've tweaked the code - try it now.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
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 |
#9
|
||||
|
||||
The problem is that you've embedded the macro I provided inside another (WordSplitter)...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
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 |
#11
|
||||
|
||||
I've made another tweak. Try it now.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
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 |
#13
|
||||
|
||||
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#14
|
|||
|
|||
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. |
#15
|
||||
|
||||
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 [Fmr MS MVP - Word] |
|
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 |