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
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
> 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
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.
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 -- David Horowitz Lead Technologist Soundside Inc. www.soundside.biz
> 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
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 -- David Horowitz Lead Technologist Soundside Inc. www.soundside.biz