Microsoft Word Repeat Text Editing

D

dsda123

I'm trying to create a macro which will let me delete text that may/
may not repeat in the Word document somewhere else. What I mean is,
if on page 1 I have the words XX YY ZZ TT typed in, and on the next
page I have the words AA BB YY TT typed in, and on the third page the
words YY and TT and AA, I'd like for the macro to delete only the
repetitive entries of YY and TT on the 2nd and 3rd page, and the AA
entry on the 3rd page. If anyone could help me, I'd greatly I
appreciate it.

Thank you.
 
B

Bob I

That is not going to be a very easy task. How would it know that it is
"repetitive"? Also the "page" is determined by the printer. In reality
the document is one continuous data file.
 
H

Harlan Grove

[email protected] wrote...
I'm trying to create a macro which will let me delete text that
may/may not repeat in the Word document somewhere else. What I
mean is, if on page 1 I have the words XX YY ZZ TT typed in, and on
the next page I have the words AA BB YY TT typed in, and on the
third page the words YY and TT and AA, I'd like for the macro to
delete only the repetitive entries of YY and TT on the 2nd and 3rd
page, and the AA entry on the 3rd page. If anyone could help me,
I'd greatly I appreciate it.

If the document to which you were applying such a filter wasn't too
large in terms of text contents, you could use a VBA macro to store
the first instance of each word in the file in a WSH Scripting
Dictionary object with the word itself as the key. As you read through
the file, check if the current word already exists in the Dictionary
object. If so, delete the current instance of the word. If not, load
it into the Dictionary object and leave the word in place in the file.
This would also delete subsequent instances of XX YY ZZ TT in page 1
and AA and BB in page 2.


Sub foo()
'requires a reference to Microsoft Scripting Runtime
Dim w As Range, wc As Long
Dim od As New Scripting.Dictionary
For Each w In ActiveDocument.Words
wc = wc + 1
If w.Text Like "*[A-Za-z]*" Then
If od.Exists(w.Text) Then
w.Delete
Else
od.Add Key:=w.Text, Item:=wc
End If
End If
Next w
End Sub


If you want multiple instances of each word remain in the first page
in which that word appears, it gets more complicated. You'd have to
iterate through pages within panes, rectangles within pages.


Sub foo2()
'requires a reference to Microsoft Scripting Runtime
Dim p As Page, r As Rectangle, w As Range, pn As Long
Dim od As New Scripting.Dictionary
For Each p In ActiveDocument.ActiveWindow.Panes(1).Pages
pn = pn + 1
For Each r In p.Rectangles
For Each w In r.Range.Words
If w.Text Like "*[A-Za-z]*" Then
If od.Exists(w.Text) Then
If od(w.Text) < pn Then w.Delete
Else
od.Add Key:=w.Text, Item:=pn
End If
End If
Next w
Next r
Next p
End Sub


Finally, you'd be far better off asking this sort of question in
newsgroups specifically for Word, like microsoft.public.word,
microsoft.public.word.general or (especially in this case)
microsoft.public.word.programming.
 
Top