none
Insert newline or cariage return in word 2007

    Question

  • Hi All,

    I have written an application in c# that reads data from an xml file, parses the data and writes it to a word document.
    Now I would like to be able to put in cariage return or newlines in the xml, and have the application insert them when found in the xml.

    I have tried putting \n \r or \f in the text in the xml, but the characters are just written, and not interpreted like CR or newline control characters.
    Anyone know how I coul do this?

     
    "Everyone is an expert at something" Kim Oppalfens Configmgr expert for lack of any other expertise. http://www.scug.be/blogs/sccm
    Friday, February 20, 2009 2:11 PM

Answers

  • Hi Kim

    It would help if you could show us the code that handles the "writes it to a Word document" part.

    If you pass a text string in C# to Word, like this: 

        Document.Content.Text = "abc\rdef";

    you should get a carriage return character in Word. Substituting \n ought to do the same. I can think of no reason why this shouldn't work, but perhaps you're trying to

    VB languages have to do this differently, by passing the character symbol:

        Document.Content.Text = "abc" & Chr(13) & "def"

    Perhaps more efficient would be to transform your XML to WordProcessingML or WordOpenXML and insert that into the document. (Or, if you're creating entire documents, write the document in memory using the Office 2007 OpenXML file format.) These file formats use XML elements to generate paragraphs (<w:p>). You'll find more information on the file formats and how to generate OpenXML files at OpenXMLDeveloper.org
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Tim Li Friday, February 27, 2009 3:21 AM
    Saturday, February 21, 2009 7:58 AM

All replies

  • Hi Kim

    It would help if you could show us the code that handles the "writes it to a Word document" part.

    If you pass a text string in C# to Word, like this: 

        Document.Content.Text = "abc\rdef";

    you should get a carriage return character in Word. Substituting \n ought to do the same. I can think of no reason why this shouldn't work, but perhaps you're trying to

    VB languages have to do this differently, by passing the character symbol:

        Document.Content.Text = "abc" & Chr(13) & "def"

    Perhaps more efficient would be to transform your XML to WordProcessingML or WordOpenXML and insert that into the document. (Or, if you're creating entire documents, write the document in memory using the Office 2007 OpenXML file format.) These file formats use XML elements to generate paragraphs (<w:p>). You'll find more information on the file formats and how to generate OpenXML files at OpenXMLDeveloper.org
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Tim Li Friday, February 27, 2009 3:21 AM
    Saturday, February 21, 2009 7:58 AM
  • The code that writes to the word document is this:

    Object oMissing = System.Reflection.Missing.Value;
    oPara1 = oWordDoc.Content.Paragraphs.Add(ref oMissing);
    oPara1.Range.Text = TextToWrite;

    TextToWrite is a string that is read from an xml input file, if I add \r or \n for that matter in the input xml file it just prints the characters \r or \n it doesn't insert a newline, like I hoped it would do.


    "Everyone is an expert at something" Kim Oppalfens Configmgr expert for lack of any other expertise. http://www.scug.be/blogs/sccm
    Tuesday, March 17, 2009 9:32 AM
  • Kim Oppalfens [MVP] said:

    The code that writes to the word document is this:

    Object oMissing = System.Reflection.Missing.Value;
    oPara1 = oWordDoc.Content.Paragraphs.Add(ref oMissing);
    oPara1.Range.Text = TextToWrite;

    TextToWrite is a string that is read from an xml input file, if I add \r or \n for that matter in the input xml file it just prints the characters \r or \n it doesn't insert a newline, like I hoped it would do.



    Mmm, well, there must be something missing in your problem description, because the following works just fine for me:

                Word.Document oWordDoc = Globals.ThisAddIn.app.ActiveDocument;  
                Word.Paragraph oPara1 = null;  
                string TextToWrite = "Testing writing\rcarriage returns";  
                //Write a carriage return  
                Object oMissing = System.Reflection.Missing.Value;  
                oPara1 = oWordDoc.Content.Paragraphs.Add(ref oMissing);  
                oPara1.Range.Text = TextToWrite;  
     

    Note that this is a VSTO forum, so my test environment is a VSTO add-in. But that shouldn't make any difference whatsoever as far as setting the text in the Word object model goes. It might be interesting to see exactly what's in your TextToWrite variable.
    Cindy Meister, VSTO/Word MVP
    Tuesday, March 17, 2009 9:48 AM
  • ContentControl.Range.Text = "asasa \r asasa";
    

    This code doesn`t work. It generates the error " One of the values passed to this method or property is incorrect." .If I remove the "\r" character, it works. I tries Char(10), Char(13) and the result was the same error.

    The ContentControls I tried are all ( richtextbox, plaintextbox, buildingblock).

    I am using VSTO 3.0 => VS 2008 => Office 2007=> Microsoft Word.

     

    Best regards,

    Silviu.

    Tuesday, January 11, 2011 1:00 PM
  •  

    ContentControl.Range.Text = "asasa \r asasa";
    
    

     

    This code doesn`t work. It generates the error " One of the values passed to this method or property is incorrect." .If I remove the "\r" character, it works. I tries Char(10), Char(13) and the result was the same error.

    The ContentControls I tried are all ( richtextbox, plaintextbox, buildingblock).

    I am using VSTO 3.0 => VS 2008 => Office 2007=> Microsoft Word.


    Hi Silviu

    Two things would cause this:

    1. With a plain text box, the option to allow line breaks is not activated

    2. With any kind of content control: The content control is located in-line in a paragraph with other text. For example, the following cannot work:

      some text: <content control text with a carriage return or new line break>

    The content control must be in its own paragraph in order to allow carriage returns or line breaks. Test it as an end-user: pressing Enter won't create a new line.

    If you need to layout the document similar to the above, and want new lines, put the "some text" in the first cell of a table and the content control in a second cell.


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 11, 2011 4:29 PM
  • Try Char(11) in plain text box, it should work. That's exactly what Word uses when you manually type Shift + Enter inside a plain text box content control. Its the vertical tab character and so closely mimics a newline though it's not actually a newline.
    Tuesday, August 28, 2012 4:56 PM