PDA

View Full Version : Adding Line numbers in Word



Mister C
06-22-2004, 10:49 PM
Hello All,

Currently on another project-will answer those AP questions later this week. :D

Have a problem.

In VS .NET I can add line numbers to the code, but when I copy and paste this code with line numbers into Word, the line numbers are not there.

How can I keep these line numbers to stay in Word?

PS. I know you can suppress line numbers/add line numbers in Word itself. I tried it via help. It is not easy to do, especially with multiple places in the document to add line numbers to.

Thoughts appreciated.

Also, will let you know about the little project I am working on at a later date.

Mr. C. :p

ober
06-23-2004, 06:08 AM
Try pasting it into notepad or wordpad first. Those programs won't try to manipulate your text before showing it to you.

anon29
06-23-2004, 06:40 AM
Hello, this post was made with a 3rd party application for testing reasons.

Thantos
06-23-2004, 06:57 AM
Are you sure that when you copy from VS.NET it is actually copying the line numbers? To me it would make more sense for the line numbers to be added by the IDE and not really be text. But of course I don't have VS.NET so I don't know for sure :)

Mister C
06-23-2004, 07:26 AM
Are you sure that when you copy from VS.NET it is actually copying the line numbers? To me it would make more sense for the line numbers to be added by the IDE and not really be text. But of course I don't have VS.NET so I don't know for sure

That what it is doing. It is there for printing purposes, not to the text. I guess I will have to add the line numbers in word. :(

Thantos
06-23-2004, 07:41 AM
Does VS.NET have a print to file option? And could word open that file? Just throw out ideas ;)

Prelude
06-23-2004, 08:38 AM
>I guess I will have to add the line numbers in word.
And you call yourself a programmer. :D Write a script to do it for you. ;)


#!/usr/bin/perl
#
# nladd
# Add line numbers to a text file

use warnings;
use strict;

open LINES_FILE, ">lines" or die;

my $line_count = 1;

while (<ARGV>) {
print LINES_FILE "$line_count $_";
$line_count++;
}

ober
06-23-2004, 09:03 AM
Wouldn't that be techically screwed up if a certain line wraps? Unless when you copy it from the IDE, it carries along any new line characters.

DavidP
06-23-2004, 09:08 AM
or with PHP:



<?

$a_lines = file("sourcefile.cpp");
$line_count = count($a_lines);

$fp = fopen('sourcefile.cpp', 'w');

for ( $i = 0; $i < $line_count; $i++ )
{
$content = "$i\t$a_lines[$i]";
fwrite($fp, $content);
}

fclose($fp);

?>

Prelude
06-23-2004, 09:10 AM
>Wouldn't that be techically screwed up if a certain line wraps?
Not enough to warrant a fix for the simple use the script was designed for. ;)

Salem
06-23-2004, 01:43 PM
@Mister C
Why don't you write yourself a nice little VBA macro in word, which you pass to it the name of the .c source file you want to quote from, and the range of lines you want to import, and it does all the hard work for you.

Even better, why not add magic comments to the 'C' source code so you don't have to refer to line numbers at all, but can refer instead to named sections of the code.

Perspective
06-23-2004, 06:30 PM
first get a real operating system :p , then do this



grep -n .\* in-file > out-file

linuxdude
06-23-2004, 10:13 PM
behold the many powers of grep:D. Don't you just love linux?

anonytmouse
06-23-2004, 10:55 PM
Here is the result of some procrastination:


Sub ImportCode()
Dim xlApp As Object, fso As Object, tstream As Object
Dim nStart As Integer, nEnd As Integer, nLine As Integer
Dim strLine As String

Set xlApp = CreateObject("Excel.Application")
strFile = xlApp.GetOpenFilename("C/C++ Source Files (*.c; *.cpp),*.c;*.cpp")
xlApp.Quit

If (strFile = False) Then Exit Sub
nStart = CInt(InputBox("Please enter the start line.", "Start", 0))
nEnd = CInt(InputBox("Please enter the end line.", "End", 999))

Set fso = CreateObject("Scripting.FileSystemObject")
Set tstream = fso.OpenTextFile(strFile)

While (Not tstream.AtEndOfStream)
strLine = tstream.ReadLine

If (nLine >= nStart And nLine <= nEnd) Then
Selection.Font.Color = wdColorBlueGray
Selection.TypeText Format(nLine, "#000")
Selection.Font.Color = wdColorAutomatic
Selection.TypeText " " & strLine & vbCrLf
End If

nLine = nLine + 1
Wend

End Sub

Paste it into the VBA project and have fun.

Ken Fitlike
06-24-2004, 05:15 AM
first get a real operating system :p , then do this



grep -n .\* in-file > out-file
That'll work in windows, too - with msys (http://www.mingw.org/msys.shtml) (includes a port of grep among others). :)

DavidP
06-24-2004, 06:59 AM
well looks like there have been 4 different methods posted...

Perl, PHP, grep, and VB...

now go at it!

Perspective
06-24-2004, 10:12 AM
well looks like there have been 4 different methods posted...

Perl, PHP, grep, and VB...

now go at it!

hmmm, interesting that 4 non-C/C++ solutions were posted at the c-board. :eek:

DavidP
06-24-2004, 10:29 AM
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main ( void )
{
std::ifstream load ( "sourcefile.cpp" );

std::vector < std::string > lines;
std::string tempStr;

while ( !load.eof() )
{
getline(load, tempStr);
lines.push_back(tempStr);
}

load.close();

std::ofstream save ( "sourcefile.cpp" );

for ( int k = 0; k < lines.size(); k++ )
{
save << (k + 1) << "\t" <<
lines[k] << "\n";
}

save.close();

return 0;
}


Solves that problem...

ober
06-24-2004, 10:56 AM
hmmm, interesting that 4 non-C/C++ solutions were posted at the c-board. :eek:Hmm.. does that tell you anything about the worthiness of C/C++? ;)

Jeremy G
06-24-2004, 11:11 AM
Hmm.. does that tell you anything about the worthiness of C/C++? ;)
It's so pure that to be spoken in this thread would dishonor it - and the speaker?

Salem
06-24-2004, 01:01 PM
> Hmm.. does that tell you anything about the worthiness of C/C++?
It tells you that many people have a lot more tools in the toolbox than some one-dimensional being who swears 'x' is best for everything.

DavidP
06-24-2004, 01:04 PM
Salem is kickin'

(according to his reputation bars)

....

Meanwhile Prelude exudes kindness and good coding practice while Thantos apparently refers to himself as being special.

Thantos
06-24-2004, 01:26 PM
Just for the record I never rode ANY school buses!

ober
06-24-2004, 02:01 PM
RIGHT. Just short buses, huh? ;)

swoopy
06-24-2004, 08:45 PM
>Just for the record I never rode ANY school buses!

You poor soul. I'm sorry you missed out on the adventure.