Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-02-2017, 03:14 AM
paik1002 paik1002 is offline How to get start and end range indices of a selection Windows 7 64bit How to get start and end range indices of a selection Office 2010 64bit
Advanced Beginner
How to get start and end range indices of a selection
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default How to get start and end range indices of a selection

Two related questions:



Q1. How do I detect if text is already selected.
Q2. If text has already been selected (highlighted), how to get start and end range indices of the selection?

I need to get the text re-selected after doing something else (hence above text selection becomes unselected), so I need to know the exact range indices.

VBA codes please. Help would be appreciated.
Reply With Quote
  #2  
Old 07-02-2017, 05:04 AM
gmayor's Avatar
gmayor gmayor is offline How to get start and end range indices of a selection Windows 10 How to get start and end range indices of a selection Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Declare and apply a range to the selection, while it is selected e.g.

Code:
Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 02 Jul 2017
Dim oRng As Range
    Set oRng = Selection.Range
    If Len(oRng) = 0 Then
        MsgBox "Nothing selected!"
    Else
        'do something with orng
    End If
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 07-02-2017, 05:39 AM
paik1002 paik1002 is offline How to get start and end range indices of a selection Windows 7 64bit How to get start and end range indices of a selection Office 2010 64bit
Advanced Beginner
How to get start and end range indices of a selection
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default

Thank you!
One more question related to your answer: How re-select the same text (maybe after being unselected) using another macro (e.g. Macro2)?

Macro1 will get the range of a selected text and memorize it.
Macro2 will find the selected text and reselect it from memory.

Yours gratefully.
Reply With Quote
  #4  
Old 07-02-2017, 06:21 AM
gmaxey gmaxey is online now How to get start and end range indices of a selection Windows 7 32bit How to get start and end range indices of a selection Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Provided something doesn't happen to drop the variable out of scope something like this perhaps:

Code:
Option Explicit
Private oRng As Range
Sub SetSelection()
  Set oRng = Selection.Range
End Sub
Sub Reselect()
  oRng.Select
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 07-02-2017, 07:35 AM
paik1002 paik1002 is offline How to get start and end range indices of a selection Windows 7 64bit How to get start and end range indices of a selection Office 2010 64bit
Advanced Beginner
How to get start and end range indices of a selection
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default

Bulls eye! Thank you very much and have a good day.
Reply With Quote
  #6  
Old 07-02-2017, 08:08 PM
gmayor's Avatar
gmayor gmayor is offline How to get start and end range indices of a selection Windows 10 How to get start and end range indices of a selection Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The beauty of ranges is that you don't need to select them in order to process them. What was it that you were doing with the 'selection'?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 07-02-2017, 09:44 PM
paik1002 paik1002 is offline How to get start and end range indices of a selection Windows 7 64bit How to get start and end range indices of a selection Office 2010 64bit
Advanced Beginner
How to get start and end range indices of a selection
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default

I am planning to use it mostly for navigational purposes combined with editing functions such as copy/paste. I do get lost a lot when moving around in a document, so I found it convenient to keep track of past edits.

Since you mention the versatility of ranges,
may I ask further how I can extend or contract ranges given the current selection of text (e.g. oRng.Select) or the memory of it (e.g. Set oRng = Selection.Range)?
Maybe add/subtract an adjacent word at the outer periphery (front or back) of the current selection of text.

For example:
the body of text is: Hello there how are you doing these days?
current selection: how are you doing
add to selection: how are you doing these days?
subtract to selection: how are you

Please kindly provide example VB codes if possible.
Reply With Quote
  #8  
Old 07-02-2017, 10:44 PM
gmayor's Avatar
gmayor gmayor is offline How to get start and end range indices of a selection Windows 10 How to get start and end range indices of a selection Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can manipulate a range in a variety of ways, but a couple of examples using your text. Note that you don't have to select the range to process it. The .select rows are only inserted to show what is happening to the range directly in the text. If you are moving range ends then choose a method that is unambiguous with regard to the text

Code:
Sub Macro1()
Dim oRng As Range
    Set oRng = Selection.Range
    oRng.MoveEndUntil "?"
    oRng.End = oRng.End + 1
    oRng.Select    'not necessary to process the range
    MsgBox oRng.Text
    oRng.MoveEndUntil "u", wdBackward
    oRng.Select    'not necessary to process the range
    MsgBox oRng.Text
End Sub

Sub Macro2()
Dim oRng As Range
    Set oRng = Selection.Range
    'move to the end of the paragraph (before the paragraph mark)
    oRng.End = oRng.Paragraphs(1).Range.End - 1
    oRng.Select    'not necessary to process the range
    MsgBox oRng.Text
    'Move the end back four 'words'
    oRng.MoveEnd wdWord, -4
    'Remove the final space from the range
    oRng.End = oRng.End - 1
    oRng.Select    'not necessary to process the range
    MsgBox oRng.Text
End Sub
You can also have multiple ranges e.g.
Code:
Sub Macro3()
Dim orng As Range
Dim oStart As Range
    Set oStart = Selection.Range
    Set orng = Selection.Range
    'move to the end of the paragraph (before the paragraph mark)
    orng.End = orng.Paragraphs(1).Range.End - 1
    orng.Select    'not necessary to process the range
    MsgBox orng.Text
    'Move the end back four 'words'
    orng.MoveEnd wdWord, -4
    'Remove the final space from the range
    orng.End = orng.End - 1
    orng.Select    'not necessary to process the range
    MsgBox orng.Text
    'go back to the original selection
    oStart.Select     'not necessary to process the range
    MsgBox oStart.Text
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Distribute text in one cell across a range of cells (overcoming selection.range.cells.count bug) slaycock Word VBA 0 02-18-2017 07:00 AM
How to get start and end range indices of a selection Working with Selection.range. PRA007 Word VBA 2 02-19-2016 12:52 AM
How to get start and end range indices of a selection Search and reduce the range of a text selection paik1002 Word VBA 1 12-17-2015 04:51 AM
Selection (range) in Word or Excel table NobodysPerfect Word VBA 2 09-16-2014 12:06 AM
Selection or Range Tommes93 Word VBA 1 04-10-2014 02:50 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:22 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft