Link to home
Start Free TrialLog in
Avatar of isischen
isischen

asked on

trying to retrieve the end range of a range object

I want to retrieve the end range of a range object but my code in the follow only gave a return of value 1:
Sub cntEndRng()
Dim x As Range, y As Range, z As Range
Dim xrnt As Long, yrnt As Long, zrnt As Long
Set x = ActiveDocument.Paragraphs(1).Range
Set y = ActiveDocument.Paragraphs(1).Range
xrnt = x.Start
yrnt = y.End
Set z = ActiveDocument.Range(xrnt, yrnt)
if z.find.execute("_")=ture then
zrnt=z.moveuntil("_",wdforward)
set a=activedocument.range(xrnt,xrnt+zrnt)
arnt=a.end
MsgBox arnt
End Sub
I and looking for _ in a text and need to know the position of that _.
What could I have done differently to get the right result?
Thank you.
Avatar of isischen
isischen

ASKER

I am using Microsoft Word 2003' VBA language.
Avatar of GrahamSkan
It is not at all clear what you are trying to do.

If you want to find the range of the first "_" in the first paragraph of of the active document, all you need is this.

Sub cntEndRng()
    Dim z As Range
   
    Set z = ActiveDocument.Paragraphs(1).Range
    If z.Find.Execute("_") = True Then
            MsgBox "The string: ""_"" is in the first paragraph, ending " & z.End & " characters from the start of the document."
    Else
            MsgBox "There is no ""_"" is in the first paragraph."
    End If
End Sub
GrahamSkan, thank you for your comment but your sub always run into the ELSE:...  no "_" msgbox.
I am going to find a number of target symbols and increments each range along the way therefore I need to know each start and end position of each symbol found in a paragraph.  So that I could referencing each symbol in a seperate sub.
sorry, GrahamSkan, Your code did worked after all but I want to find the end posotion of an incremented range summed from a series of variable of return by the MOVEUNTIL method.
You can keep use the Find object to locate subsequent positions

Sub cntEndRng()
    Dim z As Range
    Dim Ends() As Long
    Dim i As Integer
    Dim strFindText As String
   
    strFindText = "_"
    Set z = ActiveDocument.Paragraphs(1).Range
    Do While z.Find.Execute(strFindText)
        ReDim Preserve Ends(i)
        Ends(i) = z.End
        Debug.Print z.End
        i = i + 1
    Loop
    MsgBox "There are " & i & " occurences of """ & strFindText & """ in the first paragraph."
End Sub


I misunderstood, because your example showed only the one character. I'll create some similar code for Moveuntil.
My symbol varies so I can't use a loop.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial