Solved

trying to retrieve the end range of a range object

Posted on 2007-11-25
9
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
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 …

751 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