Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-08-2019, 09:51 AM
Valvolino78 Valvolino78 is offline search in specific page Windows 10 search in specific page Office 2010 64bit
Novice
search in specific page
 
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
  #2  
Old 07-08-2019, 11:22 AM
kilroy kilroy is offline search in specific page Windows 10 search in specific page Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 118
kilroy is on a distinguished road
Default

Not very elegant but try this:


Sub MacroFindAndDeleteAllButFirst1()
Dim Word
Dim Page
CurrentPage = Selection.Information(wdActiveEndPageNumber)

Selection.HomeKey Unit:=wdStory
Page = InputBox("What Page is it on?", "Page Number")
Word = InputBox("Enter Word to Delete", "Keep The first")

Selection.GoTo What:=wdGoToPage, Count:=Page

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorRed

With Selection.Find
.Text = Word
.Replacement.Text = Word
.Forward = True
'.Wrap = Forward
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne

Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorAutomatic
Selection.Find.Replacement.ClearFormatting

With CurrentPage
With Selection.Find
.Text = Word
.Replacement.Text = ""
.Forward = True
.Wrap = Forward
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorRed
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorAutomatic

With Selection.Find
.Text = Word
.Replacement.Text = Word
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
MsgBox Word + " deleted on the selected page except for the first occurrence"
End Sub

Last edited by kilroy; 07-08-2019 at 01:06 PM. Reason: Mis read the requirements
Reply With Quote
  #3  
Old 07-08-2019, 08:43 PM
gmayor's Avatar
gmayor gmayor is offline search in specific page Windows 10 search in specific page Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If the aim of the game is to delete xxx and yyy from each page but the first then

Code:
Sub DelTexts()
Dim vFind As Variant
Dim oRng As Word.Range
Dim i As Integer
    vFind = Array("xxx", "yyy")
    For i = 0 To UBound(vFind)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:=vFind(i), MatchWildcards:=True)
                If oRng.Information(wdActiveEndPageNumber) > 1 Then
                    oRng.Text = ""
                End If
                oRng.Collapse 0
            Loop
        End With
    Next i
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
page count, search and replace



Similar Threads
Thread Thread Starter Forum Replies Last Post
search in specific page Search for lines that DO NOT have specific character knpaddac Word 1 01-14-2019 01:16 PM
search in specific page Macro to search for specific words in a document mike0215 Word VBA 2 11-28-2017 07:25 AM
Search for specific data in a text string teligence Excel 1 05-20-2017 11:51 AM
search in specific page Search for multiple texts in cell, return specific text mariur89 Excel 4 12-14-2014 01:33 AM
search in specific page Search first section for specific string... donaldadams1951 Word VBA 4 03-14-2014 11:24 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft