View Single Post
 
Old 07-08-2019, 09:51 AM
Valvolino78 Valvolino78 is offline Windows 10 Office 2010 64bit
Novice
 
Join Date: Jun 2018
Posts: 2
Valvolino78 is on a distinguished road
Default search in specific page

Hi everyone,
I have to search some text in each page of a document and delete it, but the first one.
The code I wrote (also borrowing here and there over the web) do the job, but even in the first page, with the result that the text is deleted there too.
Here part the code:

intpagecount = ActiveDocument.ComputeStatistics(wdStatisticPages)
For intpage = 2 To intpagecount + 1

MYFIND = "xxx"
Dim ORANGEw As Word.Range
Set ORANGEw = ActiveDocument.Range
With ORANGEw.Find
.Text = MYFIND
.MatchWildcards = True
.Forward = True
.Wrap = wdFindContinue
.Execute
If .Found Then
ORANGEw.Start = ORANGEw.Start
ORANGEw.Collapse
ORANGEw.Bookmarks.Add Name:="str1"
End If
ORANGEw.Start = ORANGEw.End
End With

MYFIND = "yyy"
Dim ORANGEww As Word.Range
Set ORANGEww = ActiveDocument.Range
With ORANGEww.Find
.Text = MYFIND
.MatchWildcards = True
.Forward = True
.Wrap = wdFindStop
.Execute
If .Found Then
ORANGEww.Start = ORANGEww.Start + 15
ORANGEww.Collapse
ORANGEww.Bookmarks.Add Name:="ndn1"
End If
ORANGEww.Start = ORANGEww.End
End With

p3 = ActiveDocument.Bookmarks("str1").Range.Start
p4 = ActiveDocument.Bookmarks("ndn1").Range.End
Set myrange1 = ActiveDocument.Range(Start:=p3, End:=p4)
myrange1.Delete

If intpage <> 1 Then
Selection.GoToNext what:=wdGoToPage

intpagecount = ActiveDocument.ComputeStatistics(wdStatisticPages)

Next

I tried changing the initial number of "for...next" cycle (beginning from 2 instead of 1), but no way: the routine starts always from the first page.
I very appreciate any help.
Thanks.
Valvolino78
Reply With Quote