JMcalpine February 2016

Search for sentences in a MSWord document using vba but doesn't work if a number is in the sentence

I realize VBA does not like #s hence I’m struggling with this code.

Do
  'Find the next sentence starting at the end of the last sentence
  Selection.Find.Text = "[^13.\?\!]{1}[!.\?\!^13]@[.\!\?]{1}"
  Selection.Find.Execute

  OldValue = Selection.Start  'This is for loop control

  'Remove the punctuation at the beginning of the sentence (that was from the last sentence
  Selection.Find.Text = "<*>*[.\!\?]{1}"
  Selection.Find.Execute
  If InStr(1, Selection.Text, FindWord, vbTextCompare) > 0 Then  
     'save the sentence
  End if

  Selection.Collapse wdCollapseEnd
  Selection.MoveLeft wdCharacter, 1, False

Loop While BreakLoop

My problem is that the first execute gets all screwy when it hits a sentence with a # in it. and Selection.Start becomes 0 (anytime it hits a sentence that has a # in it).

Any ideas? I’m thinking of putting it up on stackoverflow.

Answers


JMcalpine March 2016

Huzzah, I finally found the answer ....

Using Microsoft VBScript Regular Expressions 5.5 (from the Tools --> References menu) I was able to go through my text with #s using ActiveDocument.Sentences

 For Each s In ActiveDocument.Sentences

 Next s

It's still a little buggy - like it does not like ".," as in "e.g.," It doesn't recognize the sentence it is in or if you have "some text e.g. some more text." It breaks it up into "some text e.g." and "some more text"

I made a comment in the macro to prep the text to remove all ., because overall it works well.

Post Status

Asked in February 2016
Viewed 3,704 times
Voted 8
Answered 1 times

Search




Leave an answer