End of the sentence

S

Sahana

Hi People

I m new here, and a vba beginner. I hope to learn and get help from
people who are familar with Microsoft Word VBA.

I have few documents. Documents consists of sentences, and few
numbered list.

The sentences usually ends with the word " completed", while the lists
end with the word " done".

Is it possible for the VBA macros to detect these words, which occur
at the end of the list?

If so, please let me know. Please explain how macros do this so that I
can as well something.


Thank you.


Regards

Sahana
 
P

Pesach Shelnitz

Hi Sahana,

I'll try answer the part about whether it is possible to find the word
"done" at the end of a list. A list is represented by a List object, but be
aware that Word considers what appears to be a new list as a continuation of
the previous list unless you do something like restarting the numbering in
the new list.

The items in a list are each represented by a ListParagraph object. The
ListParagraph object has a .Range.Words.Last property, but this property
seems to hold the paragraph mark and the last word is one or two words behind
it, depending on whether the sentence ends with punctuation. The following
macro searches for the word "done" in all list items backwards from the end
of the item. When the word is found, the macro selects it and pops up a
message.

Sub FindInLists()
Dim i As Long
Dim j As Long
Dim pos As Integer
Dim searchText As String
Dim myRange As Range

searchText = "done"
With ActiveDocument
For j = 1 To .Lists.Count
For i = 1 To .Lists(j).ListParagraphs.Count
Set myRange = .Lists(j).ListParagraphs(i).Range
pos = InStrRev(myRange.Text, searchText, -1, _
vbTextCompare)
If pos > Len(myRange.Text) - 6 Then
myRange.start = myRange.start + pos - 1
myRange.End = myRange.start + Len(searchText)
myRange.Select
resp = MsgBox("The word """ & searchText & _
""" was found.", vbOKCancel)
If resp = vbCancel Then
Exit Sub
End If
Selection.Collapse Direction:=wdCollapseStart
End If
Next
Next
End With
End Sub
 
S

Sahana

Hi Sahana,

I'll try answer the part about whether it is possible to find the word
"done" at the end of a list. A list is represented by a List object, but be
aware that Word considers what appears to be a new list as a continuationof
the previous list unless you do something like restarting the numbering in
the new list.

The items in a list are each represented by a ListParagraph object. The
ListParagraph object has a .Range.Words.Last property, but this property
seems to hold the paragraph mark and the last word is one or two words behind
it, depending on whether the sentence ends with punctuation. The following
macro searches for the word "done" in all list items backwards from the end
of the item. When the word is found, the macro selects it and pops up a
message.

Sub FindInLists()
    Dim i As Long
    Dim j As Long
    Dim pos As Integer
    Dim searchText As String
    Dim myRange As Range

    searchText = "done"
    With ActiveDocument
        For j = 1 To .Lists.Count
            For i = 1 To .Lists(j).ListParagraphs.Count
                Set myRange = .Lists(j).ListParagraphs(i).Range
                pos = InStrRev(myRange.Text, searchText, -1, _
                    vbTextCompare)
                If pos > Len(myRange.Text) - 6 Then
                    myRange.start = myRange.start +pos - 1
                    myRange.End = myRange.start + Len(searchText)
                    myRange.Select
                    resp = MsgBox("The word """ & searchText & _
                        """ was found.", vbOKCancel)
                    If resp = vbCancel Then
                        Exit Sub
                    End If
                    Selection.Collapse Direction:=wdCollapseStart
                End If
            Next
        Next
    End With
End Sub

--
Hope this helps,
Pesach Shelnitz











- Show quoted text -

Shelnitz,

Can you please mention which line no of the code browses through the
list. Is it possible to browse through any particular list ( 3rd of
5 ) for example?


Regards

Sahana
 
P

Pesach Shelnitz

Hi Sahana,

The loop that starts with the following line browses through the lists in
the document.

For j = 1 To .Lists.Count

However, there is no guarantee that there is a one-to-one relationship
between the objects in the Lists collection and the lists in the document.
There is thus no certainty that what appears to be the third list in a
document is the third List object in the collection because the second List
object could include what appears to be the second list and the third list.

Some things that you can rely on are that the Lists collection contains all
the lists in the document, that all the text in the lists is included the
ListParagraph objects accessed through the properties of the List objects.

To better understand the issues with lists, take a look at
http://www.word.mvps.org/FAQs/Numbering/WordsNumberingExplained.htm by John
McGhie.
 

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