Reputation: 638
I have a Word document, with the first two sentences as normal text and sentences three and four in a table (in one cell):
My first sentence. My second sentence.
My third sentence. My fourth sentence.
My code is the following:
Option Explicit
Sub test()
Dim sentence As Variant
Dim i As Long: i = 0
Selection.Expand wdSentence
Debug.Print "--------->" & ActiveDocument.ActiveWindow.Selection.Sentences.Count
Debug.Print "selection: " & ActiveDocument.ActiveWindow.Selection
For Each sentence In ActiveDocument.ActiveWindow.Selection.Sentences
i = i + 1
Debug.Print i & " sentence: " & sentence
Next
End Sub
If I select the first two sentences, the debug output is correct:
--------->2
selection: My first sentence. My second sentence.
1 sentence: My first sentence.
2 sentence: My second sentence.
If I select the two sentences in the table, the debug output is strange (or wrong?):
--------->2
selection: My third sentence. My fourth sentence.
1 sentence: My third sentence.
Why is the output of the table content different from normal text? How can I get the same result for the table content as for the normal text?
Upvotes: 0
Views: 639
Reputation: 638
@Rich Michaels: Thanks for the hint with the paragraphs. I made a fast and minimalistic macro that also works in tables:
Sub Par2Sen()
Dim s0, s1, smax As Long
Dim para As Word.Paragraph
Dim r() As Byte
For Each para In ActiveDocument.Paragraphs
r = para.Range
Debug.Print "> Paragraph= " & para.Range;
smax = UBound(r) - 1
s0 = 0
s1 = 0
Do Until s1 > smax
If r(s1) = 46 Or s1 = smax Then
Debug.Print "-> Sen from" & (s0 / 2) + 1 & " to " & (s1 / 2) + 1 & MidB$(r, s0 + 1, s1 - s0 + 3)
s0 = s1 + 2
End If
s1 = s1 + 2
Loop
Next
End Sub
Upvotes: 0
Reputation: 1713
Tables bring a another whole dimension of complexity to what Word recognizes as a sentence. Paragraph, end of cell marks, and end of row markers all factor into the confusion of what constitutes a sentence to VBA.
Here is some code that should work, but I can’t claim with 100% certainty that it will work in all situations. In other words, I know it can be improved upon, but it should give you a good start for your own debugging sessions.
Sub ParseBySentence()
Dim doc As Word.Document
Dim i As Long, s As Long, para As Word.Paragraph
Dim rng As Word.Range, sRng As Word.Range
Application.ScreenUpdating = False
On Error Resume Next
Set doc = ActiveDocument
For i = 1 To doc.Paragraphs.Count
Set para = doc.Paragraphs(i)
If para.Range.Information(wdWithInTable) Then
Set rng = para.Range
Do While Asc(rng.Characters.Last) = 13
rng.MoveEnd unit:=wdCharacter, Count:=-1
Loop
If rng.Text = vbNullString Or _
Asc(rng.Text) = 13 Then
'do nothing
Else
For s = 1 To rng.Sentences.Count
Set sRng = rng.Sentences(s)
Do While Asc(sRng.Characters.Last) = 13
sRng.MoveEnd unit:=wdCharacter, Count:=-1
Loop
sRng.Select
Debug.Print Selection.Text
Selection.Collapse Word.WdCollapseDirection.wdCollapseEnd
Next
End If
End If
Next
Selection.HomeKey unit:=wdStory
Application.ScreenUpdating = True
MsgBox "Action Complete"
End Sub
Upvotes: 1