Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Loop through document and compare sentences

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Kaurloto - 02 Aug 2006 15:30 GMT
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
Helmut Weber - 02 Aug 2006 16:11 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey - 02 Aug 2006 17:50 GMT
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
> Hi John,
>
[quoted text clipped - 54 lines]
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
Helmut Weber - 02 Aug 2006 18:08 GMT
<grr>

Right you are.

Have a nice day.

Signature

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"

John Kaurloto - 02 Aug 2006 19:52 GMT
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

> Helmut,
>
[quoted text clipped - 90 lines]
> > Win XP, Office 2003
> > "red.sys" & Chr$(64) & "t-online.de"
Jonathan West - 02 Aug 2006 20:09 GMT
> Gentlemen:
>
[quoted text clipped - 10 lines]
> 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.

Signature

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

John Kaurloto - 02 Aug 2006 20:27 GMT
Thank you, Sir.

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

John

> > Gentlemen:
> >
[quoted text clipped - 12 lines]
>
> Look up the ScrollIntoView method in the Word VBA Help file.
Greg Maxey - 02 Aug 2006 20:17 GMT
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

> Gentlemen:
>
[quoted text clipped - 141 lines]
> > > Win XP, Office 2003
> > > "red.sys" & Chr$(64) & "t-online.de"
Greg Maxey - 02 Aug 2006 20:24 GMT
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

> Gentlemen:
>
[quoted text clipped - 141 lines]
> > > Win XP, Office 2003
> > > "red.sys" & Chr$(64) & "t-online.de"
John Kaurloto - 02 Aug 2006 20:42 GMT
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.
:-)

> John,
>
[quoted text clipped - 165 lines]
> > > > Win XP, Office 2003
> > > > "red.sys" & Chr$(64) & "t-online.de"
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.