void listBox1_SelectedValueChanged(object sender, System.EventArgs e) { WrappedStyle ws = listBox1.SelectedItem as WrappedStyle; if (ws == null) { log.Warn("SelectedValueChanged, but IsNullOrEmpty. Deselction?"); return; } else { object pStyle; if (ws == selectedValue) { // Fired on something already selected, so deselect listBox1.SelectedIndex = -1; // no selection cc.set_DefaultTextStyle(ref styleDefaultPFont); // can't set to null? selectedValue = null; return; } Word.Style selectedStyle = ws.getStyle(); pStyle = ws.getStyle(); // Displayed the paragraph style; use the linked character style // (but setting the p style seems to work) log.Info("selected " + selectedStyle.NameLocal); //object newStyle = (Word.Style)selectedStyle.get_LinkStyle(); cc.set_DefaultTextStyle(ref pStyle); // I wonder whether we can use the p style? selectedValue = ws; //for next time // Now, select cc, and apply p style, so user see visible result // (only need to do that if you set c style (as opposed to p) // But it only applies the style to the first paragraph, so: cc.Range.set_Style(ref pStyle); } }
public FormStyleChooser() { InitializeComponent(); styleDefaultPFont = Globals.ThisAddIn.Application.ActiveDocument.Styles[Word.WdBuiltinStyle.wdStyleDefaultParagraphFont]; cc = ContentControlMaker.getActiveContentControl(Globals.ThisAddIn.Application.ActiveDocument, Globals.ThisAddIn.Application.Selection); if (cc == null) { // Shouldn't happen log.Error("Which content control?"); this.Close(); } dynamic d = cc.get_DefaultTextStyle(); // actually returns the PARAGRAPH style! if (d == null) { // Nothing selected log.Error("No current style!"); } else { //log.Debug(((Object)d).GetType().FullName); // System.__ComObject //string typeName = ComHelper.GetTypeName(d); //log.Debug(typeName); //Marshal.ReleaseComObject(selection); existingStyle = d as Word.Style; log.Debug(existingStyle.NameLocal); // eg Heading 6 ie the PARAGRAPH style! } foreach (Word.Style s in Globals.ThisAddIn.Application.ActiveDocument.Styles) { try { if (s.Type.Equals(Word.WdStyleType.wdStyleTypeParagraph) && s.Linked) { //log.Debug(s.NameLocal); //log.Debug(".. linked to " + linkedS.NameLocal); WrappedStyle wrapped = new WrappedStyle(s); listBox1.Items.Add(wrapped); if (existingStyle != null && wrapped.ToString().Equals(existingStyle.NameLocal)) { //log.Debug(" found " + existingStyle.NameLocal); listBox1.SelectedItem = wrapped; } } } catch (System.Runtime.InteropServices.COMException) { } } // events click, doubleclick, mouseXXX //this.listBox1.SelectedIndexChanged += new System.EventHandler(listBox1_SelectedIndexChanged); this.listBox1.SelectedValueChanged += new System.EventHandler(listBox1_SelectedValueChanged); }
object styleDefaultPFont = null; //Word.Style #endregion Fields #region Constructors public FormStyleChooser() { InitializeComponent(); styleDefaultPFont = Globals.ThisAddIn.Application.ActiveDocument.Styles[Word.WdBuiltinStyle.wdStyleDefaultParagraphFont]; cc = ContentControlMaker.getActiveContentControl(Globals.ThisAddIn.Application.ActiveDocument, Globals.ThisAddIn.Application.Selection); if (cc == null) { // Shouldn't happen log.Error("Which content control?"); this.Close(); } dynamic d = cc.get_DefaultTextStyle(); // actually returns the PARAGRAPH style! if (d == null) { // Nothing selected log.Error("No current style!"); } else { //log.Debug(((Object)d).GetType().FullName); // System.__ComObject //string typeName = ComHelper.GetTypeName(d); //log.Debug(typeName); //Marshal.ReleaseComObject(selection); existingStyle = d as Word.Style; log.Debug(existingStyle.NameLocal); // eg Heading 6 ie the PARAGRAPH style! } foreach (Word.Style s in Globals.ThisAddIn.Application.ActiveDocument.Styles) { try { if (s.Type.Equals(Word.WdStyleType.wdStyleTypeParagraph) && s.Linked) { //log.Debug(s.NameLocal); //log.Debug(".. linked to " + linkedS.NameLocal); WrappedStyle wrapped = new WrappedStyle(s); listBox1.Items.Add(wrapped); if (existingStyle!=null && wrapped.ToString().Equals(existingStyle.NameLocal)) { //log.Debug(" found " + existingStyle.NameLocal); listBox1.SelectedItem = wrapped; } } } catch (System.Runtime.InteropServices.COMException) { } } // events click, doubleclick, mouseXXX //this.listBox1.SelectedIndexChanged += new System.EventHandler(listBox1_SelectedIndexChanged); this.listBox1.SelectedValueChanged += new System.EventHandler(listBox1_SelectedValueChanged); }