search and insert

T

Tony

I have to perform the following action it the text data files. I have to find
the pattern, if found check if the word on the beginning of the paragraph is
ENTRY, if yes move 1 paragraph down and insert new paragraph with some text.
I have difficulties with repeating of the searches to the end of the file. I
am trying the following code but it is not doing what I need:

Sub Test()

SearchFor = "7322712"
PlaceNo = "123123123"

With ActiveDocument.Content.Find
Do While .Execute(FindText:=SearchFor, Forward:=True, _
Format:=False) = True
Selection.MoveLeft Unit:=wdWord, Count:=4
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
If Selection.Text = "ENTRY" Then
Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.TypeText Text:="FIELD,Marker," & PlaceNo
Selection.TypeParagraph
End If
Loop
End With

End Sub

Can someone help me and corret my code. Thank you.

Tony
 
T

Tony

Trying to solve my problem I have generated slightly modified code. I have to
add to that I have to find out if the word 4 words to the left of the found
match is ENTRY, not the word on the beginning of the paragraph. If yes I have
to perform insertion.

This is new version of my code which require correction:

Sub Test()

SearchFor = "7322712"
PlaceNo = "123123123"

With ActiveDocument.Content.Find
Do While .Execute(FindText:=SearchFor, Forward:=True, _
Format:=False) = True
With Selection
.MoveLeft unit:=wdWord, Count:=4
.MoveLeft unit:=wdWord, Count:=1, Extend:=wdExtend
If Selection.Text = "ENTRY" Then
With Selection
.MoveDown unit:=wdParagraph, Count:=1
.InsertAfter "FIELD,Marker," & PlaceNo
.TypeParagraph
End With
End If
End With
Loop
End With
End Sub

Thanks for help.

Regards,

Tony
 
T

Tony

I have done it. The code is:

Sub Test()

SearchFor = "7322712"
PlaceNo = "123123123"

With ActiveDocument.Content.Find
Do While .Execute(FindText:=SearchFor, Forward:=True, _
Format:=False) = True
.Parent.Select
With Selection
.MoveLeft Unit:=wdWord, Count:=4
.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
If Selection.Text = "ENTRY" Then
With Selection
.MoveDown Unit:=wdParagraph, Count:=1
.InsertAfter "FIELD,Marker," & PlaceNo & Chr(13)
End With
End If
End With
Loop
End With
Selection.HomeKey Unit:=wdStory

End Sub

Any comments which may improve my work.

Thank you.

Tony
 

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