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
Using latest Word from MS365. It's a one-shot thing, so I can hard-code the string I'm interested in, in this case "@hotmail.com". So I would open my big file in word, the launch the script that would basically run trough the whole file, looking for my string, and when it finds it, print the line the string is on.
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
- Eskimo007Mar 09, 2025Copper Contributor
You rule Doug! Respect!