Forum Discussion
Need a macro to create a subdocument
- Mar 08, 2025
Use a macro containing the following code
Sub MakeSubDoc()
Dim source As Document
Dim target As Document
Dim str As String
Dim rngstr As Range
Set source = ActiveDocument
Set target = Documents.Add
source.Activate
str = InputBox("Insert the text to be found.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=str, MatchWildcards:=False, MatchCase:=True, Wrap:=wdFindStop, Forward:=True) = True
Set rngstr = Selection.Range.Bookmarks("\line")
target.Range.InsertAfter rngstr & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
target.Activate
End Sub
I know a fair amount about Word, and some DOS (having started with 1.1), but have no idea what GREP is. I suspect it to be a Linux command.
You might have better luck asking about GREP in the Windows forum.
Have you looked at the AutoText feature in Word?
Thanks for considering my request Charles. grep is just a linux command to locate a string. the OR vertical bar is a pipe, ie you can in linux use the TYPE command to display the content of a file, the pipe the output to another command, like grep, to basically print the content of a string on your terminal everytime it appears in a file. ie "type <file>|grep <string>" all on one line for example.