Using ConvertToText to create tables containing cells with embedded new line characters

M

Mystery Man

We use Word 2000 and 2003 automation from C# (via interop). We create
tables dynamically. However, we now need to have embedded returns
within a cell.

For example:-

Row 1: Date Text
Row 2: 1/1/2002 Help me
please
Row 3: 1/1/2004 Help me

Sample code is as follows:-

object rows = 3;
object columns = 3;

string text = "Date\tText\t1/1/2002\tHelp me\nplease\t1/1/2004\tHelp
me";

Word.Range range = _wordApp.Selection.Range;
range.Text = tableValue.ToString();

Word.Table newTable = range.ConvertToTable(ref sep // Separator
,ref numRows // NumRows
,ref numColumns // NumColumns
,ref oMissing // InitialColumnWidth
,ref oMissing // Format
,ref oMissing // ApplyBorders
,ref oMissing // ApplyShading
,ref oMissing // ApplyFont
,ref oMissing // ApplyColor
,ref oMissing // ApplyHeadingRows
,ref oMissing // ApplyLastRow
,ref oMissing // ApplyFirstColumn
,ref oMissing // ApplyLastColumn
,ref oMissing // AutoFit
,ref oMissing // AutoFitBehaviour
,ref oMissing // DefaultTableBehaviour
);


However, what this does is create a new row when it encounters the \n.
I have tried using different column separators and a few other
possibilities but to no avail.
 
K

Klaus Linke

Hi mystery man,

You probably could wait until you have created the table, and only then change \n into paragraph marks.

I guess currently \n is changed into vbCr in the function .ToString?
You could either take the conversion of "\n" out of that function, or, if you don't want to change it because it's used elsewhere, immediately replace vbCr back into some placeholder ("\n" or whatever).

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top