[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Easy Word/Visual Basic question..

Posted on 2000-03-17
Medium Priority
172 Views
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
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
• 6
• 4

LVL 6

Expert Comment

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

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

Thanks!

rick
0

Author Comment

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

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

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

Author Comment

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

ID: 2629458
Adjusted points from 10 to 20
0

Expert Comment

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

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

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

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

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asseâ€¦
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and oâ€¦
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â€¦
###### Suggested Courses
Course of the Month13 days, 9 hours left to enroll