none
Count Words in Document and Create Separate Documents after a Word Count RRS feed

  • Question

  • Hi All,

    I have a macro that will look for a specified delimiter in a file and based on that delimiter it will save a file off everytime it hits that.

    So for instance: 

    The sky is blue. I am playing baseball today. || What do you want to do after baseball. I would like to go to the movies. I heard the new Spider-Man movie is out. || What time do you want to go? Maybe around four. I am not sure.

    This would cut up into 3 separate word files. First file would have the first two sentences before the first delimiter and then the next file would have the 3 sentences before the second delimiter. And finally third file would have the last 3 sentences.

    What I am looking to do is instead of manually putting in this delimiter throughout the file to either A) put the delimiter into the file after x (variable) amount of characters. OR B) combine the two macros into 1 and call both procedures at the same time. 

    Of course there is a caveat, I need to make sure that the delimiter is not put in mid sentence so I would need to look for a certain amount of characters and if the count total isn't a period, exclamation point, question mark then I would need to continue moving along the string until it hits the next sentence ending.

    I have been trying to find a starting point but am new to VBA.

    Thanks

    Chris

    Saturday, July 15, 2017 9:00 PM

All replies

  • There is a fundamental problem in what you're trying to achieve, in that VBA has no idea what a grammatical sentence is. For example, consider the following:
    Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
    For you and me, that would count as one sentence; for VBA it counts as 5 sentences.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, July 16, 2017 5:49 AM
  • Hi Paul,

    So is there a way of counting the words in an entire document. Setting a variable to that count and then creating a  loop that would add a || after every 1000 words and repeat until you have hit the end? My guess would be that you would need to have a few variables (maybe called startpoint, another called endpoint and another called totalcount). You're start point variable would be the very first word and endpoint would be 1000 words later. If the character to the right of the last word is a  . ? or ! then reset your startpoint variable to the next word which would be 1001 and begin loop again.. If the last character is NOT a . ! or ? then create a subloop that would continue to count each additional word until it hits a . ! or ? once it hits that jump out of the loop and go back to the main loop and reset startpoint to the new value.

    Does that make sense?

    My background has been more javascript or versions of that and SQL so VBA is all new to me.

    Thanks

    Chris

    Sunday, July 16, 2017 11:28 PM
  • You could do something like the following in VBA:

    Sub DocMarker()
    Dim i As Long, j As Long, Rng As Range
    With ActiveDocument
      Set Rng = .Range(0, 0): j = 250
      For i = 1 To Int(.ComputeStatistics(wdStatisticWords) / j)
        With Rng
          .MoveEnd wdWord, j
          Do While .ComputeStatistics(wdStatisticWords) Mod j <> 0
            .MoveEnd wdWord, j - .ComputeStatistics(wdStatisticWords) Mod j
          Loop
          .End = .Sentences.Last.End
          .InsertAfter "|| "
          .Collapse wdCollapseEnd
        End With
      Next
    End With
    End Sub

    As coded, the above macro inserts || after the end of every VBA 'sentence' that includes the 250th word since the last such insertion. Change the 250 in 'j = 250' to suit your requirements.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, July 17, 2017 10:47 PM