none
Formating TextBox Shape programtically in Excel 2010 C# RRS feed

  • Question

  • Hi 

    I am creating an application level addin and have a following line of code to add a textbox to a worksheet. 

    Excel.Shape sh = ws.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 150, 30, 30, 30);

    Then I managed to do the following formatings. 

    sh.TextFrame2.TextRange.Text = "Testing";

    sh.TextEffect.Alignment = Office.MsoTextEffectAlignment.msoTextEffectAlignmentCentered;

     sh.TextEffect.FontSize = 24;
     sh.TextEffect.FontBold = Office.MsoTriState.msoCTrue;

    I couldn't do the followings.

    1. change the background and foreground color of the text box. 

    2. change the color of the border (I want to make it white) 

    3. select a portion of the text and format (for example, let's say there are three lines of text in the textbox. I'd like to apply font size 24 for the first line, size 18 for the second line, 12 for the third line. 

    I have googled for two hours and dig into a book I have but couldn't find an example. 

    Could you help me please? 

    Kind regards 

    Mark


    • Edited by SQLMa Wednesday, January 2, 2013 7:12 AM
    Wednesday, January 2, 2013 7:09 AM

Answers

  • <<Finally, I am confused with WordArt you brought up. In Excel  different formatting can apply to part of text in a textbox. I assume this can be done programatically as well. Does it has to do with WordArt?>>

    The TextEffect part of the object model applies the "WordArt" formatting - the WordArt group in the Drawing Tools/Format tab.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by SQLMa Thursday, January 3, 2013 10:49 PM
    Thursday, January 3, 2013 6:21 AM
    Moderator

All replies

  • Hi Mark

    The background and line color will depend from the sh (Shape) object: sh.Fill.BackColor and sh.Line.ForeColor. But if you don't want to see a line, set Line.Visible = false

    The foreground is the text, so I believe you access over TextRange like this:
     sh.TextFrame2.TextRange.Font.Fill

    FWIW TextEffect is for WordArt. I'm not sure that's what you want?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 2, 2013 1:39 PM
    Moderator
  • Thank for the reply. 

    I managed to make the border invisible by 

    sh.Line.Visible = Office.MsoTriState.msoFalse;

    Now I know that Line is the border property of the textbox. 

    For setting color, I knew I could use Forecolor and BackColor property but was struggling with setting the color to the property. I finally found how to do it.. 

    sh.Line.ForeColor.RGB = System.Drawing.Color.LightCoral.ToArgb();  

    What I missed before was RGB property on the left and ToArgb() method on the right.  

    Finally, I am confused with WordArt you brought up. In Excel  different formatting can apply to part of text in a textbox. I assume this can be done programatically as well. Does it has to do with WordArt? 




    • Edited by SQLMa Thursday, January 3, 2013 12:12 AM
    Thursday, January 3, 2013 12:08 AM
  • <<Finally, I am confused with WordArt you brought up. In Excel  different formatting can apply to part of text in a textbox. I assume this can be done programatically as well. Does it has to do with WordArt?>>

    The TextEffect part of the object model applies the "WordArt" formatting - the WordArt group in the Drawing Tools/Format tab.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by SQLMa Thursday, January 3, 2013 10:49 PM
    Thursday, January 3, 2013 6:21 AM
    Moderator
  • I see. 

    Thank you again. 

    I will try it!!

    Thursday, January 3, 2013 10:48 PM