PDA

View Full Version : Solved: find whole line based on some text?



samohtwerdna
10-06-2009, 05:00 PM
Hello all,

I am trying to create a macro to clean up some documents that have way too much info. I need to write a function that will loop through my entire doc looking for lines with certain text

Example: I may have a line that looks like this
a. hx,Oj Hebrew Pp+xxnbfhkd (particle preposition)

If I do a find with wildcards for this text (Hebrew P* * ^13) I will find everything after word Hebrew, But I want to select (then delete) the whole line.

Also I would like to put the search parameters in an array because I will end up having more than ten different searches I need to do.

Maybe something like:

Do
If InStr(1, Selection.Text, "Hebrew P*") Then
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Selection.Delete

Loop


Any Help?

Thanks

samohtwerdna
10-06-2009, 05:24 PM
OK, the Do while was not good. Instead I went with:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Hebrew P"
While .Execute
oRng.Paragraphs(1).Range.Delete
Wend
End With

With oRng.Find
.Text = "Hebrew p"
While .Execute
oRng.Paragraphs(1).Range.Delete
Wend
End With

With oRng.Find
.Text = "Hebrew n"
While .Execute
oRng.Paragraphs(1).Range.Delete
Wend
End With


This works well enough but I hate repeating the code. Any Ideas how to get an array as the search text?

fumei
10-08-2009, 10:53 AM
Put your text strings into an array and use it.
Option Explicit

Sub Yadda()
Dim oRng As Word.Range
Dim Search()
Dim var

Search = Array("Hebrew P", _
"Hebrew p", "Hebrew n")

For var = 0 To UBound(Search)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = Search(var) ' each item in the array
While .Execute
oRng.Paragraphs(1).Range.Delete
Wend
End With
Next
End Sub

samohtwerdna
10-08-2009, 12:34 PM
Thanks fumei!

fumei
10-08-2009, 01:34 PM
You are just doing this one-time (each item)?

Also it is important to understand why the range is re-set for each loop:
For var = 0 To UBound(Search)
Set oRng = ActiveDocument.Range
It may not be needed, but generally it would be. I am not totally clear on what you are doing.

If this has indeed solved your question, please mark the thread as Solved, from the Thread Tools tabs at the top.

samohtwerdna
10-12-2009, 06:05 AM
Fumei,

Thanks for the help, I am not quite finished with the setup. I want to put the search ability in a UserForm and have an input text field for the end user to specify what to delete just by placing the first letter in the input and comma separating for more than one entry.
So I did this:
Private Sub cmdRun_Click()
Dim oRng As Word.Range
Dim Search()
Dim var


Search = Array(txtExclude.Value)

For var = 0 To UBound(Search)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Hebrew " + Search(var) ' each item in the array
While .Execute
oRng.Paragraphs(1).Range.Delete
Wend
End With
Next
End Sub

however, that does not work for separating the values?

samohtwerdna
10-12-2009, 07:32 AM
I tried to use the split function but I keep getting a type mismatch??


Search = Array(Split(txtExclude.Value, ","))

am I using the split wrong??

TonyJollans
10-13-2009, 02:51 AM
The Split function returns an array, so all you need to code is:

Search = Split(txtExclude.Value, ",")

samohtwerdna
10-13-2009, 04:44 AM
thanks Tony,

That actually did not work either because of the Dim Search()
but I removed that and it seems to work

samohtwerdna
10-14-2009, 07:50 AM
OK - So if I use
Option Explicit

I must Dim my variables but Dim Search() does not work neither does Dim Search As String

I get a type miss-match on
Search = Split(txtExclude.Value, ",")

But If I remove Option Explicit and any Dim for Search then it works just fine. That must mean Word is guessing what to use and I am declaring it incorrectly.

Any Ideas??

thanks,

fumei
10-14-2009, 09:30 AM
1. Use Option Explicit.

2. "Dim Search() does not work" Oh yes it does. But it work better if you:
Dim Search
Dim strIn As String
strIn = "one,two,three"
Search = Split(strIn, ",")
MsgBox Search(1)
Notice Search is declared without the parenthesis. Thus, it is declared as a Variant. NOT an array. The Split instruction makes it an array.

Dim Search()
Dim strIn As String
strIn = "one,two,three"
Search = Split(strIn, ",")
Declaring Search() - WITH the parenthesis - will indeed return a error 13, the dreaded type-mismatch.



Use Option Explicit.

samohtwerdna
10-14-2009, 10:18 AM
thanks fumei!

That works well. And thank for the explanation of the variant - very helpful