none
Microsoft.Office.Interop.Word c# - Add and remove a textbox with text RRS feed

  • Question

  • I am working on Office.Interop.Word using c#. I have a simple winform and has a button. I would like to add a text box with some text at the bottom of current page (Not at footer)and have option to remove the textbox upon clicking on a separate button. This following code block is inserting the text at the end of entire document. But not at the bottom of current page as a text box. Any help ?

    private void btnOK_Click(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Word._Application oWord;
                object oMissing = Type.Missing;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord = Globals.ThisAddIn.Application;
                foreach (word.Section wordSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
                {
                    string documentname = Globals.ThisAddIn.Application.ActiveDocument.Name;
    oWord.ActiveDocument.Characters.Last.Select();
                        oWord.Selection.Collapse();
                        oWord.Selection.InsertParagraph();
                        oWord.Selection.TypeText(docNumber);
    }}

    Tuesday, April 3, 2018 2:19 PM

All replies

  • Hi newbiesaspnet.

    You can try to use code like below.

     private void button4_Click(object sender, RibbonControlEventArgs e)
            {
                Globals.ThisDocument.Application.Selection.GoToNext(Word.WdGoToItem.wdGoToPage);
                Globals.ThisDocument.Application.Selection.MoveEnd(Word.WdUnits.wdCharacter);
               
                Word.Selection currentSelection = Globals.ThisDocument.Application.Selection;
                
                Microsoft.Office.Interop.Word.Shape textbox = Globals.ThisDocument.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 20, 20, 100, 15);
                textbox.Name = "demotextbox";
                
            }

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 6, 2018 1:07 AM
    Moderator
  • Hi newbiesaspnet,

    Did the suggestion from Deepak work for you?

    If it did, I would suggest you mark the solution as answer to close this thread.

    If not, please feel free to let us know.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 24, 2018 6:08 AM