none
Apply a specific style to any text anywhere in MS Word upon a ribbon button click using c# RRS feed

  • Question

  • Hello All,

    I'am trying to add a style to any ms word document upon a click of a ribbon button , pretty much works like the bold,underline,heading buttons of ms word, but the problem is , I'am not able to toggle (switch on and off ) the style even if the button is of type toggle .Here is my code snippet.

    PS:I'am very much new to the interop libraries and intend to learn a lot over the course of time.Any help is much appreciated.Thanks in advance!


    private void SectionBtn_Click(object sender, RibbonControlEventArgs e)
            {
                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                        Word._Document oDoc;
                oDoc = Globals.ThisAddIn.Application.ActiveDocument;
                      Word.Paragraph oPara1;
                oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                         oPara1.Range.Font.Bold = 1;//just for demo but intend to add my own custom font styles
                }

    • Moved by CoolDadTx Wednesday, September 17, 2014 3:16 PM Office related
    Wednesday, September 17, 2014 10:15 AM

Answers

  • Hello Apoorva,

    Did you try to debug the code? Is your callback invoked by the host application?

    If you use the toggleButton in the markup you need to use the following callback signatures:

    getPressed

    C#: bool GetPressed(IRibbonControl control)

    VBA: Sub GetPressed(control As IRibbonControl, ByRef returnValue)

    C++: HRESULT GetPressed([in] IRibbonControl *pControl, [out, retval] VARIANT_BOOL *pvarfPressed)

    Visual Basic: Function GetPressed(control As IRibbonControl) As Boolean

    onAction

    C#: void OnAction(IRibbonControl control, bool pressed)

    VBA: Sub OnAction(control As IRibbonControl, pressed As Boolean)

    C++: HRESULT OnAction([in] IRibbonControl *pControl, [in] VARIANT_BOOL *pvarfPressed)

    Visual Basic: Sub OnAction(control As IRibbonControl, pressed As Boolean)

    Most probably a wrong declaration is used for the callback. Is this the case?

    Wednesday, September 17, 2014 6:18 PM

All replies

  • Hello Apoorva,

    Did you try to debug the code? Is your callback invoked by the host application?

    If you use the toggleButton in the markup you need to use the following callback signatures:

    getPressed

    C#: bool GetPressed(IRibbonControl control)

    VBA: Sub GetPressed(control As IRibbonControl, ByRef returnValue)

    C++: HRESULT GetPressed([in] IRibbonControl *pControl, [out, retval] VARIANT_BOOL *pvarfPressed)

    Visual Basic: Function GetPressed(control As IRibbonControl) As Boolean

    onAction

    C#: void OnAction(IRibbonControl control, bool pressed)

    VBA: Sub OnAction(control As IRibbonControl, pressed As Boolean)

    C++: HRESULT OnAction([in] IRibbonControl *pControl, [in] VARIANT_BOOL *pvarfPressed)

    Visual Basic: Sub OnAction(control As IRibbonControl, pressed As Boolean)

    Most probably a wrong declaration is used for the callback. Is this the case?

    Wednesday, September 17, 2014 6:18 PM
  • I used a syntax such as this

    if(toggle_btn.clicked==true)

    {

    //apply a style 

    range.Font.Bold = 1;

    }

    else

    {

    range.Font.Bold = 0;

    }

    Thank you very much Eugene it worked :)

    Monday, September 22, 2014 9:14 AM