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
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
"junya55" <caf...@nifty.com> wrote in message
news:0506b173-a190-4a1f...@s38g2000prg.googlegroups.com...
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
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
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
--
David Horowitz
Lead Technologist
Soundside Inc.
www.soundside.biz
"junya55" <caf...@nifty.com> wrote in message
news:0506b173-a190-4a1f...@s38g2000prg.googlegroups.com...
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