Search code examples
c#cellautosizepowerpoint-2010

Autosize of TextFrame for a specific table cell in PowerPoint presentation programmatically


I got the next situation. Got some variables:

        PowerPoint.Application objApp;
        Microsoft.Office.Interop.PowerPoint.Shape oShape;
        PowerPoint.Presentations objPresSet;
        PowerPoint._Presentation objPres;
        PowerPoint.Slides objSlides;
        PowerPoint._Slide objSlide;
        Microsoft.Office.Interop.PowerPoint.Shape oShape;

I'm creating a PowerPoint 2010 slide presentation:

        objApp = new PowerPoint.Application();
        objApp.Visible = MsoTriState.msoTrue;
        objPresSet = objApp.Presentations;
        objPres = objPresSet.Open(strTemplate, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
        objSlides = objPres.Slides;

Here I'm creating a slide:

        objSlide = objSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

For the next step I add the table to slide:

        int rows = 4;
        int cells = 10;
        oShape = objSlide.Shapes.AddTable(rows, cells, 10, 10, 400, 450);

And I don't have any problems with text adding to a specific cell:

        oShape.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "Dummy Text";

But, the cell does not want to stretch. I tried to modify TextFrame with AutoSize property:

        oShape.Table.Cell(1, 1).Shape.TextFrame.AutoSize = MsoAutoSize.msoAutoSizeTextToFitShape;

And here I'm getting the "ArgumentException":

The specified value is out of range.

Any thoughts why it's happening? If no, are there another way to stretch table cell?


Solution

  • So, thanks to Steve Rindsberg solution would be:

    oShape.Table.Columns._Index(2).Width = 12;
    

    Thats change size of columns in the table pretty well.