?
Solved

Easy Word/Visual Basic question..

Posted on 2000-03-17
11
Medium Priority
?
173 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:saksafon2
  • 6
  • 4
11 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2629383
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
 

Author Comment

by:saksafon2
ID: 2629390
Use a counter.. I'm a novice, could you be a touch more specific?

Thanks!

rick
0
 

Author Comment

by:saksafon2
ID: 2629413
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Accepted Solution

by:
raquel061097 earned 80 total points
ID: 2629427
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
 

Expert Comment

by:raquel061097
ID: 2629434
Oops. On my answer I added a variable intS2. Just ignore it, I forgot to erase it.
0
 

Author Comment

by:saksafon2
ID: 2629456
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
 

Author Comment

by:saksafon2
ID: 2629458
Adjusted points from 10 to 20
0
 

Expert Comment

by:raquel061097
ID: 2629846
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
 

Author Comment

by:saksafon2
ID: 2636076
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
 

Expert Comment

by:raquel061097
ID: 2636291
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
 

Author Comment

by:saksafon2
ID: 2636348
Word.. I'm trying a couple of things.. I may have it.
I'll check in in a bit.

rick
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

830 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