Hi Bruce,
Study this example. Don't get nervous; it's more comments than code.
Sub demo()
Dim SrcDoc As Document ' the document being checked
Dim DestDoc As Document ' the document to receive text
Dim SrcRg As Range ' the text currently being checked
Dim DestRg As Range ' the place to copy it to
' As simple example, assume we're checking the
' currently active document. Could use Documents.Open
' to get user's choice instead.
Set SrcDoc = ActiveDocument
' Create a new blank doc to receive text, and the location
' in it to insert copied text.
Set DestDoc = Documents.Add
Set DestRg = DestDoc.Range
DestRg.Collapse wdCollapseEnd
For Each SrcRg In SrcDoc.Sentences
' The Sentences collection contains ranges, one per
' "sentence". Note that Word's idea of a sentence may
' not match yours, depending on how you punctuate and
' what non-text items (tables, etc.) are in the document.
' Call the function to check whether the text needs
' to be copied to the destination document.
If IsOffendingText(SrcRg) Then
' Copy the source to the destination without
' using the clipboard. Add a paragraph mark to
' separate entries.
DestRg.FormattedText = SrcRg.FormattedText
DestRg.InsertAfter vbCr
' Adjust the location for the next insertion
Set DestRg = DestDoc.Range
DestRg.Collapse wdCollapseEnd
End If
Next SrcRg
' Optional: open the Save dialog to name and save DestDoc
' DestDoc.Save
End Sub
Private Function IsOffendingText(objRg As Range) As Boolean
' Build code here to examine the text in objRg.
' Return True if it meets the criteria for offending text,
' otherwise return False.
Dim Result As Boolean
' Default value:
Result = False
' Example: offending if it contains more than 40 words
Result = (objRg.Words.Count > 40)
' Another example: offending if it starts with "But"
If Not Result Then
Result = (objRg.Words(1).Text = "But ")
End If
' Keep checking more criteria until one of them is true
' or you have checked them all...
' Finally, set the function value
IsOffendingText = Result
End Function
Things to notice: By using two range objects, one in the "source" document
and one in the "destination" document, you can copy text from one to the
other simply by assigning the FormattedText property of one to the other.
This (a) does not use the clipboard, and (b) does not change which document
is active. Everything is done by referring to the two ranges and the two
document objects; there is no mention of ActiveDocument or Selection other
than to assign SrcDoc (and, as noted, even that could be done differently).
The part that needs your attention is the code in the function
IsOffendingText that looks at the text of the current range and decides
whether it's "offending" and thus needs to be copied. The way I've shown
this setup, each sentence in the document will be examined only once, and it
will be copied if it meets any of the criteria. If you need separate lists
of sentences matching separate criteria, with the same sentence in more than
one list, then the macro needs to be restructured (there are a couple of
ways you might do that).
As noted in one of the comments, it's possible that some of the things Word
regards as a "sentence" aren't what you would expect. If you get odd-looking
results, single-step through the code with the F8 key and watch the contents
of SrcRg.Text.