user1587834
user1587834

Reputation: 43

Openxml: using a custom property add a new line

I'm using the OpenXML Library (in C#) to pull data out of MS CRM 2013 and insert it into a word template using CustomDocumentProperty, everything works fine until I try inserting text from a multiline textbox, the text gets pulled in fine but the output in Word is all on one line. The string pulled out of CRM looks like "This\nIs\nA\nTest\n\Multiline\nComment", I've tried replacing \n with \r\n, and Environment.NewLine but I get same result. Does anybody know how it is possible to insert a new line in a CustomDocumentProperty? I'm constructing the new property like so:

var stringValue = "This\nIs\nA\nTest\n\Multiline\nComment";

if (stringValue != null && stringValue.Contains("\n"))
{
    stringValue = stringValue.Replace("\n", "\r\n");
}

var newProp = new CustomDocumentProperty
{
    VTLPWSTR = new VTLPWSTR(stringValue),
    Name = ((CustomDocumentProperty)mergeField).Name,
    FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
};

Thanks for any help.

Upvotes: 2

Views: 1213

Answers (1)

Varun Rathore
Varun Rathore

Reputation: 7898

Newline and tab characters don't work in Microsoft Office.

You'll have to create a new Paragraph element every time you encounter a \n character.

<w:p>
    <w:r>
        <w:t> Hi,
        </w:t>
    </w:r>
</w:p>
<w:p>
    <w:r>
        <w:t> I'm Varun
        </w:t>
    </w:r>
</w:p>

this what your XML should look like and I'd Suggest you use the Open XML SDK 2.5 Productivity tool so that you're able to work with the XML better.

Upvotes: 1

Related Questions