none
Macro code help: Extracting text between [brackets] from Word file into a new file

    Question

  • Hello everyone,

    I'm running Microsoft Word 2010 off of a Dell laptop.

    I have a .docx (created on a Mac) with lots of information between [brackets] like this that I would like to extract and place in a separate document. I want to keep the stuff between brackets, but nothing else.

    I don't know how to write code for macros at all. I do know the very basics of how they work. I can get to Visual Basic and understand how to create a new macro, but I'm pretty green with all of this.

    Is this action even possible?

    One other thing (in case it matters): All of the information I want is in boldface. I saw in another forum a code to extract boldface from a sentence range, but some of the sentences aren't complete and are only partially bold. I ran that macro and it did indeed skip stuff, so that code won't work, unfortunately.

    I would greatly appreciate any help you can offer.

    Thank you so much!

    Friday, August 02, 2013 7:36 PM

Answers

  • Since your post is not related to VSTO coding it really should be in the Word Developers forum. A moderator will move it.

    The following code should get you going... Good Luck

    Sub ExtractText()
        Dim cDoc As Word.Document, nDoc As Word.Document
        Dim cRng As Word.Range, nRng As Word.Range
        
        Set cDoc = ActiveDocument
        Set nDoc = Documents.Add
        
        Set cRng = cDoc.Content
        Set nRng = nDoc.Content
        
        cRng.Find.ClearFormatting
        With cRng.Find
            .Forward = True
            .Text = "["
            .Wrap = wdFindStop
            .Execute
            Do While .Found
                cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
                cRng.MoveEndUntil Cset:="]", Count:=Word.wdForward
                nRng.FormattedText = cRng.FormattedText
                nRng.InsertParagraphAfter
                nRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
                cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
                .Execute
            Loop
        End With
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, August 02, 2013 9:19 PM

All replies

  • Do you know if those brackets are actually bookmarks? A quick way to tell is to go to the Insert tab on your ribbon and click Bookmarks. The dialog that opens will show a list of all bookmarks.

    Are you planning on doing this with VBA or VSTO?


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, August 02, 2013 8:30 PM
  • Hi Rich,

    Thanks for the reply. They are not bookmarks, unfortunately. Just text between brackets.

    I plan to run this on VBA.

    Thanks so much for your help!

    Friday, August 02, 2013 8:41 PM
  • Since your post is not related to VSTO coding it really should be in the Word Developers forum. A moderator will move it.

    The following code should get you going... Good Luck

    Sub ExtractText()
        Dim cDoc As Word.Document, nDoc As Word.Document
        Dim cRng As Word.Range, nRng As Word.Range
        
        Set cDoc = ActiveDocument
        Set nDoc = Documents.Add
        
        Set cRng = cDoc.Content
        Set nRng = nDoc.Content
        
        cRng.Find.ClearFormatting
        With cRng.Find
            .Forward = True
            .Text = "["
            .Wrap = wdFindStop
            .Execute
            Do While .Found
                cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
                cRng.MoveEndUntil Cset:="]", Count:=Word.wdForward
                nRng.FormattedText = cRng.FormattedText
                nRng.InsertParagraphAfter
                nRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
                cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
                .Execute
            Loop
        End With
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, August 02, 2013 9:19 PM
  • This code worked flawlessly. Thank you kindly, so very much!
    Friday, August 02, 2013 10:14 PM