R
Ridge Kennedy
Dear Group,
Inspired by comments from one of you about the value of using range vs.
selection for working with Word, I've been working to get a grasp of how to
do that. (It's sooo much easier for me to get record it on the keyboard
don'cha know). MS and most of the documents I've found tell me how to
identify the first or third paragraph, beginning or end of document, stuff
like that. Straightforward, but not always the useful information I thought
I needed.
Following is code for three subs. The first just finds the current cursor
location (or start of a current selection). The latter two identify the
paragraph where the cursor is currently located and set that "current"
paragraph as a range.
I'd appreciate comments, corrections and recommendations for improvements.
I'd also appreciate suggestions for strategy on a way to select the current
*line* as a range. Select properties (or are they methods -- I still don't
grock that) make it fairly easy to select a single line in the document. I
haven't figured out how to do it with range.
Any other useful tips on manipulating ranges also greatly appreciated.
Here are my subs (novice code writer -- be gentle):
Sub findCurrSelLocation()
' find the character number of the current
' cursor location or start of current selection
Dim lPosNow As Long
lPosNow = Selection.Range.Start
MsgBox ("The insertion point" & vbCrLf & "(or selection area)" _
& vbCrLf & "is at character location:" & lPosNow)
End Sub
Sub SetCurrParagraphAsRange()
' procedure to set the paragraph where the cursor
' is currently located as myrange
Dim myrange As Range
Dim lPos1 As Long
Dim lPos2 As Long
Dim oDoc As Object
Set oDoc = ActiveDocument
lPos1 = Selection.Paragraphs(1).Range.Start
lPos2 = Selection.Paragraphs(1).Range.End
Set myrange = oDoc.Range(Start:=lPos1, End:=lPos2)
' run sub showrange that changes the text color of myrange
' to verify where range is set
showrange myrange
End Sub
Sub SetCurrParagraphAsRangeII()
' procedure to identify the index number of the
' current paragraph and set it as myrange
Dim myrange As Range
Dim oDoc As Object
Dim lPosNow As Long
Dim lParCount As Long
Dim lStartDoc As Long
Set oDoc = ActiveDocument
Set myrange = oDoc.Content
' set myrange from start of document to
' current cursor location
lStartDoc = myrange.Start
lPosNow = Selection.Range.Start
Set myrange = oDoc.Range(Start:=lStartDoc, End:=lPosNow)
' store the count of paragraphs in myrange in lParCount
lParCount = myrange.Paragraphs.Count
' redefine myrange using the count to select
' the currently selected paragraph
Set myrange = oDoc.Paragraphs(lParCount).Range
' run sub that changes the text color of myrange
' to verify where range is set
showrange myrange
End Sub
Thanks. Ridge (in New Joisey[Exit 145])
Inspired by comments from one of you about the value of using range vs.
selection for working with Word, I've been working to get a grasp of how to
do that. (It's sooo much easier for me to get record it on the keyboard
don'cha know). MS and most of the documents I've found tell me how to
identify the first or third paragraph, beginning or end of document, stuff
like that. Straightforward, but not always the useful information I thought
I needed.
Following is code for three subs. The first just finds the current cursor
location (or start of a current selection). The latter two identify the
paragraph where the cursor is currently located and set that "current"
paragraph as a range.
I'd appreciate comments, corrections and recommendations for improvements.
I'd also appreciate suggestions for strategy on a way to select the current
*line* as a range. Select properties (or are they methods -- I still don't
grock that) make it fairly easy to select a single line in the document. I
haven't figured out how to do it with range.
Any other useful tips on manipulating ranges also greatly appreciated.
Here are my subs (novice code writer -- be gentle):
Sub findCurrSelLocation()
' find the character number of the current
' cursor location or start of current selection
Dim lPosNow As Long
lPosNow = Selection.Range.Start
MsgBox ("The insertion point" & vbCrLf & "(or selection area)" _
& vbCrLf & "is at character location:" & lPosNow)
End Sub
Sub SetCurrParagraphAsRange()
' procedure to set the paragraph where the cursor
' is currently located as myrange
Dim myrange As Range
Dim lPos1 As Long
Dim lPos2 As Long
Dim oDoc As Object
Set oDoc = ActiveDocument
lPos1 = Selection.Paragraphs(1).Range.Start
lPos2 = Selection.Paragraphs(1).Range.End
Set myrange = oDoc.Range(Start:=lPos1, End:=lPos2)
' run sub showrange that changes the text color of myrange
' to verify where range is set
showrange myrange
End Sub
Sub SetCurrParagraphAsRangeII()
' procedure to identify the index number of the
' current paragraph and set it as myrange
Dim myrange As Range
Dim oDoc As Object
Dim lPosNow As Long
Dim lParCount As Long
Dim lStartDoc As Long
Set oDoc = ActiveDocument
Set myrange = oDoc.Content
' set myrange from start of document to
' current cursor location
lStartDoc = myrange.Start
lPosNow = Selection.Range.Start
Set myrange = oDoc.Range(Start:=lStartDoc, End:=lPosNow)
' store the count of paragraphs in myrange in lParCount
lParCount = myrange.Paragraphs.Count
' redefine myrange using the count to select
' the currently selected paragraph
Set myrange = oDoc.Paragraphs(lParCount).Range
' run sub that changes the text color of myrange
' to verify where range is set
showrange myrange
End Sub
Thanks. Ridge (in New Joisey[Exit 145])