Easy Word/Visual Basic question..

This is easy if it's possible and difficult if it's not.

I want to return to a variable the number of lines that are in a selection.

Say I have a selection that contains 5 lines of text.  I want..

---------------------------------
dim nip as integer

nip = selection.numoflines
-------------------------------

I don't believe "numoflines" is an actual option, but if there is a way to accomplish this same logical idea it'd be wonderful.  Also, is it possible to have a command return the line number that the cursor is on?

Thanks..

-rick detroit
saksafon2Asked:
Who is Participating?
 
raquel061097Connect With a Mentor Commented:
The following function should help. If nothing is selected then the Function should return 0. Otherwise it should tell you how many lines are selected. This is assuming that the return character is Chr(13,10). If not you may have to change vbCrLf to Chr(10) or Chr(13) depending on the text you are selecting.

Function FcnNumLines(strSelection As String) As Integer

    If IsNull(strSelection) Or strSelection = "" Then
        FcnNumLines = 0
        Exit Function
    End If

    Dim intLines As Integer, intS1 As Integer, intS2 As Integer
    intLines = 0
    intS1 = 1
    Do
        intLines = intLines + 1
        intS1 = InStr(intS1, strSelection, vbCrLf)
        If intS1 > Len(strSelection) Or intS1 = 0 Then Exit Do
        intS1 = intS1 + 1
    Loop
    FcnNumLines = intLines
End Function
0
 
MarineCommented:
i haven't done this myself. but if there is no method to do this you can probably use a counter to see how many lines have been selected by selection.
0
 
saksafon2Author Commented:
Use a counter.. I'm a novice, could you be a touch more specific?

Thanks!

rick
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
saksafon2Author Commented:
Oh!  And before someone plays be complete new, I know what a counter is.  I just don't know how to incorporate it into counting lines in a selection or document..

Thanks

rick
0
 
raquel061097Commented:
Oops. On my answer I added a variable intS2. Just ignore it, I forgot to erase it.
0
 
saksafon2Author Commented:
Wow!  Excellent.  I understand all of the code except for this line.. I read the help file a couple of times but I don't get why intS1 goes in the first spot and the carriage return is in third.. mayhaps you could ellaborate?

"intS1 = InStr(intS1, strSelection, vbCrLf) "
0
 
saksafon2Author Commented:
Adjusted points from 10 to 20
0
 
raquel061097Commented:
Sure. The format for InStr is InStr([start, ]string1, string2[, compare]).

intS1 is where within the string you want to start looking. Character #1, #2, etc. intS1 is increased by one each time vbCrLf is found so that it will look for the next occurance of vbCrLf. It intS1 were not increased it would see the exact same occurance over and over and the Loop would continue endlessly. Anyway, the string1 spot is the string to searched and string2 is the string to be found, that being vbCrLf. The [start, ] is optional so if you don't have it InStr just needs string1 and string2.
0
 
saksafon2Author Commented:
Function FcnNumLines(strSelection As String) As Integer

                       If IsNull(strSelection) Or strSelection = "" Then
                           FcnNumLines = 0
                           Exit Function
                       End If

                       Dim intLines As Integer, intS1 As Integer, intS2 As Integer
                       intLines = 0
                       intS1 = 1
                       Do
                           intLines = intLines + 1
                           intS1 = InStr(intS1, strSelection, vbCrLf)
                           If intS1 > Len(strSelection) Or intS1 = 0 Then Exit Do
                           intS1 = intS1 + 1
                       Loop
                       FcnNumLines = intLines
                   End Function
----------------------------------------------------------------
Hm.. I'm having a bit of trouble.  It all seems to make perfect sense to me, but the cpu is returning only the answer "1" every time.  I've attempted a debug and it constantly jumps out of the loop after the first pass.  I've flipped the operator > to < and it does the same thing.  Any ideas?

thanks

-rick
0
 
raquel061097Commented:
What kind of text are you trying to check? Is this in some VB program you are writing or in Word or some other Document?
0
 
saksafon2Author Commented:
Word.. I'm trying a couple of things.. I may have it.
I'll check in in a bit.

rick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.