Results 1 to 3 of 3

Thread: [RESOLVED] macro help, multiple text selects

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Resolved [RESOLVED] macro help, multiple text selects

    Hey all.
    I've written a few macros to make my office work faster, but I'm stuck on this one.
    I need to be able to select two separate pieces of text manually, using the CTRL key, and then run a macro that can handle these two pieces of text.
    Ultimately I'm dropping them into a database.
    The database part is all working.

    Where I'm stuck is on dealing with the selection in the macro.
    For example, if I select the word "hello" and then later down the document select "world" while holding down CTRL, the two items are both selected.
    But if I write a quick macro: Msgbox(Selection.Text)
    the output is "world"

    I need a way to get at both selected pieces.

    Anybody?
    thanks.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: macro help, multiple text selects

    Add the following function to your module:
    vb Code:
    1. Private Function GetSelectedRanges() As Range()
    2.     Dim doc As Document, rng As Range, tempStyle As Style
    3.     Dim selectedRanges() As Range, selectionCount As Long
    4.    
    5.     Set doc = ActiveDocument
    6.     ReDim selectedRanges(100) As Range
    7.    
    8.     '' Add a temporary style to the document
    9.     Set tempStyle = doc.Styles.Add("$$MyTemporaryStyle##", wdStyleTypeCharacter)
    10.     Selection.Style = tempStyle
    11.        
    12.     '' Now find this style in the document and work with it.
    13.     Set rng = doc.Range
    14.     With rng.Find
    15.         .ClearFormatting
    16.         .Style = tempStyle
    17.         .Text = ""
    18.         .Replacement.Text = ""
    19.         .Forward = True
    20.         .Wrap = wdFindStop
    21.         .Format = True
    22.         .MatchCase = False
    23.         .MatchWholeWord = False
    24.         .MatchWildcards = False
    25.         .MatchSoundsLike = False
    26.         .MatchAllWordForms = False
    27.         While rng.Find.Execute
    28.             selectionCount = selectionCount + 1
    29.             If selectionCount > UBound(selectedRanges) Then ReDim Preserve selectedRanges(selectionCount * 2) As Range
    30.             Set selectedRanges(selectionCount) = rng.Duplicate
    31.         Wend
    32.     End With
    33.     ReDim Preserve selectedRanges(selectionCount) As Range
    34.  
    35.     '' Remove the temporary style we added.
    36.     tempStyle.Delete
    37.    
    38.     GetSelectedRanges = selectedRanges
    39. End Function

    Sample Usage:
    vb Code:
    1. Sub Macro1()
    2.     Dim rng() As Range, i As Integer
    3.     rng = GetSelectedRanges
    4.     For i = 1 To UBound(rng)
    5.         Debug.Print rng(i)
    6.     Next
    7. End Sub
    Last edited by Pradeep1210; Feb 23rd, 2011 at 10:22 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: macro help, multiple text selects

    Hey.
    Thanks a lot.
    that worked well for what I needed, was very easy to adapt.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

Posting Permissions

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



Click Here to Expand Forum to Full Width