PDA

View Full Version : VBA If, Then using Find question



thegreg
04-02-2010, 09:28 AM
Hi, everyone. I'm new here. I'm fairly new to Word macros, too. I'm just having a problem with this macro and I don't know what I'm doing wrong.

The point of this macro is to highlight each line of the document, search to see if it has this string of text (|100=), if it does, just move down to the next line and search that one. If it doesn't, then I need it to either do a backspace or maybe prompt the user. Here's what I have

Do
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "|100="
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Selection.Find.Found = False Then
Selection.HomeKey Unit:=wdLine
Selection.TypeBackspace
Selection.MoveDown Unit:=wdLine, Count:=1
Else
Selection.MoveDown Unit:=wdLine, Count:=1
End If

Loop Until (Selection.End = ActiveDocument.Content.End - 1)

I also tried it with
If Selection.Find.Execute Then
(move down a line)
Else
(Do the 'homekey, backspace' thing)

Any ideas how I can make this work? Thanks so much for any help!

thegreg
04-06-2010, 07:11 AM
Nothing? Ouch. I'll keep searching.

fumei
04-06-2010, 09:40 AM
"Any ideas how I can make this work? "

Very likely, yes, but I am unclear as to what - exactly - you want to make "work".

" then I need it to either do a backspace or maybe prompt the user. "

Which? I am certain we can make something work, but you have to make a decision as to what - exactly - that is to be.

BTW: here are some possible improvements. IF by "line" you mean paragraph, then use paragraph.

"The point of this macro is to highlight each line of the document, search to see if it has this string of text (|100=), if it does, just move down to the next line and search that one. If it doesn't, then I need it to either do a backspace or maybe prompt the user."

1. you do not need to highlight, or select, anything.
2. by "backspace" it appears you want the "line" - hopefully actually PARAGRAPH - deleted.

So, the following does exactly that. It goes through the document, and if a paragraph has "|100 =", it is ignored. If it does NOT have the string, it is deleted. This is the code.
Sub YaddaBlah()
Dim r As Range
Dim oPara As Paragraph

For Each oPara In ActiveDocument.Paragraphs
If InStr(1, oPara.Range.Text, "|100 =") = 0 Then
' do nothing
Else
Set r = oPara.Range
r.Delete
End If
Next
End Sub
Demo attached. Click "YaddaBlah" on the top toolbar. Take a look at the text first.

fumei
04-06-2010, 09:43 AM
Actually, the code can be even shorter.
Sub YaddaBlah()
Dim oPara As Paragraph

For Each oPara In ActiveDocument.Paragraphs
If InStr(1, oPara.Range.Text, "|100 =") = 0 Then
' do nothing
Else
oPara.Range.Delete
End If
Next
End Sub
as you do not technically need the separate range object.