Solved

Easy Word/Visual Basic question..

Posted on 2000-03-17
11
165 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

706 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

21 Experts available now in Live!

Get 1:1 Help Now