Adding Line Numbers to a selection

W

Webtechie

Hello,

I am a developer. In my new company, I need to include my code in
documentation. So I create my documentation in Word and bring in code (from
another application) into a text box. Great.

Now they want line numbers.

So I paste the code into a blank page and go to line numbers. It changes
the indentation I had in my coding when it adds the numbers.

I am trying to write a macro to insert numbers, but can't seem to find how
to find the first row of a selection and the last row of a selection and just
loop through and insert a counter?!! I find many examples for a table, but
this is not a table. Not to mention the code for tables doesn't work for a
selection.

Example:

If CX_BUTTON2 = "T" Then
&GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_BUDG);
&GRIDLEVEL1.HIDEALLROWS();
&GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_TBL);
&GRIDLEVEL1.SHOWALLROWS();
Else
&GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_TBL);
&GRIDLEVEL1.HIDEALLROWS();
&GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_BUDG);
&GRIDLEVEL1.SHOWALLROWS();
End-If;

The above becomes

1. If CX_BUTTON2 = "T" Then
2. &GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_BUDG);
3. &GRIDLEVEL1.HIDEALLROWS();
4. &GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_TBL);
5. &GRIDLEVEL1.SHOWALLROWS();
6. Else
7. &GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_TBL);
8. &GRIDLEVEL1.HIDEALLROWS();
9. &GRIDLEVEL1 = GetRowset(Record.COMBO_DATA_BUDG);
10. &GRIDLEVEL1.SHOWALLROWS();
11. End-If;


Not the same indentation. Can someone please tell me the code to just find
the first line of a selection and the last line of selection, so I can
iterate through the selection and put in line numbers?

Thanks.

Tony
 
H

Helmut Weber

Hi Tony,

stay away form autonumbering.
Do it yourself.

Lets call it basic homemade indentation
with a grid of 3:

Sub test9003()
Dim oPrg As Paragraph
Dim l As Long ' the paragraph number
Dim s As Long ' the number of spaces leading, if there are any
For Each oPrg In selection.Range.Paragraphs
l = l + 1
s = 0
If oPrg.Range.Characters(1) = " " Then
s = s + 1
While oPrg.Range.Characters(s + 1) = " "
s = s + 1
Wend
oPrg.Range.InsertBefore Format(l, "000") & String(s, " ")
Else
oPrg.Range.InsertBefore Format(l, "000") & String(3, " ")
End If
Next
End Sub

Depends on the regularity of your data, of course.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
W

Webtechie

Danke Shurn fur seine helfen!!

Thanks man, but isn't this counting the paragraphs in a selection? I need
to count the lines (or rows) in a selection. And how would I move down from
one line of code to thenext line of code?

It seems as if your code would go from paragraph to paragraph in a selection
and not line by line.

Thanks again.
 
H

Helmut Weber

Hi Tony,

too often people, not to familiar with Word's object model,
if I may say so, don't distinguish between paragraphs and lines.
So, to avoid the ubiquitous question:
"Do you mean lines or paragraphs?"
I took a long shot
and assumed what was meant are paragraphs. ;-)

I don't have the time right now to rewrite it to lines,
but certainly doable.

Maybe some of the friends here might jump in.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
W

Webtechie

Helmut,

I appreciate your help.

I originally stated:

1) I am a developer
2) I need line numbers for my code

So I hope I wasn't one of those folks you were talking about. :)

In any event, now that I know you were talking about paragraphs and that
there is code for lines, I am off and running.

You answered my question. I can rewrite your code for lines myself now that
I know what you meant.

Thanks.

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top