Jack Medford
Jack Medford

Reputation: 15

Is it possible to separate text into chunks of a specified length?

I am working on a project that analyzes how well various algorithms summarize various lengths of text. I have a number of ebooks formatted as Word documents that I am using. So far I am just copying and pasting the exact number of words I want to test every time, which is very tedious.

I am not very familiar with VBA, but upon research it seems like this may be an option. Is there a way for a macro to separate a long Word document into chunks of specified numbers? For example, extract the first 750 words into a new document. If easier, creating a page break after every 750 words could also work.

There may be a better way to go about this, so any other suggestions would also be much appreciated.

Upvotes: 0

Views: 55

Answers (1)

Jerry Jeremiah
Jerry Jeremiah

Reputation: 9643

What about this:

Sub CopyWords()
    Dim InputString As String
    Dim Words As Integer
    Do
        InputString = InputBox("How many words?", "CopyWords")
        If InputString = "" Then Exit Sub
        Words = Val(InputString)
        If Words = 0 Then MsgBox "You can't have """ & InputString & """ words!"
    Loop While Words = 0
    Selection.MoveRight Unit:=wdWord, Count:=Words, Extend:=wdExtend
    If Selection.Range.ComputeStatistics(wdStatisticWords) < Words Then
        MsgBox "There aren't " & InputString & " words available from this point"
    Else
        Selection.Copy
    End If
End Sub

It asks for a value and then if it is a number tries to select that many words from the cursor. If there are enough it copies them to the clipboard.

Upvotes: 1

Related Questions