Group:  Microsoft Word ยป microsoft.public.word.vba.beginners
Thread: Loop through document and compare sentences

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Loop through document and compare sentences
"John Kaurloto" <jkaurloto[ at ]schnader.com> 02.08.2006 14:30:23
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









Re: Loop through document and compare sentences
Helmut Weber <nbhymsjxdgcn[ at ]mailinator.com> 02.08.2006 15:11:19
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"






Re: Loop through document and compare sentences
"Greg Maxey" <gmaxey[ at ]mvps.org> 02.08.2006 16:50:12
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:
[Quoted Text]
> 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"

Re: Loop through document and compare sentences
Helmut Weber <nbhymsjxdgcn[ at ]mailinator.com> 02.08.2006 17:08:36
<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"
Re: Loop through document and compare sentences
"John Kaurloto" <jkaurloto[ at ]schnader.com> 02.08.2006 18:52:54
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" <gmaxey[ at ]mvps.org> wrote in message
news:1154537412.206712.22200[ at ]s13g2000cwa.googlegroups.com...
[Quoted Text]
> 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"
>


Re: Loop through document and compare sentences
"Jonathan West" <jwest[ at ]mvps.org> 02.08.2006 19:09:03

"John Kaurloto" <jkaurloto[ at ]schnader.com> wrote in message
news:OQ0EeTmtGHA.2392[ at ]TK2MSFTNGP05.phx.gbl...
[Quoted Text]
> 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

Re: Loop through document and compare sentences
"Greg Maxey" <gmaxey[ at ]mvps.org> 02.08.2006 19:17:25
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 Kaurloto wrote:
[Quoted Text]
> 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" <gmaxey[ at ]mvps.org> wrote in message
> news:1154537412.206712.22200[ at ]s13g2000cwa.googlegroups.com...
> > 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"
> >

Re: Loop through document and compare sentences
"Greg Maxey" <gmaxey[ at ]mvps.org> 02.08.2006 19:24:33
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 Kaurloto wrote:
[Quoted Text]
> 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" <gmaxey[ at ]mvps.org> wrote in message
> news:1154537412.206712.22200[ at ]s13g2000cwa.googlegroups.com...
> > 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"
> >

Re: Loop through document and compare sentences
"John Kaurloto" <jkaurloto[ at ]schnader.com> 02.08.2006 19:27:57
Thank you, Sir.

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

John



"Jonathan West" <jwest[ at ]mvps.org> wrote in message
news:eTkyJdmtGHA.2260[ at ]TK2MSFTNGP03.phx.gbl...
[Quoted Text]
>
> "John Kaurloto" <jkaurloto[ at ]schnader.com> wrote in message
> news:OQ0EeTmtGHA.2392[ at ]TK2MSFTNGP05.phx.gbl...
> > 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
>


Re: Loop through document and compare sentences
"John Kaurloto" <jkaurloto[ at ]schnader.com> 02.08.2006 19:42:57
WOW!

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

You also wrote previously:
[Quoted Text]
> 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" <gmaxey[ at ]mvps.org> wrote in message
news:1154546673.262083.281930[ at ]p79g2000cwp.googlegroups.com...
> 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 Kaurloto wrote:
> > 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" <gmaxey[ at ]mvps.org> wrote in message
> > news:1154537412.206712.22200[ at ]s13g2000cwa.googlegroups.com...
> > > 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"
> > >
>


Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net