none
Populating Word Table with data using Office RRS feed

  • Question

  • I want to export data in a word table. According to the MSDN link http://msdn.microsoft.com/en-us/library/aa537149(v=office.11).aspx, I am using  the ConvertToTable() method of Range object for better performance. My field value contains tab and newline characters. In the article a note says to place the content of each field within a pair of single or double quotes if the field or record delimiter could be part of the field data. But it's not working in my case.
    So please provide any example of how to make it work?
    Here is my code:
     private Word.Table BuildWordTable(ref Word.Range wdRng, IList<ItemExportView> items)
        {
          object separatorChar = "\t";
          object separator = Word.WdTableFieldSeparator.wdSeparateByTabs;
          object NumColumns = 2;
          // object numberOfRows = items.Count + 1;
    
          StringBuilder dataString = new StringBuilder();
    
          // Fill the column headings.      
          {        
            dataString.AppendFormat("{0}", "Image Name");
    
            // Append a field delimiter.
            dataString.Append(separatorChar);
            dataString.AppendFormat("{0}", "Content");
    
            //append a record delimiter
            dataString.Append("\n");
    
          } // end for column headings
    
          //Set data rows
          for (int i = 0; i < items.Count; i++)
          {
            dataString.AppendFormat("{0}", items[i].ImageName);
    
            // Append a field delimiter.
            dataString.Append(separatorChar); 
            dataString.AppendFormat("{0}", items[i].Content);        
    
            //append a record delimiter
            dataString.Append("\n");
          }
          
    
          wdRng.Text = dataString.ToString();      
    
          object objDefaultBehaviorWord8 = Word.WdDefaultTableBehavior.wdWord8TableBehavior;
          object objAutoFitFixed = Word.WdAutoFitBehavior.wdAutoFitFixed;
    
          Word.Table wTable = wdRng.ConvertToTable(ref separator, ref missing,
                              ref NumColumns, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing,
                              ref objAutoFitFixed, ref objDefaultBehaviorWord8);
          return wTable;
        } 
    
     
    Wednesday, June 29, 2011 5:16 PM

Answers

  • In that case I think you will have to

     a. change every newline in your data to some other character (perhaps \u2346 ?) before inserting the data

     b. convert text to table

     c. change the substitute characters back to newlines, e.g. using Find/Replace.

    If that approach isn't working for you, a completely different approach that might work better for you might be generate the WordML (XML) for your entire table and insert it using wdRng.InsertXML . However, that's another story that I'd rather not get into right now.

     


    Peter Jamieson
    Thursday, June 30, 2011 11:20 PM

All replies

  • <<

    In the article a note says to place the content of each field within a pair of single or double quotes if the field or record delimiter could be part of the field data.

    >>

    AFAIK the article is completely wrong on this point and is probably confusing this situation with the one where you create a table using (say) InsertDatabase (which you could only really consider if your data is already in a disk file or you can write such a file)

    I think the only thing you can do here is to set separatorChar to a value that cannot possibly occur in your data - the following arbitrary choice worked for me, but it may have to be a character code that actually exists in the font of the text being inserted:

       object separatorChar = "\u2345";
    

    then pass that object as the first parameter of the COnvertToTable call:

       Word.Table wTable = wdRng.ConvertToTable(ref separatorChar, ref missing,
                 ref NumColumns, ref missing, ref missing, ref missing,
                 ref missing, ref missing, ref missing, ref missing,
                 ref missing, ref missing, ref missing, ref missing,
                 ref objAutoFitFixed, ref objDefaultBehaviorWord8);
    




    Peter Jamieson
    Wednesday, June 29, 2011 10:25 PM
  • Thanks for your reply.

    I have used the given char code and its working fine for field value having tab character; But my field value contains newline also, so it splits any filed value whenerve it found any newline char.

    Any suggestion about this?

    Thursday, June 30, 2011 6:33 PM
  • In that case I think you will have to

     a. change every newline in your data to some other character (perhaps \u2346 ?) before inserting the data

     b. convert text to table

     c. change the substitute characters back to newlines, e.g. using Find/Replace.

    If that approach isn't working for you, a completely different approach that might work better for you might be generate the WordML (XML) for your entire table and insert it using wdRng.InsertXML . However, that's another story that I'd rather not get into right now.

     


    Peter Jamieson
    Thursday, June 30, 2011 11:20 PM