Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

trying to retrieve the end range of a range object

Posted on 2007-11-25
9
Medium Priority
?
245 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Progress
Suggested Courses

705 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