Consulting

Results 1 to 4 of 4

Thread: Rng.Sentence(1).text

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location

    Post Rng.Sentence(1).text

    I've got the following code:
    Sub demo()
        Dim rng As Range
        
        Set rng = ActiveDocument.Paragraphs(1).Range
        
        Debug.Print rng.Sentences(1).Text
        
    End Sub
    and the following paragraph

    Blah blah blah blah test/test blah.

    Which will return what is expected ("Blah blah blah blah test/test blah."). And if I change it to

    "Blah blah blah blah test/!test blah."

    I get "test blah!" which seems to need a space after the ! otherwise it prints what is right of the !. If I change it to:

    "Blah blah blah blah test!/test blah."

    All I get is "/"

    Anyone have any idea what is happening?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    A VBA 'sentence' is not a grammatical sentence. Indeed, 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
    [Fmr MS MVP - Word]

  3. #3
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    Quote Originally Posted by macropod View Post
    A VBA 'sentence' is not a grammatical sentence. Indeed, 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.
    Thank you for the reply. What I was most curious about it why "!/" only printed out "/". Any idea?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    I can only guess what the VBA logic is in this case. It seems nothing before your ! in this case is recognised as a 'sentence' and the / following it is interpreted as a 'sentence' end.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •