find / replace for only certain pages - Please see code

C

Casey Mac

Greetings,

Here is my code. What i would like for it to do is only work on say the
first 10 pages of a document and not go any further. I have users who will
be adding (insert - file) later on in this template and i dont want the find
replace to go and change any of the potential areas other than the first 10
pages..

Hope this makes sence. Thanks in advance


Sub FindAndReplaceWithHypertext()
Dim rDoc As Range ' range replace
Dim sFnd As String ' string to be found
Dim sHpl As String ' hyperlink
Dim sDsp As String ' hyperlink display

sFnd = InputBox("Enter the word you wish to replace with a hyperlink:")
sHpl = InputBox("Enter File Name:")
sDsp = InputBox("Enter the HyperLink display name")

Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
.Text = sFnd
.Wrap = wdFindStop
Do While .Execute
ActiveDocument.Hyperlinks.Add _
rDoc, sHpl, TextToDisplay:=sDsp
rDoc.End = rDoc.End + Len(sDsp)
rDoc.Collapse wdCollapseEnd
Loop
End With
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
J

Jezebel

The only line you need to work on is this one:

Set rDoc = ActiveDocument.Range

Instead of the entire document, you want a range reference to the first 10
pages. Simplest is probably to insert a section break after page 10, then
use

Set rDoc = ActiveDocument.Sections(1).Range
 
C

Casey Mac

Thanks for your quick response. i have since looked at the end result and
concluded that it may be longer than the first 10 pages as i first though.
i tried the secton break but it didnt have the affect that i thought it
would. Is it possible to have that find replace work only until it finds a
specific book mark? Im not sure what the best course of action is as i now
realize that this doc might be more dynamic than i first thought.

Your thoughts are most appreciated.

Cheers

Casey
 
C

Casey Mac

Greetings.

I would like to alter this code so that it prompts the user if they want to
replace the nwely found text or not. I am finding that the information that
im trying to replace is sometimes supposed to be there. I want to offer the
user an option. ha ha ha

Thanks in advance

cm
 

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