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.
BHW wrote:
> Some simple criteria for offending text includes long sentences (say,
> 40 words or more - easy to check for) or sentences with multiple
> instances of "of" (e.g., the x of y of z - probably harder to check
> for). I already know how to find long sentences, but the pasting and
> switching back and forth is beyond my current level.
>
> Thanks for your interest and help :)
>
> Bruce
>
>
> On Mar 25, 11:34 am, "Jay Freedman"
>> The last three steps are easy enough (although I wouldn't use
>> copy/paste and "go back" -- there are much better ways to do that).
>> The hard part, at least from your description, is defining what
>> "offending text" to look for. Is it always the same phrase, or is
>> there some other way to recognize it?
>>
>> --
>> Regards,
>> Jay Freedman
>> Microsoft Word MVP FAQ:http://word.mvps.org
>> Email cannot be acknowledged; please post all follow-ups to the
>> newsgroup so all may benefit.
>>
>> BHW wrote:
>>> Jay (or anyone else),
>>
>>> Can you possibly expand this example to show the following:
>>
>>> 1) find some text in the original document
>>> 2) copy and paste it in the second document
>>> 3) go back to first document
>>> 4) find more text (i.e., some loop structure that finds the text in
>>> doc1 and transfers it to doc2)
>>
>>> - what I have in mind is some sort of intelligent proofreader that
>>> will (with my codes instructions) find offending text in doc1 and
>>> put it in doc2. I can then read only the offending text in doc2 and
>>> see if I need to change it. As a bonus, it would be nice to put
>>> bookmarks in doc1 when I find offending text.
>>
>>> Thanks,
>>
>>> Bruce
>>
>>> On Jan 26, 1:30 am, Jay Freedman
>>>> On Fri, 25 Jan 2008 16:44:00 -0800, Alan Stancliff
>>
>>>>
>>>>> Is it possible to write a macro that does the following:
>>>>> 1. Notice whatdocumentthe cursor is in
>>>>> 2. Open anewdocument. Perform some task there
>>>>> 3. Close thenewdocumentwithout saving.
>>>>> 4. Go back to thedocumentthat was active when the macro began
>>
>>>>> Regards,
>>
>>>>> Alan
>>
>>>> Sub demo()
>>>> Dim firstDoc AsDocument
>>>> Dim secondDoc AsDocument
>>
>>>> ' remember where you parked the car
>>>> Set firstDoc = ActiveDocument
>>
>>>> ' make anewdoc based on Normal.dot,
>>>> ' which automatically becomes active
>>>> Set secondDoc = Documents.Add
>>
>>>> ' do some work in secondDoc
>>>> secondDoc.Range.Text = "Hello Word"
>>
>>>> ' close without saving
>>>> secondDoc.Close SaveChanges:=wdDoNotSaveChanges
>>
>>>> ' firstDoc probably becomes active here,
>>>> ' but if not then force it
>>>> firstDoc.Activate
>>>> End Sub
>>
>>>> --
>>>> Regards,
>>>> Jay Freedman
>>>> Microsoft Word MVP FAQ:http://word.mvps.org
>>>> Email cannot be acknowledged; please post all follow-ups to the
>>>> newsgroup so all may benefit.