Results 1 to 4 of 4

Thread: Get selected text in VBA

  1. #1

    Thread Starter
    Hyperactive Member koolprasad2003's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    443

    Arrow Get selected text in VBA

    Dear all
    I have word document I which I have 10 lines. if I select line 1 and 7 with control key, how can I get only selected lines in VBA ?
    I have tried with 'Application.Selection.Text' but it gives me only last selected line that is only 7th Line
    Please help me to get out of this issue

    Thanks
    MCP, MCTS, Microsoft MVP [Asp.Net/IIS]

    For more .NET development tips visit .NET Tips

    If the post is useful then please Rate it

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Get selected text in VBA

    you could try copying the selection, then retrieve the text from the clipboard
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Get selected text in VBA

    There is very little that can be done with discontiguous selections without doing a bit of hackery type coding.

    See: Limited programmatic access to Word discontiguous selections

    With that stated, one method is to set a unique format on the Selection and then search for that format. I chose to set the Font Size to 1.

    Code:
    Selection.Font.Size = 1
    Dim foundText As New Collection ' to store the individual selections
    
    Dim searchRange As Word.Range
    Set searchRange = ActiveDocument.Range
    
    With searchRange.find
       .ClearFormatting
       .Font.Size = 1
       .Format = True
       .Execute
       Do While .Found
          foundText.Add searchRange.Text
          .Execute
       Loop
    End With
    ActiveDocument.Undo ' undo Font.Size = 1
    
    Dim txt As Variant
    For Each txt In foundText
       Debug.Print txt
    Next txt

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Get selected text in VBA

    i tested my previous suggestion, like
    Code:
    set myclip = createobject("Clipbrd.Clipboard")
    myclip.clear
    selection.copy
    debug.print myclip.gettext
    use your own method to access the clipboard, you can use a dataobject or APIs, else checkout
    http://www.vbforums.com/showthread.php?t=585616
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

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