Forum Discussion

Eskimo007's avatar
Eskimo007
Copper Contributor
Feb 26, 2025
Solved

Need a macro to create a subdocument

I have a document with some lines that contain a string, say <astring>.  I would like to have a macro that would parse the document, and create a new document that only contains the lines that have <astring> in it.  Anyone willing to give me advice?

  • 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

10 Replies

  • Charles_Kenyon's avatar
    Charles_Kenyon
    Bronze Contributor

    I know a lot about Word. I know some things about DOS. I know nothing about GREP. I suspect it to be a Linux program.

    In DOS (or actually the Windows command line) you can use Batch files with the .bat extension to run a list of DOS commands.

    I think we have been corresponding in another forum as well. If I am correct, I ask you to look at A Message to Forum Cross-Posters.

    • Eskimo007's avatar
      Eskimo007
      Copper Contributor

      So you reply the same thing to me twice and accuse me of cross-posting (which I didn't do?) Sheesh.  Since nobody else with macro knowledge replied to me here, I leave disappointed.  Ecept that I found a novel way to identifying all the lines with the string I wanted, which is to open the ascii file in excel, and do its "Find all" which listed all the lines with the string, like I was hoping word's macro language could do.  Maybe next time I'll find myself a word macro self-teach online page.  Goodbye

    • Eskimo007's avatar
      Eskimo007
      Copper Contributor

      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.

       

  • Charles_Kenyon's avatar
    Charles_Kenyon
    Bronze Contributor

    Your macro is going to need some way to identify the string you want copied. Start there.

    Version of Word you are using?

    See also Boiler - Insert a selection of documents by Graham Mayor, MVP.

    Consider also putting such text snippets in a template in the form of AutoText. They can then be available for quick insertion in any document.

    By the way "subdocument" is a term or art or jargon in the Word context relating to a broken feature. Master Documents "Feature" in Microsoft Word

    • Eskimo007's avatar
      Eskimo007
      Copper Contributor

      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

  • Eskimo007's avatar
    Eskimo007
    Copper Contributor

    ...or maybe I can chain some DOS commands ala "type myfile.vcf|grep "@hotmail.com" >hotmaillines.txt... (Where can I find a grep program for DOS?)

    • Charles_Kenyon's avatar
      Charles_Kenyon
      Bronze Contributor

      Sorry. I know Word. I know DOS to some extent.

      I know not GREP.

      You might ask in a Windows forum.

      Have you looked at the AutoText feature in Word?

       

Resources