none
Insert a textbox in Word 2010 RRS feed

  • Question

  • Hello,

    I have been developing Word 2010 plugins using VS2010. So far inserting content controls and shapes, tables has been good but this time I'm really stuck. I know this might be easy but I need some guidance. I want to Add a Textbox (shape) on Word 2010 document. I'll be adding content controls to it to hide/show later. But to get the initial textbox added seems a hurdle.

    Thanks a lot in advance for the help!

    Regards,

    May

    Wednesday, November 20, 2013 8:44 PM

All replies

  • Hi May

    Try the Document.Shapes.AddTextBox method :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 21, 2013 4:31 PM
    Moderator
  • Thanks for your reply Cindy!

    So if I put Document.Shapres.AddTextBox(0,0,0,0)...it gives me an error. I was thinking if I put this at the top, I should be able to drag it anywhere but apparently I can't seem to still get this thing rite!

    Any thoughts?

    Thanks a lot,

    May

    Tuesday, December 3, 2013 8:03 PM
  • Hello May1006,

    Please note that you specified 0 for the Width and Height parameters of the AddTextbox function. That is why you get an error.

    Sub NewCanvasTextbox() 
     Dim docNew As Document 
     Dim shpCanvas As Shape 
     
     'Create a new document and add a drawing canvas 
     Set docNew = Documents.Add 
     Set shpCanvas = docNew.Shapes.AddCanvas _ 
     (Left:=100, Top:=75, Width:=150, Height:=200) 
     
     'Add a text box to the drawing canvas 
     shpCanvas.CanvasItems.AddTextbox _ 
     Orientation:=msoTextOrientationHorizontal, _ 
     Left:=1, Top:=1, Width:=100, Height:=100 
    End Sub
    

    Wednesday, December 4, 2013 12:26 PM
  • Thanks Eugene.

    I tried your code but I get a COM error: Error HRESULT E_FAIL has been returned from a call to a COM component.

    My snippet (C#) is below

    Microsoft.Office.Interop.Word.Shape picShape =  WDocument.Shapes.AddCanvas(100, 75, 150, 200);               picShape.CanvasItems.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 150, 10, 40, 40);

    Any thoughts?

    Thanks,

    May


    • Edited by May1006 Wednesday, December 4, 2013 5:35 PM missed information
    Wednesday, December 4, 2013 5:35 PM
  • Hello May1006,

    I would recommend breaking the line of code listed above into separate lines:

    Word.Shapes shapes = WDocument.Shapes;
    Word.Shape shape=  shapes.AddCanvas(100, 75, 150, 200);
    
    Do you get any exceptions or error messages in that case? If so, what method or property call causes it?
    Thursday, December 5, 2013 5:37 PM
  • Hi May

    The following cannot work as Shapres is not a valid object:

      Document.Shapres.AddTextBox

    What is the error you get when you use the line of code, with values for the height and width?

    Also, please note that this method has SIX parameters, of which FIVE are required - your code uses only four. You mention you're using C#, so you may need to define all six, even if you pass an object to which System.Type.Missing is assigned to the last one.

    In any case, it appears you are missing the first parameter, which defines the orientation (data type MsoOrientation) for the text in the text box. I'm guessing this is the reason for the error, whatever it is.

    Also, I do NOT advise you to follow the advice about putting the text box in a Canvas. Canvas is deprecated and is NOT necessary for a text box.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, December 5, 2013 5:55 PM
    Moderator
  • Thanks for the reply Cindy and sorry for not replying on time. I tried to add all the 6 parameters but I still don't understand what is missing.

    WDocument.Shapes.AddTextbox(Microsoft.Office.Core.

    MsoTextOrientation.msoTextOrientationHorizontal, 100, 75, 150, 200, System.Type.Missing);

    Error I'm getting: Error HRESULT E_FAIL has been returned from a call to a COM component.

    Also if I place the textbox at this location, I want to know if the user can drag it anywhere in the document and just place it.

    Monday, December 23, 2013 2:48 PM
  • Hi May

    Since you do not specifiy a RANGE object for that sixth parameter, Word will try to use the current Selection. It's possible that the current Selection in the currently ActiveDocument is not available for inserting a Shape - this could the be the cause of the error you're getting. But it's difficult for me to say with the meagre amount of information available.

    <<Also if I place the textbox at this location, I want to know if the user can drag it anywhere in the document and just place it.>>

    Since we have no idea what "this location" is and it's impossible to predict what it could be when the code is run, there can be no answer to your question. Generally, I'd expect the answer to be that, yes, the user can drag it anywhere...


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by May1006 Monday, December 23, 2013 6:36 PM
    • Unmarked as answer by May1006 Thursday, November 6, 2014 4:27 PM
    Monday, December 23, 2013 5:13 PM
    Moderator
  • You are right Cindy. Even though I passed the Range object, since it wasn't availab,e it threw the error. As soon as I used the currentRange, it worked! Thanks once again!

    And yes, I was able to move it. My next goal would be to insert not just one, but multiple textboxes. Hopefully it should be smooth. I'm only concerned since I'm assing the parameters to set it the first time. It shouldn't shout on inserting the second the textbox at the same location.

    Monday, December 23, 2013 6:38 PM
  • Hi May

    Word won't shout if you put another textbox in the same location, but they will be one right over the other. You might want to adjust the parameters for top and left slightly so that they "cascade" a bit...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, December 24, 2013 10:17 AM
    Moderator
  • Hi Cindy,

    I was able to add multiple textboxes. I have also added content controls to each of the textboxes. And like you said, right now they are overlapping on each other. My only concern is that the cursor doesn't get placed in content control of the textbox. It exists out of the shape where the I have it initially. Can you please help me in getting it in the textbox. 

     Word.Document WDocument = application.ActiveDocument;
                        Word.Shape tbox = WDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 100, 100, 150, 100, WRange);
                        Word.Range wRange = tbox.TextFrame.TextRange;
                        object obj = (object)wRange;
    
                        Word.ContentControl textControl = tbox.TextFrame.TextRange.ContentControls.Add(Word.WdContentControlType.wdContentControlText, ref obj);
                        tbox.Fill.ForeColor.RGB = 14804223;
    
                       
                        baseText = "{}";
                        textControl.Range.Text = baseText;
    
                        textControl.Range.Font.Color = noteFont.Color;
                        textControl.Range.Font.Size = noteFont.Size;
                        textControl.Range.Font.Name = noteFont.Name;
                        textControl.Range.Font.Italic = noteFont.Italic;
                        textControl.Range.Font.Shading.BackgroundPatternColor = noteFont.Shading.BackgroundPatternColor;
    
                        objMoveUnit = Word.WdUnits.wdCharacter;
                        objMoveLength = 1;
    
                        textControl.Range.Font = noteFont;
     wRange.MoveStart(ref objMissing, objMoveLength);
    
                     

    In short, when I add the textbox at the position mentioned in the code, I insert the content control as well. It has opening and closing tag - {}. So I want the cursor to be placed within the {}. 

    wRange.MoveStart does give me the range correctly but it doesn't put the cursor there. I think I need the WSelection but don't know how to convert the wRange to WSelection. Please help me!!!

    Thanks,

    May

    Wednesday, November 5, 2014 4:23 AM
  • I am still struggling to get the cursor in the content control of the textbox. Any suggestions? Please help!
    Thursday, November 6, 2014 4:27 PM