1. as has been mentioned, whenever possible, avoid using Selection. Use Range.
2. if you want to keep performing action on a search string, while you CAN use Bong's
Code:
If myRange.Find.Found = True
it is not really required. If you make the .Execute (and its parameters) instruction go through a Boolean (True/False) test, this is the equivalent of .Find.Found = True/False. An .Execute can not be True unless its .Found is also True.
So, to recap what I understand you want to do. You have a userform and when you click a commandbutton
1. open "the" file (WHAT file??? How are you determining which file to open????)
2. make a string variable = the text of a Textbox
3. search the open file for the search string
4. for EVERY found instance of that search string, create a new document that will have the text of the "line" that contains the found string.
OK. Can be done. Some questions though. You make no mention of what you are doing with the new documents. Saving them? Keeping them all open?
As Skip mentions, the term "line" is a bit tricky in Word. Word primarily deals with
paragraphs.
IF - repeat IF - these "lines" are indeed paragraphs, then it makes things much easier. Let me demonstrate. When describing things, <p> is the standard for a paragraph mark.
This is the first
line of four "lines"
but it is really just
one single paragraph.<p>
The above has four "lines", but is ONE paragraph.
This is the first<p>
line of four "lines"<p>
but it is really<p>
four separate paragraphs.<p>
The above has four "lines", but is also FOUR paragraphs.
Can you see how similar they look? However, from Word's perspective (as if a piece of software can actually have a pespective...) they are VERY VERY different.
Again, IF your "lines" are actually paragraphs, then it makes it much easier. Here is some code.
Code:
Private Sub CommandButton1_Click()
Dim textSearch As String
Dim ThisDoc As Document
Dim WhateverDoc As Document
Dim r As Range
[COLOR=red]' where is the code to open "the" file??!!!
' This file MUST be open for the following to work
' as it uses ActiveDocument[/color red]
textSearch = TextBox1.Text
[COLOR=red]' this is the CURRENT open document![/color red]
Set ThisDoc = ActiveDocument
Set r = ThisDoc.Range
With r.Find
[COLOR=red]' for EVERY .Found[/color red]
Do While .Execute(FindText:=textSearch, _
Forward:=True)=True
With r [COLOR=red]' the range of the .Found[/color red]
.Expand Unit:=wdParagraph
.Copy
[COLOR=red]' make a [b]new[/b] blank doc[/color red]
Set WhateverDoc = Documents.Add
[COLOR=red]' put in the grabbed text and SaveAs[/color red]
With WhateverDoc
.Range.Paste
.SaveAs FileName:="c:\" & "Whatever" & _
j & ".doc"
.Close
End With
[COLOR=red]' Collapse the range [b]so loop can continue[/b][/color red]
.Collapse 0
End With
j = j + 1
Loop
End With
End Sub
The above will search for the text, expand the range to the paragraph it is in, make a new document, put the text of the paragraph in the new document, do a SaveAs, and then close the new file. It then Collapses the range in order to continue on until the search string is not found. The Collapse is very important!
I would strongly recommend using Document objects, as in the code above. It makes working with specific documents much more clear.
The above may not work quite correctly, as I do not know what you are doing regarding documents. The VBA is running from a document (of course), and you are opening a file? This is my assumption so the ThisDoc object is
that file, and the Whatever document is another, new, document to which the text is inserted.
Gerry