1
Answer

window selection change event is not firing in word vsto add-in C#

Jayanthi M

Jayanthi M

7h
31
1

Hi,

could anyone suggest me how to call the style change/click event to fire. This is my sample code.

<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load">
    <ribbon>
        <tabs>
            <tab idMso="TabAddIns">
                <group id="ContentGroup" label="Content">
                    <button id="textButton" label="Draft assist panel"
                         screentip="Text" onAction="OnTextButton"
                         supertip="click on to open the next drafting style panel"/>
                    
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

In Ribbon1.cs(Ribbon visual designer)
 

Creating own style and creating a new paragraph when click/change on the custom style but the selection event is not firing. Here is the my sampke code.

public void OnTextButton(Office.IRibbonControl control)
{
    string[] array_StyleRules = null;
    array_StyleRules = getStyleRules("");

    Microsoft.Office.Interop.Word.Application wordApp = Globals.ThisAddIn.Application;
    Microsoft.Office.Interop.Word.Document doc = wordApp.ActiveDocument;

    Microsoft.Office.Interop.Word.Style newStyle;

    // Create a new custom style
    foreach (string arr_style in array_StyleRules)
    {
        newStyle = doc.Styles.Add(arr_style, Microsoft.Office.Interop.Word.WdStyleType.wdStyleTypeCharacter);
        newStyle.Font.Bold = 1;
        newStyle.Font.Italic = 1;
        // Apply the custom style to selected text (for example)
        if (wordApp.Selection != null && !string.IsNullOrEmpty(wordApp.Selection.Text))
        {
            wordApp.Selection.set_Style(arr_style);
        }
    }

}

The below Ribbon.cs event is not firing when change the custom style. could anyone please help me how and where I need to call style change event 

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Globals.ThisAddIn.Application.WindowSelectionChange +=
        new ApplicationEvents4_WindowSelectionChangeEventHandler(Application_WindowSelectionChange);

}

 

private void Application_WindowSelectionChange(Word.Selection Sel)
  {
      if (Sel != null)
      {
          InsertNewParagraph(Sel);
      }
  }
  private void InsertNewParagraph(Selection selection)
  {
      Microsoft.Office.Interop.Word.Application wordApp = Globals.ThisAddIn.Application;
      Microsoft.Office.Interop.Word.Document doc = wordApp.ActiveDocument;

      // Insert a new paragraph at the current selection
      Paragraph newParagraph = wordApp.ActiveDocument.Content.Paragraphs.Add();// selection.Paragraphs.Add();
      newParagraph.Range.Text = "This is a new paragraph.";
      newParagraph.Range.set_Style(selection);
      newParagraph.Range.InsertParagraphAfter();
  }

Answers (1)