none
Issue to add a table to the slide RRS feed

  • Question

  • Hi there, I add a table to the slide with the code "slide.Shapes.AddTable(1, 1, 15, 80, 60, 20)". there is no error and the table is created. but when I want to set the text with the code "shape.TextFrame.TextRange.Text ="111"", I get the error that is "The specified value is out of range." If I change the shape to a textbox, that is slide.Shapes.AddTextBox(1,15,70,60,20), and change the text with "shape.TextFrame.TextRange.Text = "111"", it's no error. Any one can help me ?

    Monday, December 27, 2010 7:23 AM

Answers

  • Hi Zhu

    You might want to ask this of the application specialists in the PowerPoint forums on "Answers" (http://answers.microsoft.com/en-us/default.aspx). They usually know the best way to handle the object model.

    But from what I can see, the best you can do is to loop the ROWS of a table and set the Cells.Borders on each row. Pseudocode:

    foreach (PowerPoint.Row in pptApp.Slides[index].Tables[index].Rows)
    {
        rw.Borders[PowerPoint.PpBorderType.ppBorderBottom].Visible = -1
    }

    Most efficient, however, would be to apply a table STYLE with your preferred formatting. To see what I mean, open the presentation as an end-user and click in a table. In the Ribbon, choose the tab Table Tools/Design. Click on the bottommost arrow of the Table Styles gallery to see all available table styles associated with the current (default) theme. I see one for example that, when I hover the mouse over it, says "No style, Table Grid".

    Select the style that looks like what you want.

    Press Alt+F9 to open the Office VBA Editor, then Ctrl+G for the Immediate Window. Type the following (subsituting the appropriate number for each "indexNrOf"):
    ?ActivePresentation.Slides(indexNrOfSlide).Shapes(indexNrOfShape).Table.Style.ID

    Press Enter and you should see the GUID for the table style in the next line. Note this number.

    In your application, add a line of code to use the ApplyStyle method with this GUID. In VB:
    Table.ApplyStyle("{5940675A-B579-460E-94D1-54222C63F5DA}", True)

    Note that it's also possible to provide a custom style, via the Thema attached to the presentation, but that goes beyond the scope of this forum.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Monday, January 3, 2011 10:00 AM
    Thursday, December 30, 2010 8:56 AM
    Moderator

All replies

  • Hi Recycle_Zhu,

    I don't know how many shapes in you slide. If you want to add text in you table, I would recommand you follow these steps:

    1. find out the indexs of the shape for you table.
    2. use the property "table" to find a table in the shape which you find at previous.
    3. use the funtion cell(int row,int column) to select the cell which you want edit.
    4. add the Shape.TextFrame which you want.

    Here is some code which have writen in my PowerPoint Soultion. I hope these can help you.

    Sld.Shapes.AddTable(1, 1, 15, 80, 60, 20);

    Sld.Shapes[4].Table.Cell(1,1).Shape.TextFrame.TextRange.Text =

     

    "111";//I have 3 TextFrame before I add the table.

    Monday, December 27, 2010 8:57 AM
  • Hi XuYang,

    Thanks a lot. I have resolved the problem.Can't write like this:"Table.Shape.TextFrame.TextRange.Text". Just can set the cell text, right? I found it's very complex to set the table borders on Powerpoint 2007. Is there any method to set all borders of the table. It's default to set table all borders when you create the table on Powerpoint 2003.

    Thursday, December 30, 2010 1:24 AM
  • Hi Zhu

    You might want to ask this of the application specialists in the PowerPoint forums on "Answers" (http://answers.microsoft.com/en-us/default.aspx). They usually know the best way to handle the object model.

    But from what I can see, the best you can do is to loop the ROWS of a table and set the Cells.Borders on each row. Pseudocode:

    foreach (PowerPoint.Row in pptApp.Slides[index].Tables[index].Rows)
    {
        rw.Borders[PowerPoint.PpBorderType.ppBorderBottom].Visible = -1
    }

    Most efficient, however, would be to apply a table STYLE with your preferred formatting. To see what I mean, open the presentation as an end-user and click in a table. In the Ribbon, choose the tab Table Tools/Design. Click on the bottommost arrow of the Table Styles gallery to see all available table styles associated with the current (default) theme. I see one for example that, when I hover the mouse over it, says "No style, Table Grid".

    Select the style that looks like what you want.

    Press Alt+F9 to open the Office VBA Editor, then Ctrl+G for the Immediate Window. Type the following (subsituting the appropriate number for each "indexNrOf"):
    ?ActivePresentation.Slides(indexNrOfSlide).Shapes(indexNrOfShape).Table.Style.ID

    Press Enter and you should see the GUID for the table style in the next line. Note this number.

    In your application, add a line of code to use the ApplyStyle method with this GUID. In VB:
    Table.ApplyStyle("{5940675A-B579-460E-94D1-54222C63F5DA}", True)

    Note that it's also possible to provide a custom style, via the Thema attached to the presentation, but that goes beyond the scope of this forum.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Monday, January 3, 2011 10:00 AM
    Thursday, December 30, 2010 8:56 AM
    Moderator
  • Hi Cindy,

    Thanks a lot for your help.

    Wednesday, January 5, 2011 5:30 AM