Find/Replace on last page of doc only

M

Minerva

I have a large Word XP doc that is being built by a VB.Net
project. Content is inserted into the document and then a
Find/Replace is done to replace certain terms.

Because of the length of the document, the Find/Replace
routine is taking a significant amount of time. Can I set
the Find/Replace routine to only look at the last two
pages of the document (I know the inserted text will
always be one or two pages but no longer). Right now I am
starting at the beginning of the doc and searching through
the entire thing. I dont know if I can start the find at
the end of the document and/or search only a specified
number of pages but the way I have it set up now, the
process is taking up to a half hour for documents that end
up being 200 pages or so.

With oDoc.Range.Find
.ClearFormatting()
.Forward = True
Try
Dim objTagName As Object = tagName
Dim objReplaceString As Object = replaceString
.Execute(FindText:=objTagName, _
ReplaceWith:=objReplaceString, _
Replace:=2) '2 = wdReplaceAll)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End With
 
H

Helmut Weber

Hi Minerva,
Can I set the Find/Replace routine to only look at the
last two pages of the document
Certainly.
You got to define a range, that contains nothing but
the last two pages. Like that:
Sub Last2Pages()
Dim iP As Integer
Dim sP As String
Dim rR As Range
Set rR = Selection.Range
iP = ActiveDocument.ComputeStatistics(wdStatisticPages)
sP = Trim(Str(iP - 1))
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext,
Name:=sP
rR.Start = Selection.Start
rR.End = ActiveDocument.Content.End
rR.Select
With rR.Find
.Text = "i"
.Replacement.Text = "#"
.Wrap = wdFindStop
rR.Find.Execute Replace:=wdReplaceAll
End With
End Sub
....
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, W98
 

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