none
Looking for a reasonable button for Word document RRS feed

  • Question

  • Hi,

    I need to use buttons in my document.

    Option 1:

    Tried using Windows.Forms.Controls.Button, but the problem with these controls are, they are not stored along with the document. Retrieving the button information when reopening the document and placing them back in the right places is causing lot of issues and performance crisis.

    Option 2:

    So, I tried Microsoft.Vbe.Interop.Forms.Button. It is looking fine on the document,but if it is inserted on top of a contentcontrol, I am able to hook up the C# code when clicked. But, if I try to insert the button at runtime, the button goes behind the contentcontrol.

    object ClassType = "Forms.CommandButton.1";
                Word.Shape _inlineButton = Globals.ThisAddIn.applicationObject.Selection.Range.InlineShapes.AddOLEControl(ref ClassType).ConvertToShape();
                MSForms.CommandButton _cmdButton = _inlineButton.OLEFormat.Object as MSForms.CommandButton;
                _cmdButton.Caption = "Browse";
                _cmdButton.Click += new MSForms.CommandButtonEvents_ClickEventHandler(CmdBtn_Click);

    Option 3:
    So, tried changing the button to content control. Wherever I need a button, I add a contentcontrol with the button text there. OnContentControl Enter event, I check the tag of the contentcontrol and trigger the C# code accordingly. This works fine on mouseclick. I need to add these buttons inside table. So, when users press a tab, the button event is being raised because Contentcontrol OnEnter event will be called when cursor moves in. This does not resemble actual button.

    Is there any other way to add a button without all the above issues?

    Thanks,

    Sree.


    Thanks a lot. Sree

    Thursday, March 8, 2012 12:36 PM

All replies

  • Hi Sree,

    We're looking into this query, please wait some time.

    best regards,


    Forrest Guo | MSDN Community Support | Feedback to us

    Sunday, March 11, 2012 4:17 PM
    Moderator
  • Thanks Forrest Guo.

    When I tried using the below code, the button started to appear correctly. I am able to hook the even handlers whenever the document is reopened as well. But the event stops working abruptly. No errors are being thrown, but it just stops invoking the method attached to the click event.

    object ClassType = "Forms.CommandButton.1";
                Word.InlineShape _inlineButton = Globals.ThisAddIn.applicationObject.Selection.Range.InlineShapes.AddOLEControl(ref ClassType);
                MSForms.CommandButton _cmdButton = _inlineButton.OLEFormat.Object as MSForms.CommandButton;
                _cmdButton.Caption = "Browse";
                _cmdButton.Click += new MSForms.CommandButtonEvents_ClickEventHandler(CmdBtn_Click);

    I am eagerly waiting for you to come back on this.

    Thanks,

    Sree.


    Thanks a lot. Sree

    Tuesday, March 13, 2012 9:39 AM
  • Hi Sree,

    You’re looking for an alternative. The solution is to
    explore and consider using a Custom Task Pane as part of a VSTO add-in. On that
    pane you can have a single button, or multiple controls, each with code behind
    it. You can set the width and height programmatically, and place the task pane
    where you want it. Please see the following site:
    Custom Task Panes Overview - MSDN – Explore Windows, Web, Cloud ...
    http://msdn.microsoft.com/en-us/library/aa942864.aspx

    To add a button to just the specific document see the
    discussion about Action Pane – a VSTO feature that associates with a single
    Word document:
    What is the Actions Pane? - Microsoft Visual Studio Tools for the ...
    http://blogs.msdn.com/b/vsto2/archive/2004/03/11/88183.aspx

    Also see this Forum thread::
    How to write contents in Word document using Add-ins button using code C#
    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/6cc8f150-8a88-4488-b6a1-3a7fd655cf3a

    Regards,
    Chris Jensen
    Senior Technical Support Lead





    Chris Jensen

    Tuesday, March 13, 2012 3:17 PM
    Moderator
  • Thanks Chris for providing alternates. But, I am expecting buttons on documents. Because, we are already using Custom task panes and ribbons. Placing a button somewhere away from the document might not give a good user experience, and the client refused these two suggestions.

    My doubt is why the buttons are losing event handlers in between. As I mentioned in the above reply, inserting a button as inline shape and attaching the event handlers work for two or three times. But, you never know when it will stop working. Is it a bug in Word?

    I am expecting a button as below. Is it possible by any chance?

    Button what I am expecting

    Thanks,

    Sree


    Thanks a lot. Sree

    Wednesday, March 14, 2012 6:38 AM
  • Hello Sree,

    You ask “Is it a bug in Word?”

    Reporting bugs to Microsoft Development is the job of Microsoft Customer
    Support engineers and Microsoft Technical Account Managers.

    Because of the complexity of your project please consider opening a technical
    support incident with Microsoft Customer Technical Support. If the Customer
    Support Engineer confirms that the issue is a bug the charge for the incident will be refunded. Otherwise the Engineer will work with you to resolve the issue to your satisfaction.

    To view the options for using Customer support please visit the following link:
    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Regards
    Chris Jensen
    Senior Technical Support Lead





    Chris Jensen

    Wednesday, March 14, 2012 2:30 PM
    Moderator