Select/Display Only Lines With *string*

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Okay, I'm able to use Find and Find All in Word 2003 (for example, all
occurences of xyz). What I would like to do is for Word 2003 to then allow
me to select/copy ONLY those lines. I can do something vaguely similar in
Excel 2003 using the AutoFilter, but I'm limited to 64k lines, and I've got
some rather bodacious output files that I need to search and select. Nothing
I've found so far in the Help file seems to cover this. If you've come
across this, or if you know of a macro that would spit out lines to a
separate document based on a search criteria, I'd appreciate your help.
 
I guess this has to be solved with some VBA its not a particular had macro to
write. I doubt you even have to write it, Ok perhaps the looping part of it.

Try recording a macro where you find one instance of the string and then
using the home key to get to the beginning of the line and pressing end while
holding down the shift key to mark the row.

Then look at the macro and use something like this
while(trur)
'the Find stuff
if not selection.find.execute then
exit sub
end if
'the select line stuff
'do something with the selected line
wend
 
Well, I've tried a variation on the theme you suggested. Now, the problem is
that it seems to continue looping even after 'wend' is hit. I tried including
a "stop" command but either the system doesn't recognise it or just doesn't
care ( * snif * ).

Just so you know, I'm trying to pull about 200 lines (got the number from a
find-all search) out of a 16,000 page document.

Here's what I've got so far...


Sub FindLine2Copy()
'
' FindLine2Copy Macro
' Macro recorded 07/26/2007 by Doc Farmer
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "xyz"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.EndKey Unit:=wdLine
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
While (True)
Windows(2).Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "xyz"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.EndKey Unit:=wdLine
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Windows(2).Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "xyz"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.EndKey Unit:=wdLine
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Windows(2).Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "xyz"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.EndKey Unit:=wdLine
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Wend
Windows(1).Activate
Stop
End Sub
 
Well first of all. Why do u have four different find sections?

But I can see the error. You are letting your search wrap.
.Wrap = wdFindContinue
Which means that when it reaches the end of the document if will just
continue from the start of it again. I think you have to use wdFindStop

And then you need to check if the execute returns 0 or something else.
If its zero it means the search did not find any more string you where
looking for.

Just make sure you start from the beginning of your document in that case.
And search downwards

You have to use the If statement I wrote in my previous post.
 
I'm not sure. I used the Record Macro function, and I think the second find
is just the fact that I don't close the find window when I create the new
document.

I hate to be a pest, but WHERE do I put the IF statement and the wdFindStop
you mentioned? I'm an IT guy, but not a programmer, so this is new territory
for me.
 
this is taken from some of my code

With docRange.Find
.Text = "?@^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop '<<<< stop goes here
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
If Not docRange.Find.Execute Then ' <<<< The if statement
MsgBox prompt:="Error: Can not find header"
Exit Function
End If

replace the docRange with Selection in your case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top