Solved

trying to retrieve the end range of a range object

Posted on 2007-11-25
9
238 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

930 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