Solved

trying to retrieve the end range of a range object

Posted on 2007-11-25
9
241 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:isischen
  • 4
  • 4
9 Comments
 

Author Comment

by:isischen
ID: 20347925
I am using Microsoft Word 2003' VBA language.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20348439
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
0
 

Author Comment

by:isischen
ID: 20348626
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:isischen
ID: 20348700
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20348784
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


0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20348803
I misunderstood, because your example showed only the one character. I'll create some similar code for Moveuntil.
0
 

Author Comment

by:isischen
ID: 20348804
My symbol varies so I can't use a loop.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 20349026
Don't know why you can't use a loop, but this will find the position of any of the characters in the list defined by strFindText;
Sub cntEndRng()
    Dim z As Range
    Dim Ends() As Long
    Dim i As Integer
    Dim strFindText As String
    Dim c As Integer
    strFindText = "_a"
    Set z = ActiveDocument.Paragraphs(1).Range
    z.Collapse wdCollapseStart
    Do
        c = z.MoveUntil(strFindText)
        If c = 0 Then
            Exit Do
        End If
        If z.InRange(ActiveDocument.Paragraphs(1).Range) Then
            z.Move wdCharacter, 1
            ReDim Preserve Ends(i)
            Ends(i) = z.End
            i = i + 1
        End If
    Loop While z.InRange(ActiveDocument.Paragraphs(1).Range)
    MsgBox "There are " & i & " occurences of any of the characters in """ & strFindText & """ in the first paragraph."
End Sub

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question