none
writing line by line RRS feed

  • Question

  • What I would like to do is write text to a word doc line by line not in "paragraphs". 

    for example:

    |------------------------------------------------------------|
    |                                                 Hello                   |
    |World                                                                   |
    |                                                                           |
    |                                                                           |
    |                           !!                                              |
    |                                                                           |

    lets pretend the value of my variable is a whole bunch of spaces then Hello for that first line, for the second line it's "World" and that's it, next 2 lines are just "" and "", and lastly that final line is a bunch of space and !!

    I need to be able to transfer something along those lines to the word doc and I can't seem to find much out there that doesn't have you doing a block of text at a time.  I've tried  

     

    'it prints to the document, not right but it prints.

    thanks in advance...


     

     

    I'll be ok... I'm using VB.Net 2010 and Word 2000... sorry it's been a long day

     

    wrdDoc.Range.InsertAfter(strLine)

    • Edited by temlehdrol Monday, April 18, 2011 7:11 PM forgot system info
    Monday, April 18, 2011 7:06 PM

Answers

  • Use Chr(11) to insert the line feeds

    wdDoc.Range.InsertAfter "Line1" & Chr(11) & "Line2"


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "temlehdrol" wrote in message news:ad75904d-891a-4761-a878-121d21667fd1@communitybridge.codeplex.com...

    I tried to edit the post but that doesn't seem to be work that well... as I said it's been a long day. Anyways I tried to use:


    wrdDoc.Range.InsertAfter(strLine) 'it prints to the document, not right but it prints.

    and when I do that I get one continious stream of text...


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by temlehdrol Tuesday, April 19, 2011 11:55 AM
    • Unmarked as answer by temlehdrol Tuesday, April 19, 2011 1:21 PM
    • Marked as answer by temlehdrol Tuesday, April 19, 2011 1:44 PM
    Monday, April 18, 2011 8:00 PM

All replies

  • I tried to edit the post but that doesn't seem to be work that well... as I said it's been a long day.  Anyways I tried to use:
    wrdDoc.Range.InsertAfter(strLine) 'it prints to the document, not right but it prints.
    

    and when I do that I get one continious stream of text...

    Monday, April 18, 2011 7:13 PM
  • Use Chr(11) to insert the line feeds

    wdDoc.Range.InsertAfter "Line1" & Chr(11) & "Line2"


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "temlehdrol" wrote in message news:ad75904d-891a-4761-a878-121d21667fd1@communitybridge.codeplex.com...

    I tried to edit the post but that doesn't seem to be work that well... as I said it's been a long day. Anyways I tried to use:


    wrdDoc.Range.InsertAfter(strLine) 'it prints to the document, not right but it prints.

    and when I do that I get one continious stream of text...


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by temlehdrol Tuesday, April 19, 2011 11:55 AM
    • Unmarked as answer by temlehdrol Tuesday, April 19, 2011 1:21 PM
    • Marked as answer by temlehdrol Tuesday, April 19, 2011 1:44 PM
    Monday, April 18, 2011 8:00 PM
  • So if I were to do this in a loop I could do:

     

    wdDoc.Range.InsertAfter strLine & Chr(11) 
    

     

    And that should give me the line with a line feed thus forcing the next bit of text to the following line, I'll give that a shot and see what happens.

    Tuesday, April 19, 2011 11:09 AM
  • ok slight change but it seems to work...

    wdDoc.Range.InsertAfter(strLine & Chr(11))

    Now I just need to work on the formatting, but I think that will give me a really good start, thank you!

    Tuesday, April 19, 2011 11:55 AM
  • Actually I realized I was doing this wrong and it is in fact not working... this is part of what I have for code...

    If c = "f" Then
                'Get the font, size and line spacing from the line
                strLine = strLine.Substring(2)
                strLine = strLine.Trim("%")
                fontName = strLine.Split(",").GetValue(0)
                fontSize = strLine.Split(",").GetValue(1)
                linespace = strLine.Split(",").GetValue(2)
    
                If linespace > 0 Then wrdSelection.ParagraphFormat.LineSpacing = linespace
    
                With wrdSelection.Font
                  If (Len(fontName) > 0) Then .Name = fontName
                  If (fontSize > 0) Then .Size = fontSize
                End With
                strLine = " "
              ElseIf c = "m" Then
                'Start of a new page, margins and overlay files are still the same so no need to change
                'clear the text from the line
                strLine = " "
              ElseIf c = "b" Then
                boldState = Not boldState
                wrdSelection.Font.Bold = boldState 'wdToggle
                strline1 = strLine.Substring(2, strLine.LastIndexOf("%") - 2)
                strLine2 = strLine.Substring(strLine.LastIndexOf("%") + 2)
                'wrdDoc.Range.InsertAfter(strline1)
                wrdSelection.Range.InsertAfter(strline1)
                boldState = Not boldState
                wrdSelection.Font.Bold = boldState
                'wrdDoc.Range.InsertAfter(strLine2 + Chr(11))
                wrdSelection.Range.InsertAfter(strLine2 + Chr(11))
                bolSkipLine = True
    
    ' some other formatting with If's ......
    
    If bolSkipLine = False Then
                'wrdDoc.Range.InsertAfter(strLine + Chr(11))
                wrdSelection.Range.InsertAfter(strLine + Chr(11))
              End If
    

    you can ignore the formatting, that is another issue, however when I do this, the document is writing backwards and not correctally at that.  Now when I say backwards I mean line 1 = last line and so forth.  I have also tried InsertBefore thinking maybe I had that backwards and still no luck.  So instead of being:
    Dear Mr, So and So,
       Thank you for something

    it is:
       Thank you for something
    Dear Mr, So and So,

    Tuesday, April 19, 2011 1:28 PM
  • ok made some more changes and it seems to be right now... if I change it to be

    wrdSelection.InsertAfter(strLine2 + Chr(11))

    it does put it in the right order, though I'm not getting any text formatting but that's another issue.

    Tuesday, April 19, 2011 1:44 PM