Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jun 2003
    Location
    Minneapolis, Minnesota, USA
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Best Way to Make a List of Selected Text? (Word 2002)

    I've been trying to create a macro to create a list of selected text within a Word document. The text in question usually has the following format:

    Projection 34 has an abutting surface 36 which is used to abut against the rear surface 16 of a lower course of blocks, thereby forming a setback between successive courses of blocks.

    What I need to do is to go through the document, select each reference numeral and the preceding two-three words, copy these words and the associated numeral, and then paste them into a list in new document or at the end of the existing document. What I would end up with would be a rough list that I could format and edit to create a comprehensive list of the elements in my document. While it would be wise to do this up front, as the document was typed, there exist many such documents for which I have no element list.

    Anyone have a suggestion on how to best accomplish this?

    cj

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Best Way to Make a List of Selected Text? (Word 2002)

    Does this do what you want?

    StuartR

    Sub DoIt()
    Dim i As Integer
    Dim docNew As Document
    Dim rngSearch As Range

    Set rngSearch = ActiveDocument.Content
    Set docNew = Documents.Add

    i = 0 ' Prevent infinite loops...

    With rngSearch.Find
    .ClearFormatting
    .Text = " [0-9]{2,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True

    Do While .Execute = True And Count < 5000
    Count = Count + 1
    With .Parent
    .MoveStart Unit:=wdWord, Count:=-2
    docNew.Content.InsertAfter .Text & vbCrLf
    .Collapse Direction:=wdCollapseEnd
    End With
    Loop
    End With
    End Sub

  3. #3
    New Lounger
    Join Date
    Jun 2003
    Location
    Minneapolis, Minnesota, USA
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Best Way to Make a List of Selected Text? (Word 2002)

    Stuart:
    You da man!
    I've still got some memory of basic programming structure, but I know very little about the arcane commands withing VB that apply to MS Word.
    One small followup question: if the reference numeral in question has a small letter tacked on so that we are now talking about an alphanumeric string (e.g. 12a, 14c, 25''') how would you modify this macro to catch those extra characters. It seems that the sngSearch function automatically selects the numerals between [1-9] but ends there, regardless of the non-numeric characters. Any way to simply select the alphanumeric string in its entirety (similar to Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend)?

    Thanks again. Even if the alphanumerics can't be picked up, I'm still ahead of where I was.

    CJ

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Best Way to Make a List of Selected Text? (Word 2002)

    If you just include
    .MoveEnd Unit:=wdWord
    immediately before or after the .MoveStart

    Then you will get any characters after the two digit number, including the white space before the next word.

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •