PDA

View Full Version : Rng.Sentence(1).text



heedaf
10-18-2017, 06:47 PM
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?

macropod
10-18-2017, 08:14 PM
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.

heedaf
10-19-2017, 09:45 AM
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?

macropod
10-19-2017, 01:58 PM
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.