none
VBA or C# to determine if the searched text is an entire paragraph RRS feed

  • Question

  • My Word doc has some scientific formulas in plain text (not like MS Word equation) some of which are embedded within a a paragraph along with some other text; some consist of an entire paragraph (no other text content in that paragraph). Using VBA or C#, how can I programatically determine if an expression is in fact an entire paragraph and not contained within other content in that paragraph.

    Following code searches for a scientific expression. I need to determine if the searched text is an entire paragraph or not. In other words, if an expression is an entire paragraph there will be a new line before and after the expression; maybe, that would be a way to determine that the searched text is a paragraph:

    Sub Test()
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "Some expresion consisting of an entire paragraph."
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
    End Sub

    Thursday, December 31, 2015 6:51 PM

Answers

  • If you add vbCr (= paragraph mark = ^p in the Find dialog - test it first, there) to the search string that will specify "paragraph mark preceding" and "paragraph mark following":

        .Text = vbCr & "Some expresion denoting an equation."  & vbCr


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Proposed as answer by David_JunFeng Friday, January 1, 2016 4:54 AM
    • Marked as answer by namwam Friday, January 1, 2016 4:47 PM
    Thursday, December 31, 2015 7:40 PM
    Moderator

All replies

  • If you add vbCr (= paragraph mark = ^p in the Find dialog - test it first, there) to the search string that will specify "paragraph mark preceding" and "paragraph mark following":

        .Text = vbCr & "Some expresion denoting an equation."  & vbCr


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Proposed as answer by David_JunFeng Friday, January 1, 2016 4:54 AM
    • Marked as answer by namwam Friday, January 1, 2016 4:47 PM
    Thursday, December 31, 2015 7:40 PM
    Moderator
  • Cindy, thank you. It works. Following also works:

     .Text = "^p" & "Some expression denoting an equation."  & "^p" 

    -Nam

    Friday, January 1, 2016 3:37 AM
  • Hi Nam

    Yes, and you can concatenate your version even more:

      .Text = "^pSome expression denoting an equation.^p" 


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Saturday, January 2, 2016 9:06 AM
    Moderator
  • Better yet. Thanks Cindy. -Nam
    Tuesday, January 12, 2016 2:57 PM