none
Autofit Shape in word document. RRS feed

  • Question

  • How can I auto fit the Shape in word document using C# or VBA.

    Shape.TextFrame.AutoSize = soAutoSizeShapeToFitText 

    is not working


    dddd

    Thursday, July 11, 2019 5:03 PM

All replies

  • Hello,

    The AutoSize property returns integer which that represents whether a text frame is sized automatically. 

    Shape.TextFrame.AutoSize = Microsoft.Office.Core.MsoAutoSize.msoAutoSizeShapeToFitText;

    See the MsoAutoSize enum for possible values.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, July 11, 2019 5:21 PM
  • Hello Eugene

    Thanks for the reply.

    But I am using following code to insert shape and auto fit it               

     Shape shp = activedocument.Shapes.AddShape((int)Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle  , leftPos, TopPos, shapeWidth, shapeHeight, ref obj);
    shp.TextFrame.AutoSize = (int)Microsoft.Office.Core.MsoAutoSize.msoAutoSizeShapeToFitText;

    and it does not change to autofit property of shape

    above code works for excel but not for word


    dddd


    • Edited by tilanka Thursday, July 11, 2019 6:20 PM
    Thursday, July 11, 2019 5:59 PM
  • Word provides a macro recorder. To make sure that you do it correctly you can records the required steps using the macro recorder and then get a VBA code generated for you.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, July 11, 2019 6:44 PM
  • macro recorder does not recording this.

    Also it seems that word does not have textframe2 property


    dddd

    Thursday, July 11, 2019 7:08 PM