Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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,137
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 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



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 02:17 PM.


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