Do until end of document

M

Mark

I am using WORD 97,

I want to run a loop that copies the whole page of a document that has the
text say "from vehicle" on it from one document to another, and continues
doing the same until the end of the document is reached.

Can anyone assist with some code, please?
 
H

Helmut Weber

Hi Mark,
like this:
Sub Test601()
Dim iPgs As Integer ' number of pages in doc-001
Dim Doc1 As Document ' document doc-001
Dim Doc2 As Document ' document doc-002
Dim Rng1 As Range ' selection.range in doc 1
Dim Rng2 As Range ' all of doc 2
Set Doc1 = Documents("doc-001.doc")
Set Doc2 = Documents("doc-002.doc")
Doc1.Activate
Selection.ExtendMode = False
Selection.HomeKey unit:=wdStory
With Doc1.BuiltInDocumentProperties
For iPgs = 1 To .Item("Number of pages")
Selection.GoTo _
What:=wdGoToPage, _
Which:=wdGoToNext, _
Name:=CStr(iPgs)
Doc1.Bookmarks("\page").Select ' select the page
If InStr(Selection.Range.Text, "copyme") Then
Set Rng1 = Selection.Range.FormattedText
Set Rng2 = Doc2.Range.FormattedText
Rng2.InsertAfter Rng1
End If
Next
End With
End Sub
---
But you may run into problems with formatting,
if a paragraphs starts on page x and ends e.g.
on page x + 1. And they may be more unforeseen
complications. But "end of document" would be
something that doesn't matter at all.
 

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