Loop through document and compare sentences

J

John Kaurloto

To the group:

I want to loop through a document comparing sequential sentences. The code
is below.
To test this I have copied and pasted sentences so sentences one and two are
exact and four and five are exact, yet I continually get a 'They Match'
message box through the entire document (thirteen sentences in the test
document).
What am I doing wrong?

Thank you in advance for any assistance or suggestions.

John Kaurloto

Sub SbyS()
Dim x As Integer
Dim s1 As Object
Dim s2 As Object
' Move the insertion point to the beginning of the document.
Selection.HomeKey Unit:=wdStory, Extend:=wdMove
' Loop number of sentences in document.
x = ActiveDocument.Sentences.Count
For i = 1 To x
' Select a sentence.
ActiveDocument.Sentences(i).Select
Set s1 = Selection
' Move to next sentence.
ActiveDocument.Sentences(i + 1).Select
Set s2 = Selection
'compare sentences
If s1.IsEqual(s2.Range) = True Then
MsgBox "They Match"
Else
MsgBox "No Match"
End If
Next i
End Sub
 
H

Helmut Weber

Hi John,

to explain what's going is more difficult than
to provide a soltion.

Step through your code in single step mode [F8].
Stop after the line:
Set s2 = selection
Move the mouse pointer to s2. Remember what is displayed.
Move the mouse pointer to s1. Remember what is displayed.
It's the same.

Setting the object s1 to the selection,
causes the contents of s1 to change
with every change of the selection, IMHO.

Don't mingle with objects for what you want to do.

Sample 1:

Sub Test402()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000")
If .Item(i).Text = .Item(i + 1).Text Then
Debug.Print s & ": true"
Else
Debug.Print s & ": false"
End If
Next i
End With
End Sub

I like this better, though a bit more advanced:

Sub Test403()
Dim i As Long
Dim s As String
With ActiveDocument.Range.Sentences
For i = 1 To .Count - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000") & ": "
s = s & CStr(.Item(i).Text = .Item(i + 1).Text)
Debug.Print s
Next i
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Helmut,

I am afraid it isn't that simple. Consider the following document
containing the two sentences:

A man and his dog. A man and his dog.

Your code will return false unless you have a third sentence. A word
sentence includes the space following the punctuation mark. I haven't
quite figured out the what effect the paragraph mark plays, but it
seems to play tricks as well.

Try:

Sub ScrachMacro()
Dim oSenCount As Long
Dim i As Long
Dim s1 As Word.Range
Dim s2 As Word.Range
Dim pStr1 As String
Dim pStr2 As String
Dim oMatch As Boolean
oSenCount = ActiveDocument.Sentences.Count
For i = 1 To oSenCount - 1
pStr1 = ActiveDocument.Sentences(i).Text
pStr2 = ActiveDocument.Sentences(i + 1).Text
If Right(pStr1, 1) = Chr(32) And Right(pStr2, 1) <> Chr(32) Then
pStr2 = Left(pStr2, Len(pStr2) - 1) & Chr(32)
End If
If pStr1 = pStr2 Then
MsgBox "They Match"
Else
MsgBox "No Match"
End If
Next i
End Sub
 
H

Helmut Weber

<grr>

Right you are.

Have a nice day.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Kaurloto

Gentlemen:

I am indebted to you both and please accept my sincerest thanks.
If I may: The eventual outcome was to find if document 2 contained a
sentence in document 1, which I managed to do (I used Herr Weber's code as I
had not seen Mr. Maxey's post at the time - my code follows below), but I
have an odd dilemma.
I resize both documents (crudely) to see both sentences. I select a
sentence in document 1 and it finds it quite well in document 2.
The problem is after resizing, document 1 displays at the beginning of the
document and not the sentence I selected.
Is there a way to return to the selected sentence in document 1 after the
resizing?

Again, my sincerest thanks to you both. I have learned much from
experimenting with your replies.

Thank you.

John
p.s. - I understand Mr. Maxey's point as I experienced some anomalous
returns when running the my initial code

=========================
Sub FindSentence()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
s = Selection
'resize window vertically at top
Application.WindowState = wdWindowStateNormal
Application.Move Left:=0, Top:=0
Application.Resize Width:=768, Height:=275
'Activate other document
Windows("TEST2.doc").Activate
'resize window vertically at bottom
Application.WindowState = wdWindowStateNormal
Application.Resize Width:=768, Height:=275
Application.Move Left:=0, Top:=251
'find sentence from Test1.doc in Test2.doc
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1
If .Item(i).Text = s Then
.Item(i).Select
Exit Sub
End If
Next i
End With
End Sub






Greg Maxey said:
Helmut,

I am afraid it isn't that simple. Consider the following document
containing the two sentences:

A man and his dog. A man and his dog.

Your code will return false unless you have a third sentence. A word
sentence includes the space following the punctuation mark. I haven't
quite figured out the what effect the paragraph mark plays, but it
seems to play tricks as well.

Try:

Sub ScrachMacro()
Dim oSenCount As Long
Dim i As Long
Dim s1 As Word.Range
Dim s2 As Word.Range
Dim pStr1 As String
Dim pStr2 As String
Dim oMatch As Boolean
oSenCount = ActiveDocument.Sentences.Count
For i = 1 To oSenCount - 1
pStr1 = ActiveDocument.Sentences(i).Text
pStr2 = ActiveDocument.Sentences(i + 1).Text
If Right(pStr1, 1) = Chr(32) And Right(pStr2, 1) <> Chr(32) Then
pStr2 = Left(pStr2, Len(pStr2) - 1) & Chr(32)
End If
If pStr1 = pStr2 Then
MsgBox "They Match"
Else
MsgBox "No Match"
End If
Next i
End Sub
Helmut said:
Hi John,

to explain what's going is more difficult than
to provide a soltion.

Step through your code in single step mode [F8].
Stop after the line:
Set s2 = selection
Move the mouse pointer to s2. Remember what is displayed.
Move the mouse pointer to s1. Remember what is displayed.
It's the same.

Setting the object s1 to the selection,
causes the contents of s1 to change
with every change of the selection, IMHO.

Don't mingle with objects for what you want to do.

Sample 1:

Sub Test402()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000")
If .Item(i).Text = .Item(i + 1).Text Then
Debug.Print s & ": true"
Else
Debug.Print s & ": false"
End If
Next i
End With
End Sub

I like this better, though a bit more advanced:

Sub Test403()
Dim i As Long
Dim s As String
With ActiveDocument.Range.Sentences
For i = 1 To .Count - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000") & ": "
s = s & CStr(.Item(i).Text = .Item(i + 1).Text)
Debug.Print s
Next i
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jonathan West

John Kaurloto said:
Gentlemen:

I am indebted to you both and please accept my sincerest thanks.
If I may: The eventual outcome was to find if document 2 contained a
sentence in document 1, which I managed to do (I used Herr Weber's code as
I
had not seen Mr. Maxey's post at the time - my code follows below), but I
have an odd dilemma.
I resize both documents (crudely) to see both sentences. I select a
sentence in document 1 and it finds it quite well in document 2.
The problem is after resizing, document 1 displays at the beginning of the
document and not the sentence I selected.
Is there a way to return to the selected sentence in document 1 after the
resizing?

Look up the ScrollIntoView method in the Word VBA Help file.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg Maxey

John,

As Jonathan suggested use the ScrollIntoView method. Also in your
application I think you will want to change for i = 1 to x -1 back to
for i = 1 to x.

Using your code as is you will have problems is you select a sentence
including the trailing space in test1.doc and it is matched by a
sentence at the end of a paragraph in test2.doc.


Sub FindSentence()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
s = Selection
'resize window vertically at top
Application.WindowState = wdWindowStateNormal
Application.Move Left:=0, Top:=0
Application.Resize Width:=768, Height:=275
ActiveWindow.ScrollIntoView Selection.Range, True
'Activate other document
Windows("TEST2.doc").Activate
'resize window vertically at bottom
Application.WindowState = wdWindowStateNormal
Application.Resize Width:=768, Height:=275
Application.Move Left:=0, Top:=251
'find sentence from Test1.doc in Test2.doc
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x
If .Item(i).Text = s Then
.Item(i).Select
Exit Sub
End If
Next i
End With
End Sub




John said:
Gentlemen:

I am indebted to you both and please accept my sincerest thanks.
If I may: The eventual outcome was to find if document 2 contained a
sentence in document 1, which I managed to do (I used Herr Weber's code as I
had not seen Mr. Maxey's post at the time - my code follows below), but I
have an odd dilemma.
I resize both documents (crudely) to see both sentences. I select a
sentence in document 1 and it finds it quite well in document 2.
The problem is after resizing, document 1 displays at the beginning of the
document and not the sentence I selected.
Is there a way to return to the selected sentence in document 1 after the
resizing?

Again, my sincerest thanks to you both. I have learned much from
experimenting with your replies.

Thank you.

John
p.s. - I understand Mr. Maxey's point as I experienced some anomalous
returns when running the my initial code

=========================
Sub FindSentence()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
s = Selection
'resize window vertically at top
Application.WindowState = wdWindowStateNormal
Application.Move Left:=0, Top:=0
Application.Resize Width:=768, Height:=275
'Activate other document
Windows("TEST2.doc").Activate
'resize window vertically at bottom
Application.WindowState = wdWindowStateNormal
Application.Resize Width:=768, Height:=275
Application.Move Left:=0, Top:=251
'find sentence from Test1.doc in Test2.doc
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1
If .Item(i).Text = s Then
.Item(i).Select
Exit Sub
End If
Next i
End With
End Sub






Greg Maxey said:
Helmut,

I am afraid it isn't that simple. Consider the following document
containing the two sentences:

A man and his dog. A man and his dog.

Your code will return false unless you have a third sentence. A word
sentence includes the space following the punctuation mark. I haven't
quite figured out the what effect the paragraph mark plays, but it
seems to play tricks as well.

Try:

Sub ScrachMacro()
Dim oSenCount As Long
Dim i As Long
Dim s1 As Word.Range
Dim s2 As Word.Range
Dim pStr1 As String
Dim pStr2 As String
Dim oMatch As Boolean
oSenCount = ActiveDocument.Sentences.Count
For i = 1 To oSenCount - 1
pStr1 = ActiveDocument.Sentences(i).Text
pStr2 = ActiveDocument.Sentences(i + 1).Text
If Right(pStr1, 1) = Chr(32) And Right(pStr2, 1) <> Chr(32) Then
pStr2 = Left(pStr2, Len(pStr2) - 1) & Chr(32)
End If
If pStr1 = pStr2 Then
MsgBox "They Match"
Else
MsgBox "No Match"
End If
Next i
End Sub
Helmut said:
Hi John,

to explain what's going is more difficult than
to provide a soltion.

Step through your code in single step mode [F8].
Stop after the line:
Set s2 = selection
Move the mouse pointer to s2. Remember what is displayed.
Move the mouse pointer to s1. Remember what is displayed.
It's the same.

Setting the object s1 to the selection,
causes the contents of s1 to change
with every change of the selection, IMHO.

Don't mingle with objects for what you want to do.

Sample 1:

Sub Test402()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000")
If .Item(i).Text = .Item(i + 1).Text Then
Debug.Print s & ": true"
Else
Debug.Print s & ": false"
End If
Next i
End With
End Sub

I like this better, though a bit more advanced:

Sub Test403()
Dim i As Long
Dim s As String
With ActiveDocument.Range.Sentences
For i = 1 To .Count - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000") & ": "
s = s & CStr(.Item(i).Text = .Item(i + 1).Text)
Debug.Print s
Next i
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

John,

You might consider the following to dress up "crudely" :)


Sub FindSentence()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
s = Selection
Windows.Arrange
ActiveWindow.ScrollIntoView Selection.Range, True
Windows("TEST2.doc").Activate
'find sentence from Test1.doc in Test2.doc
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x
If .Item(i).Text = s Then
.Item(i).Select
Exit Sub
End If
Next i
End With
End Sub


John said:
Gentlemen:

I am indebted to you both and please accept my sincerest thanks.
If I may: The eventual outcome was to find if document 2 contained a
sentence in document 1, which I managed to do (I used Herr Weber's code as I
had not seen Mr. Maxey's post at the time - my code follows below), but I
have an odd dilemma.
I resize both documents (crudely) to see both sentences. I select a
sentence in document 1 and it finds it quite well in document 2.
The problem is after resizing, document 1 displays at the beginning of the
document and not the sentence I selected.
Is there a way to return to the selected sentence in document 1 after the
resizing?

Again, my sincerest thanks to you both. I have learned much from
experimenting with your replies.

Thank you.

John
p.s. - I understand Mr. Maxey's point as I experienced some anomalous
returns when running the my initial code

=========================
Sub FindSentence()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
s = Selection
'resize window vertically at top
Application.WindowState = wdWindowStateNormal
Application.Move Left:=0, Top:=0
Application.Resize Width:=768, Height:=275
'Activate other document
Windows("TEST2.doc").Activate
'resize window vertically at bottom
Application.WindowState = wdWindowStateNormal
Application.Resize Width:=768, Height:=275
Application.Move Left:=0, Top:=251
'find sentence from Test1.doc in Test2.doc
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1
If .Item(i).Text = s Then
.Item(i).Select
Exit Sub
End If
Next i
End With
End Sub






Greg Maxey said:
Helmut,

I am afraid it isn't that simple. Consider the following document
containing the two sentences:

A man and his dog. A man and his dog.

Your code will return false unless you have a third sentence. A word
sentence includes the space following the punctuation mark. I haven't
quite figured out the what effect the paragraph mark plays, but it
seems to play tricks as well.

Try:

Sub ScrachMacro()
Dim oSenCount As Long
Dim i As Long
Dim s1 As Word.Range
Dim s2 As Word.Range
Dim pStr1 As String
Dim pStr2 As String
Dim oMatch As Boolean
oSenCount = ActiveDocument.Sentences.Count
For i = 1 To oSenCount - 1
pStr1 = ActiveDocument.Sentences(i).Text
pStr2 = ActiveDocument.Sentences(i + 1).Text
If Right(pStr1, 1) = Chr(32) And Right(pStr2, 1) <> Chr(32) Then
pStr2 = Left(pStr2, Len(pStr2) - 1) & Chr(32)
End If
If pStr1 = pStr2 Then
MsgBox "They Match"
Else
MsgBox "No Match"
End If
Next i
End Sub
Helmut said:
Hi John,

to explain what's going is more difficult than
to provide a soltion.

Step through your code in single step mode [F8].
Stop after the line:
Set s2 = selection
Move the mouse pointer to s2. Remember what is displayed.
Move the mouse pointer to s1. Remember what is displayed.
It's the same.

Setting the object s1 to the selection,
causes the contents of s1 to change
with every change of the selection, IMHO.

Don't mingle with objects for what you want to do.

Sample 1:

Sub Test402()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000")
If .Item(i).Text = .Item(i + 1).Text Then
Debug.Print s & ": true"
Else
Debug.Print s & ": false"
End If
Next i
End With
End Sub

I like this better, though a bit more advanced:

Sub Test403()
Dim i As Long
Dim s As String
With ActiveDocument.Range.Sentences
For i = 1 To .Count - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000") & ": "
s = s & CStr(.Item(i).Text = .Item(i + 1).Text)
Debug.Print s
Next i
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Kaurloto

Thank you, Sir.

How mystifying when one is ignorant, how easy once one is shown.
My appreciation to you all.

John
 
J

John Kaurloto

WOW!

Thanks...
Cleaner and faster...
Really, thank you.

You also wrote previously:
Using your code as is you will have problems is you select a sentence
including the trailing space in test1.doc and it is matched by a
sentence at the end of a paragraph in test2.doc.

I will correct for this next....
And with what you all gave me, I'm confident I can manage this...
Quite a different attitude from this morning.
:)

Greg Maxey said:
John,

You might consider the following to dress up "crudely" :)


Sub FindSentence()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
s = Selection
Windows.Arrange
ActiveWindow.ScrollIntoView Selection.Range, True
Windows("TEST2.doc").Activate
'find sentence from Test1.doc in Test2.doc
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x
If .Item(i).Text = s Then
.Item(i).Select
Exit Sub
End If
Next i
End With
End Sub


John said:
Gentlemen:

I am indebted to you both and please accept my sincerest thanks.
If I may: The eventual outcome was to find if document 2 contained a
sentence in document 1, which I managed to do (I used Herr Weber's code as I
had not seen Mr. Maxey's post at the time - my code follows below), but I
have an odd dilemma.
I resize both documents (crudely) to see both sentences. I select a
sentence in document 1 and it finds it quite well in document 2.
The problem is after resizing, document 1 displays at the beginning of the
document and not the sentence I selected.
Is there a way to return to the selected sentence in document 1 after the
resizing?

Again, my sincerest thanks to you both. I have learned much from
experimenting with your replies.

Thank you.

John
p.s. - I understand Mr. Maxey's point as I experienced some anomalous
returns when running the my initial code

=========================
Sub FindSentence()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
s = Selection
'resize window vertically at top
Application.WindowState = wdWindowStateNormal
Application.Move Left:=0, Top:=0
Application.Resize Width:=768, Height:=275
'Activate other document
Windows("TEST2.doc").Activate
'resize window vertically at bottom
Application.WindowState = wdWindowStateNormal
Application.Resize Width:=768, Height:=275
Application.Move Left:=0, Top:=251
'find sentence from Test1.doc in Test2.doc
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1
If .Item(i).Text = s Then
.Item(i).Select
Exit Sub
End If
Next i
End With
End Sub






Greg Maxey said:
Helmut,

I am afraid it isn't that simple. Consider the following document
containing the two sentences:

A man and his dog. A man and his dog.

Your code will return false unless you have a third sentence. A word
sentence includes the space following the punctuation mark. I haven't
quite figured out the what effect the paragraph mark plays, but it
seems to play tricks as well.

Try:

Sub ScrachMacro()
Dim oSenCount As Long
Dim i As Long
Dim s1 As Word.Range
Dim s2 As Word.Range
Dim pStr1 As String
Dim pStr2 As String
Dim oMatch As Boolean
oSenCount = ActiveDocument.Sentences.Count
For i = 1 To oSenCount - 1
pStr1 = ActiveDocument.Sentences(i).Text
pStr2 = ActiveDocument.Sentences(i + 1).Text
If Right(pStr1, 1) = Chr(32) And Right(pStr2, 1) <> Chr(32) Then
pStr2 = Left(pStr2, Len(pStr2) - 1) & Chr(32)
End If
If pStr1 = pStr2 Then
MsgBox "They Match"
Else
MsgBox "No Match"
End If
Next i
End Sub
Helmut Weber wrote:
Hi John,

to explain what's going is more difficult than
to provide a soltion.

Step through your code in single step mode [F8].
Stop after the line:
Set s2 = selection
Move the mouse pointer to s2. Remember what is displayed.
Move the mouse pointer to s1. Remember what is displayed.
It's the same.

Setting the object s1 to the selection,
causes the contents of s1 to change
with every change of the selection, IMHO.

Don't mingle with objects for what you want to do.

Sample 1:

Sub Test402()
Dim x As Long ' number of sentences
Dim i As Long '
Dim s As String
With ActiveDocument.Range.Sentences
x = .Count
For i = 1 To x - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000")
If .Item(i).Text = .Item(i + 1).Text Then
Debug.Print s & ": true"
Else
Debug.Print s & ": false"
End If
Next i
End With
End Sub

I like this better, though a bit more advanced:

Sub Test403()
Dim i As Long
Dim s As String
With ActiveDocument.Range.Sentences
For i = 1 To .Count - 1 ' !!!
s = Format(i, "000") & " = " & Format(i + 1, "000") & ": "
s = s & CStr(.Item(i).Text = .Item(i + 1).Text)
Debug.Print s
Next i
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top