Solved

trying to retrieve the end range of a range object

Posted on 2007-11-25
9
237 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
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.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now