PDA

View Full Version : Problem when '/' at end of first sentence



GOGGS75
10-05-2010, 12:39 PM
Hi,

I have come across a basic problem with ActiveDocument.Sentences I was looking for some help on.

If the line below is entered as a paragraph on WORD

THIS IS SENTENCE 1./ THIS IS SENTENCE 2./ THIS IS SENTENCE 3./

ActiveDocument.Sentences(1) picks up "/ "
ActiveDocument.Sentences(2) picks up "THIS IS SENTENCE 2./ "
ActiveDocument.Sentences(3) picks up "THIS IS SENTENCE 3./ "

The problem left being Sentences(1) does not pick up "THIS IS SENTENCE 1." It only picks up the very end of it.

After some simple testing I could see that if a space is entered between the period and the slash at the end of the first sentence "1. /" it picks it up in full.

Is there any other way so solve the problem other than altering the text.

Thanks for any help.

fumei
10-05-2010, 12:59 PM
Not really. You could do a Replace - "/" with " /" - get your sentences, and then put back the way it was. Kludgey I know.

What exactly are you doing this for?

macropod
10-05-2010, 02:21 PM
Here's a workaround:

Sub Test()
Dim i As Long, oRng As Range
With ActiveDocument
For i = 1 To .Sentences.Count
If i = 1 Then
Set oRng = .Range(0, .Sentences(i).End)
Else
Set oRng = .Range(.Sentences(i - 1).End, .Sentences(i).End)
End If
MsgBox oRng.Text
Next
End With
End Sub

fumei
10-06-2010, 09:45 AM
Paul, do you understand why it is doing this? I have never seen it before, and I do not understand. Why would a "/" immediately following a period make the slash the Sentence? Sentence(1) is only the slash. What? So the text preceding is somehow Sentecence(0)? Seems very weird to me.

The weirdest part is that if you have:

THIS IS SENTENCE 1./ THIS IS SENTENCE 2./ THIS IS SENTENCE 3./

Sub Yadda()
Dim oSentence
For Each oSentence In ActiveDocument.Sentences
MsgBox oSentence
Next
End Subyou get:

/
/
/

Three messageboxes with the content of the three sentences...but they ONLY have the slash. All other content is ignored. What is it about the slash that screws it up? I am very curious.

GOGGS75
10-06-2010, 11:46 AM
Hi Guys

Thanks for your replies. I think I am going to alter the necessary text in a 30 page template I use.

A macro I have set up detects how many coloured characters exist in the selected range and displays them on a userform so that they can be selected for deletion. It looks at each paragraph and starts of looking at the colour of the whole paragragh. If mixed colours exist it drops down to looking at the paragraph by 'sentences', then by words if necessary and finally by characters if necessary.

As the '/' was in place to divide alternative wording in the document the problem with sentences was causing problems.

It will not be difficult to change the text but am disappointed in this VBA weekness.

Cheers

Goggs75

fumei
10-06-2010, 12:05 PM
Well...

your formatting (the variety of colours) seems very strange to me. However, I do not have enough details to really make a comment.

Are you using Styles fully?

"As the '/' was in place to divide alternative wording in the document"

I do not know what that means, practially speaking.

In any case, I am also not sure if VBA weakness is accurate. It may be, but I do not know. I have thought so on issues before, and then with better understanding of the actual objects determined that - weird - but yes, that makes sense.

I do not know why a slash like that appears to override everything. It sure seems wrong.

fumei
10-06-2010, 12:17 PM
It gets better.

It does not depend on the slash (forward or backwards). They both affect things the same way.

A dollar sign ($) does as well.
THIS IS SENTENCE 1.$ THIS IS SENTENCE 2./ THIS IS SENTENCE 3./

This is the same for all non-letter characters, except for the exclamation mark (!). The ! makes Sentence(1) = THIS IS SENTENCE 1.!

However, and this is very weird to me, any alpha character makes what was "Sentences(1)" go completely away.

THIS IS SENTENCE 1.g THIS IS SENTENCE 2./ THIS IS SENTENCE 3./

Sentences(1) = the first slash (after 2.)
Sentences(2) = the second slash (after 3.)

SAY WHAT???????????

macropod
10-06-2010, 02:56 PM
Paul, do you understand why it is doing this? I have never seen it before, and I do not understand. ... Seems very weird to me.

The weirdest part is that if you have:

THIS IS SENTENCE 1./ THIS IS SENTENCE 2./ THIS IS SENTENCE 3./
...
you get:

/
/
/
...
I am very curious.
Hi Gerry,

I have no idea why - all I know is the workaround works!

Paul_Hossler
10-06-2010, 03:35 PM
interesting.

According to

http://www.thedailyreviewer.com/windowsapps/view/in-word-vba-what-is-a-sentence-10646028

.Sentences(x) is handled differently if it's the first one, in the middle, or at the end.

FWIW, a blank paragraph in front of the text, returns 4 sentences

blank
THIS IS SENTENCE 1./
THIS IS SENTENCE 2./
THIS IS SENTENCE 3./


Paul

macropod
10-06-2010, 09:46 PM
Hi Paul,

The treatment of an empty paragraph as a sentence in its own right is understandable, since sentences cannot physically span more than one paragraph (though logically they might, as when one uses bullet points).

Paul_Hossler
10-07-2010, 12:24 PM
mac -- agreed -- it is understandable

I was just experimenting with the information in the link, which says that first, last, and middle sentences in a paragraph are treated differently, and that 'empty' paragraphs are part of the sentence

When the troublesome example is preceded by an empty para, the ./ does not seem to affect the results.

Just an observation

Paul

fumei
10-07-2010, 12:37 PM
If a sentence has bullets, the bullets are separate sentences...in other words a sentence can NOT have multiple bullets. The second (or more) bullet is considered a different paragraph (and thus a different sentence).

But this is neither here nor there, since the over-arching object in Word is Paragraph. It is a "real" object. Sentence is not. In fact...there IS no Sentence object (singular). Sentences(x) is a range collection.

A "blank" paragraph - a misnomer - is a sentence. All paragraphs have at least one, even if it is just the range of the paragraph mark.

Even by the explanation at that link, it does not explain why the text preceding "/\@#$%^&*" is ignored.

Paul_Hossler
10-07-2010, 01:01 PM
Gerry

1. I really wish you'd write that book. I'd pre-order it now.

2. If the .\ sentence is not the first sentence in the first paragraph of the document, there doesn't seem to be a problem. Empty or non-empty paragraph before the .\ sentences and there's no problem (at least in Word 2010)

(edit) 3. Actually if the .\ sentence is the first one in a Range there's a problem.

-----doc start ----
abcdef^p
THIS IS SENTENCE 1./ THIS IS SENTENCE 2./ THIS IS SENTENCE 3./ ^p

Sub test()
Dim i As Long

For i = 1 To ActiveDocument.Paragraphs(2).Range.Sentences.Count
MsgBox ActiveDocument.Paragraphs(2).Range.Sentences(i).Text
Next i

Stop
End Sub


Sub test2()
Dim i As Long

For i = 1 To ActiveDocument.Range.Sentences.Count
MsgBox ActiveDocument.Range.Sentences(i).Text
Next i

Stop
End Sub



Test exhibits the behavior, but Test2 does not

Maybe some MS Word programmer forgot to add 1 to an index
Paul

fumei
10-07-2010, 02:00 PM
I am interested in why, if you have:

abcdef^p
THIS IS SENTENCE 1./ THIS IS SENTENCE 2./ THIS IS SENTENCE 3./ ^p

and you test everything (ActiveDocument.Sentences):
Sub test3()
Dim oSentence
For Each oSentence In ActiveDocument.Sentences
MsgBox oSentence
Next
End Sub
you get:

abcdef
/
/
/

NO text of the other sentences are picked up.

"2. If the .\ sentence is not the first sentence in the first paragraph of the document, there doesn't seem to be a problem. Empty or non-empty paragraph before the .\ sentences and there's no problem (at least in Word 2010)"

maybe they "fixed" things in 2010, but this is NOT correct for 2002. abcdef as the first paragraph, or not, the slash is the only thing returned.

Paul_Hossler
10-07-2010, 03:00 PM
more puzzlers for you

Same test

-------- start of doc ------
abcdef^p
THIS IS SENTENCE 1./ THIS IS SENTENCE 2./ THIS IS SENTENCE 3./ ^p



Sub test3()
Dim oSentence As Range
For Each oSentence In ActiveDocument.Sentences
MsgBox oSentence.Text
Next
End Sub

Sub test4()
Dim i As Long
For i = 1 To ActiveDocument.Sentences.Count
MsgBox ActiveDocument.Sentences(i).Text
Next
End Sub


Your test3 is the more elegant way to do it. My alternate test4 is basically the same thing


Test3 returns

abcdef
/
/
/




Test4 returns

abcdef
THIS IS SENTENCE 1./
THIS IS SENTENCE 2./
THIS IS SENTENCE 3./


I think (guess) the difference is that Word has a 'problem' with the first sentence in a range and your oSentence is always a range that only has one sentence and therefore it's the first one and Word has problems

The range in test4 is the document.

If the first sentence in the range (i.e. document) has the .\ sentence first, the remaining sentences (since they're not first) are OK

If the first sentence in the range (i.e. document) does not have the .\ sentence first, that sentence as well as the remaining sentences (since they're not first) are OK

Paul

fumei
10-08-2010, 09:31 AM
Very strange.

fumei
10-08-2010, 12:56 PM
"If the first sentence in the range (i.e. document) does not have the .\ sentence first, that sentence as well as the remaining sentences (since they're not first) are OK"

That appears to be the case.

From that link:


Middle paragraph sentences:
From the first character following the end of the preceding sentence to (an
including all spaces) the first character (but excluding that character)
after the following period. OK, but in the example we are using, the first character following the end of the preceding (first) sentence is T(his is....

But that is not the case. The entire second "sentence" becomes the slash, the text preceding is ignored.

BTW: the actual numeric values of .Start and .End are, in fact, matching to the slash.

Some real funky coding there Microsoft.