Adding Line numbers in Word

This is a discussion on Adding Line numbers in Word within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Hello All, Currently on another project-will answer those AP questions later this week. Have a problem. In VS .NET I ...

  1. #1
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Adding Line numbers in Word

    Hello All,

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

    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.
    Mr. C: Author and Instructor

  2. #2
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    Try pasting it into notepad or wordpad first. Those programs won't try to manipulate your text before showing it to you.

  3. #3
    Registered User anon29's Avatar
    Join Date
    Jun 2004
    Posts
    1

    Post title

    Hello, this post was made with a 3rd party application for testing reasons.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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.
    Mr. C: Author and Instructor

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Does VS.NET have a print to file option? And could word open that file? Just throw out ideas

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I guess I will have to add the line numbers in word.
    And you call yourself a programmer. Write a script to do it for you.
    Code:
    #!/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++;
    }
    My best code is written with the delete key.

  8. #8
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    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.

  9. #9
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    or with PHP:

    Code:
    <?
    
    $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);
    
    ?>
    My Website

    "Circular logic is good because it is."

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    @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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    first get a real operating system , then do this

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

  13. #13
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    behold the many powers of grep. Don't you just love linux?
    Last edited by linuxdude; 06-23-2004 at 11:19 PM.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is the result of some procrastination:
    Code:
    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.

  15. #15
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Perspective
    first get a real operating system , then do this

    Code:
    grep -n .\* in-file > out-file
    That'll work in windows, too - with msys (includes a port of grep among others).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  3. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 01:22 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21