"Find this, insert page break": what approach?

E

Ed

A report generated by an old database program dumps everything out in one
long continuous file. That can not be changed. The resulting text file is
opened in Word, and the users run a macro I wrote that finds the first line
of each new record in the report and inserts a page break, so each new
record begins on a new page. All runs well - until someone changes that
first line! It's hard-coded into the macro as the string for "Find.Text =
strFind". Then, of course, it's my macro that's broke!

The header for each new record in the report has four lines. Each line is a
separate paragraph. Each line will report the same item of information,
such as:
ITEM NO: SER NO:
COLOR: BIN NO:
The information at each label may or may not change, but these labels will
remain fixed at their character positions in each line at each report
header. (For instance, whether COLOR: is "blue" or "chartreuse", the BIN
NO: label will always be at the same character position.)

Is there an easy way to tell the macro to "find three or four paragraphs
with these labels at these character positions", while disregarding whatever
information may be presented at the label?

Ed
 
K

Kevin B

Maybe this would work or point in the general direction:

Sub FindStuff()

Dim strLine As String
Dim rng As Range
Dim blnDone As Boolean

'Move to start of document
Selection.HomeKey unit:=wdStory

'Loop through the document, looking on each line for
'either "Test Text" or "Indented Text"
Do Until blnDone = True
'Select each line and verify that the end of the document
'has not been reached. If not, search for indicated text
With Selection
.HomeKey unit:=wdLine
.EndKey unit:=wdLine, Extend:=wdExtend
If .End = ActiveDocument.Content.End Then blnDone = True
strLine = .Text
End With
'If the text is found, insert page break
If InStr(1, strLine, "Test text") > 1 Or InStr(1, strLine, "Indented
Text") > 1 Then
Selection.HomeKey unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
End If
Selection.MoveDown unit:=wdLine
Loop

End Sub
 
E

Ed

Good idea! Use InStr to return the position of the text strings, and I can
repeat that as much as needed to confirm I am where I need to be. Thanks
Kevin.

Ed
 

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