Solved

Easy Word/Visual Basic question..

Posted on 2000-03-17
11
169 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
raquel061097 earned 20 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 68
Interview questions for support of a legacy ASP.NET site 4 72
Excel Automation VBA 19 104
C# LINQ ForEach() question 6 87
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

733 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