none
Microsoft.Office.Interop.PowerPoint - edit table cells RRS feed

  • Question

  • Hello,

    I am developing an application that should be able to gather data and provide some kind of report into a powerpoint presentation. Therefore I would like to have some template tables that would be edited and modified at runtime. The problem I have with my current implementation is that it crashes when trying to access the TextRange corresponding to each cell and therefore I cannot read or write the table data. The piece of code I use is:

    using IPowerpoint = Microsoft.Office.Interop.PowerPoint;
    //...
    if (pow.Shape.Type == MsoShapeType.msoTable)
    {
      IPowerpoint.Table table = pow.Shape.Table;
      IPowerpoint.Rows rows = table.Rows;
      for (int r = 1; r <= rows.Count; r++)
      {
        IPowerpoint.Row row = rows[r];
        IPowerpoint.CellRange cells = row.Cells;
        for (int c = 1; c <= cells.Count; c++)
        {
          IPowerpoint.Cell cell = table.Cell(r, c);
          IPowerpoint.Shape shape = cell.Shape;
          IPowerpoint.TextFrame txFrm =  pow.Shape.TextFrame;
    //      if (txFrm.HasText == MsoTriState.msoTrue)
    //      {
            IPowerpoint.TextRange txRng = txFrm.TextRange;
            txRng.Text = "cell[" + r.ToString() + "," + c.ToString() + "]";
            Marshal.ReleaseComObject(txRng);
    //      }
          Marshal.ReleaseComObject(txFrm);
          Marshal.ReleaseComObject(shape);
          Marshal.ReleaseComObject(cell);
        }
        Marshal.ReleaseComObject(cells);
        Marshal.ReleaseComObject(row);
      }
      Marshal.ReleaseComObject(rows);
      Marshal.ReleaseComObject(table);
    }

    I have been debugging this a little bit and the problem appears at the line:

    IPowerpoint.TextRange txRng = txFrm.TextRange;

    More exactly, the TextRange property if the TextFrame is null. This happens for all cells of all tables, even if they contain text.

    Is this the expected behaviour or am I doing something wrong with my implementation?

    I am using Office 2007 and .NET 3.5

    Thanks in advance,

    Valentin

    Monday, November 4, 2013 4:41 PM

Answers

  • OK I found my mistake:

     IPowerpoint.TextFrame txFrm = pow.Shape.TextFrame;

    This actually accesses the currently selected shape of the slide, and not the shape inside the cell. It should be:

     IPowerpoint.TextFrame txFrm = cell.Shape.TextFrame;

    which works fine

    • Marked as answer by vali11 Tuesday, November 5, 2013 5:03 PM
    Tuesday, November 5, 2013 5:03 PM

All replies

  • In addition, I am also trying to add a completely new table to one of the slides. I add the table, then loop over all cells and try to add text to each cell. But this fails with the exact same error as above. Any ideas on how to fix this?

    This is the code example:

    using IPowerpoint = Microsoft.Office.Interop.PowerPoint;
    //...
    table = pow.Shape.Table;
    for (int r = 1; r <= rows; r++)
    {
      for (int c = 1; c <= cols; c++)
      {
      IPowerpoint.Cell cell = table.Cell(r, c);
      IPowerpoint.TextFrame txFrm = pow.Shape.TextFrame;
      //if (txFrm.HasText == MsoTriState.msoTrue)
      //{
      IPowerpoint.TextRange txRng = txFrm.TextRange;
      txRng.Text = "cell[" + r.ToString() + "," + c.ToString() + "]";
      Marshal.ReleaseComObject(txRng);
      //}
      Marshal.ReleaseComObject(txFrm);
      }
    }
    Marshal.ReleaseComObject(table);

    Tuesday, November 5, 2013 3:27 PM
  • OK I found my mistake:

     IPowerpoint.TextFrame txFrm = pow.Shape.TextFrame;

    This actually accesses the currently selected shape of the slide, and not the shape inside the cell. It should be:

     IPowerpoint.TextFrame txFrm = cell.Shape.TextFrame;

    which works fine

    • Marked as answer by vali11 Tuesday, November 5, 2013 5:03 PM
    Tuesday, November 5, 2013 5:03 PM
  • Hi,

    Yes,shape.TextFrame should under  the cell

    IPowerpoint.Cell cell = table.Cell(r, c);
    IPowerpoint.Shape shape = cell.Shape;
    IPowerpoint.TextFrame txFrm =  Shape.TextFrame;
    

    Wednesday, November 6, 2013 2:11 AM