PDA

View Full Version : [SOLVED:] Need Word search string



gilbro
07-15-2015, 08:10 AM
I have text in a Word file that looks something like this:
A. Hello, I Love Lucy, Dodge.
[Some other text]
B. Hello, The Bachelor, Chevy.
[Some other text]
C. Hello, The Tonight Show, Toyota.
[Some other text]
D. Hello, Seinfeld, Toyota.
[Some other text]


What I want is a script to convert it to:
A. Hello, I Love Lucy, Dodge.
[Some other text]
B. Hello, The Bachelor, Chevy.
[Some other text]
C. The Tonight Show, Toyota.
[Some other text]
D. Seinfeld, Toyota.
[Some other text]


Specifically, every time I find that the last word in the line is "Toyota" (and not some other car), then go to the beginning of the line and keep the listing letter but delete "Hello, ".

I have tried the following:
Selection.Find.ClearFormatting
With Selection.Find
.Text = ". Hello,?*, Toyota."
.Forward = True
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.MoveRight Unit:=wdCharacter, Count:=7, Extend:=wdExtend
Selection.TypeBackspace

...but this selects everything after the "A." all the way through the end of line C. It will work for the first instance (in line C), but if I run it a second time, it will again find everything from after the "A." to the end of line C, then go back to the beginning of line C and delete the first 7 characters of "The Tonight Show."

I haven't been clever enough to find the right search string that finds only the whole line which begins "X. Hello," and ends with ", Toyota", and which doesn't also include parts of preceding lines.

Help will be appreciated.
Thanks, Gil

gilbro
07-15-2015, 12:44 PM
I finally figured it out!

I have previously determined the list length with Dim ListLength as Integer.

Then I run this:

Dim selStr As String
Dim theCount As Integer
theCount = 1

Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = Chr(theCount + 64) + ".Hello,?* Toyota.^13"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.MoveRight Unit:=wdCharacter, Count:=7, Extend:=wdExtend

selStr = Selection.Range.Text
If selStr = "Hello, " Then
Selection.Delete
End If

theCount = theCount + 1
Loop Until theCount > ListLength

Basically, this looks for "A. Hello, [any tv show], Toyota" then "B. Hello, [any tv show], Toyota", etc for the number of items in the listing, using the Chr(theCount + 64) to generate A, B, etc. If it finds the line ending in Toyota, it goes to the beginning of the line, skips the "A. " (or "B. " etc) and checks whether the next text is "Hello, ". If it is, it deletes that text. If it's not, that means the line has been "cleaned" already and skips the deletion, and searches for the next line in the list to se if it ends in Toyota...

gmayor
07-15-2015, 11:37 PM
I suppose that might work, but I wouldn't have done it that way. Try the following



Sub RemoveText()
Dim oPara As Paragraph
Dim oRng As Range
Const strFind As String = "Toyota" 'The text to find
Const strReplace As String = "Hello, " 'The text to remove
'Check each paragraph
For Each oPara In ActiveDocument.Paragraphs
'Does the paragraph contain the find string?
If InStr(1, oPara.Range, strFind) > 0 Then
'It does so set a range to the paragraph
Set oRng = oPara.Range
'Remove the paragraph break from the range
oRng.End = oRng.End - 1
'If the range contains the replace string, remove it
oRng.Text = Replace(oRng.Text, strReplace, "")
End If
'and move on to the next paragraph
Next oPara
lbl_Exit:
'clean up
Set oPara = Nothing
Set oRng = Nothing
Exit Sub
End Sub