0

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.

JMcalpine
  • 21
  • 3
  • Well, MS Word uses complex wildcards rather than regex. You are not using the MS VBScript Regex 5.5 library here. – Wiktor Stribiżew Feb 08 '16 at 15:14
  • Could you show more code? – Tom K. Feb 08 '16 at 15:15
  • @WiktorStribiżew No, I'm not, I don't know the library well enough - any suggestions where I can find a listing of the library – JMcalpine Feb 08 '16 at 15:23
  • @Tom How much code would you like to see? A lot of it does not have to do with finding the sentence – JMcalpine Feb 08 '16 at 15:24
  • @Tom OK, I added it to the code in the original question – JMcalpine Feb 08 '16 at 15:27
  • This could be ugly, but you could use something like [this](http://stackoverflow.com/questions/18609963/vba-to-find-and-replace-a-text-in-ms-word-2010). Find an replace all "#"s (chr(35)) with a placeholder, run your script, replace again. – Tom K. Feb 08 '16 at 15:40
  • I thought of that - like 4 with four but I thought I'd do that when I'm desperate enough :) Not quite there yet. – JMcalpine Feb 08 '16 at 16:06
  • Could you please give us a sample text that this should act on that includes the problem and tell us which version of Word is involved, please? Plus show us the code that makes the Find settings and indicate which of the Find.Execute actions is causing the problem? – Cindy Meister Feb 08 '16 at 18:26
  • @CindyMeister It's Word 2010 and it is the first execute code that is not working. As for giving sample text .... I need a little time on that. – JMcalpine Feb 08 '16 at 19:08

1 Answers1

0

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.

JMcalpine
  • 21
  • 3