How to get non-overlapped page numbers?

J

junya55

Hi.

Please tell me how to get non-overlapped page numbers indicating
the pages, in wihch the found text exists in a document.

For example, when "steel" is shown twice in page 3 and three times in
page 5,
the following code returns the page numbers like, "page: 3, 3, 5, 5,
5, ".

Could you give me any advice to remove the overlapped page numbers
such
that the following code returns "page: 3, 5, " instead?

The code is shown below.
thanks in advance.

junya

.................................................................................
Sub page_search()

Dim myRange As Range
Dim pageNum As String

Set myRange = Selection.Range

Selection.HomeKey Unit:=wdStory

With myRange.Find
.Wrap = wdFindStop
.MatchWholeWord = True
.Execute FindText:="steel", Forward:=True
End With

Do While myRange.Find.Found = True
pageNum = pageNum & _
myRange.Information(wdActiveEndPageNumber) _
& ", "
myRange.Find.Execute
Loop

MSGBOX "page: " & pageNum
End Sub
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use:

Dim myRange As Range
Dim pageNum As String
Dim Flag As Boolean
Flag = False
pageNum = ""
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="steel", Forward:=True,
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set myRange = Selection.Range
Selection.Collapse wdCollapseEnd
If Len(pageNum) > 0 Then
If Flag = False Then
Flag = True
If Val(pageNum) <>
myRange.Information(wdActiveEndPageNumber) Then
pageNum = pageNum & ", " &
myRange.Information(wdActiveEndPageNumber)
End If
ElseIf Val(Mid(pageNum, InStrRev(pageNum, ",") + 2)) <>
myRange.Information(wdActiveEndPageNumber) Then
pageNum = pageNum & ", " &
myRange.Information(wdActiveEndPageNumber)
End If
Else
pageNum = myRange.Information(wdActiveEndPageNumber)
End If
Loop
End With
MsgBox "page: " & pageNum


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

junya55

hi, Doug:

Thank you very much for your quick reply.
I really appreciate your help.

Val, InStrRev, and Mid are all new functions for me.
I now understand what you are doing after looking into
HELP files of VBE.

thanks a lot.

Junya
 
J

junya55

Hi, Doug:

Thank you for your advice.
I specially appreciate your IF lines for evaluating the pageNum
variable.

Because your code still returns overlapped page numbers sometimes,
I changed your code so that the code works O.K.

Your Original Code:
            If Flag = False Then
                Flag = True
                If Val(pageNum) <>
myRange.Information(wdActiveEndPageNumber) Then
                    pageNum = pageNum & ", " &
myRange.Information(wdActiveEndPageNumber)
                End If

in the above, I moved the line of "Flag = True" to the position
after the "If lines" as follows.
If Flag = False Then
If Val(pageNum) <>
myRange.Information(wdActiveEndPageNumber) Then
pageNum = pageNum & ", " &
myRange.Information(wdActiveEndPageNumber) Flag = True
End If

Flag should become "True" only after the second pageNum is added to
the first pageNum.

I think this works.
Thank you.

Junya
 
D

David Horowitz

Junya,
I wanted to try my hand at this one too. Instead of using a boolean flag and
checking the text string, I stored the most recently added page number in an
integer.
I also maintained your original code's ability to search only from the
current cursor position.
I put comments to mark changes to your code.

Sub page_search()

Dim myRange As Range
Dim pageNum As String
Dim thisPage As Integer, lastPage As Integer ' new variables

Set myRange = Selection.Range

Selection.HomeKey Unit:=wdStory

With myRange.Find
.Wrap = wdFindStop
.MatchWholeWord = True
.Execute FindText:="steel", Forward:=True
End With

Do While myRange.Find.Found = True
' changes here
thisPage = myRange.Information(wdActiveEndPageNumber)
If thisPage <> lastPage Then
pageNum = pageNum & _
thisPage _
& ", "
lastPage = thisPage
End If
myRange.Find.Execute
Loop

' remove the last comma and space
If Len(pageNum) <> 0 Then pageNum = Left$(pageNum, Len(pageNum) - 2)
MsgBox "page: " & pageNum
End Sub
 
J

junya55

Hi, David:

Thank you very much for sharing your idea.

your idea of using lastPage and thisPage strings
is very genius.

lastPage = thisPage

i like the above way of thinking.

I am learning the thinking habit or thinking process of
programers, and your advice show me good one.

thanks a lot.

Junya
 
D

David Horowitz

That great Junya. There's no one right way to do things. I always learn from
seeing the multiple ways people would go about things and going from there.
You've learned all new things between Doug's use of Val, InstrRev, and Mid,
booleans, text processing, and I added on Left and using a variable to store
the most recent page number.
Happy coding!
Dave
 

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