El Bruno

Blog dedicado a las experiencias del desarrollo con tecnologías Microsoft.
Las Ribbons molan ... pero que pasa con Office 2003 y VSTO 2nd Edition ?

Buenas,

mientras espero que en un proyecto donde estoy a tiempo parcial, se decidan a migrar las estaciones de trabajo a Microsoft Office 2007; tengo 2 opciones:

La segunda opción es la que me aprobó mi Project Manager, asi que desempolvé parte de mi hemisferio izquierdo y recordé cosas tan básicas como;

  • trabajar con las Toolbars, perdón CommandBars de Office.
  • crear un SmartTag.
  • agregar mis controles a un TaskPane.

He dejado el ejemplo completo de un projecto que implementa estas funcionalidades para Word 2003; pero si lo analizamos por partes podremos ver que:

1. Trabajamos con un proyecto de tipo Office // 2003 Add-ins // Word Add-in

 

2. En el evento StartUp del add-in, agregamos el SmartTag personalizado que hemos creado (línea 7); un UserControl al panel CustomActions (línea 11) e iniciamos la interacción con las Toolbars de Word (línea 15).

1 private void ThisDocument_Startup(object sender, System.EventArgs e) 2 { 3 ActiveDocument = this; 4 5 // Agrego un SmartTag 6 SampleSmartTagEx smEx = new SampleSmartTagEx(); 7 this.VstoSmartTags.Add(smEx); 8 9 // Agrego el UserControl1 al ActionPane 10 UserControl1 uc1 = new UserControl1(); 11 this.ActionsPane.Controls.Add(uc1); 12 this.ActionsPane.Show(); 13 14 // Agrego la configuracion para las toolbars 15 AddToolbars(); 16 }

 

3. Nuestro SmartTag, es una clase llamada SampleSmartTagEx que hereda de SmartTag (línea 5); y dentro de la misma creamos 2 objetos de tipo Action; y cuando inicializamos la clase, los creamos con el texto que mostrarán al desplegarse

Ademas implementamos los handlers de los eventos Click de cada uno (líneas 8 y 10), para insertar texto dentro de Word (línea 25) o mostrar un mensaje (línea 20). Finalmente definimos las reglas que determinan la presentación del SmartTag, utilizando una expresión regular (línea 14) o un texto predefinido (línea 17).

1 class SampleSmartTagEx : SmartTag 2 { 3 private Action popupTextAction, insertTextAction; 4 5 internal SampleSmartTagEx() : base("SampleSmartTagEx", "Prueba de un SmartTag :D") 6 { 7 popupTextAction = new Action("Mostrar Hola mundo"); 8 popupTextAction.Click += new ActionClickEventHandler(popupTextAction_Click); 9 insertTextAction = new Action("Insertar Hola Mundo"); 10 insertTextAction.Click += new ActionClickEventHandler(insertTextAction_Click); 11 this.Actions = new Action[] { popupTextAction, insertTextAction }; 12 13 // Expresion regular para evaluar el lanzamiento del SmartTag 14 this.Expressions.Add(new Regex(@"\b[0-9]\b")); 15 16 // Expresion "fija" para evaluar el lanzamiento del SmartTag 17 Terms.Add("TestEx"); 18 } 19 20 void popupTextAction_Click(object sender, ActionEventArgs e) 21 { 22 MessageBox.Show( "Hola Mundo" ); 23 } 24 25 void insertTextAction_Click(object sender, ActionEventArgs e) 26 { 27 string text = "Hola Mundo"; 28 e.Range.Text = text; 29 } 30 31 protected override void Recognize(string text, Microsoft.Office.Interop.SmartTag.ISmartTagRecognizerSite site, Microsoft.Office.Interop.SmartTag.ISmartTagTokenList tokenList) 32 { 33 // Do nothing and allow the base class to handle the recognition 34 base.Recognize(text, site, tokenList); 35 } 36 37 }

4. Para incluir dentro de un panel un control personalizado, agregamos un UserControl al TaskPane ActionsPane (línea 10).

 

5. Finalmente para agregar una CommandBar personalizada con 2 botones y un combobox

tenemos la función AddToolbars que crea obtiene la comandBar y si no existe, la crea y luego crea cada uno de los elementos que estarán dentro de la misma.

1 Microsoft.Office.Core.CommandBar cmdBar; 2 Microsoft.Office.Core.CommandBarButton firstB; 3 Microsoft.Office.Core.CommandBarButton secondB; 4 Microsoft.Office.Core.CommandBarComboBox cmbBox; 5 6 public void AddToolbars() 7 { 8 try 9 { 10 cmdBar = this.CommandBars["Test"]; 11 } 12 catch (Exception ex) 13 { } 14 if (cmdBar == null) 15 { 16 cmdBar = this.CommandBars.Add("Test", 1, missing, true); 17 } 18 cmdBar.Visible = true; 19 20 try 21 { 22 firstB = (CommandBarButton)cmdBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, missing); 23 firstB.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption; 24 firstB.Caption = "Button 1"; 25 firstB.Tag = "Button 1"; 26 firstB.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(firstB_Click); 27 28 secondB = (CommandBarButton)cmdBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, missing); 29 secondB.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption; 30 secondB.Caption = "Button 2"; 31 secondB.Tag = "Button 2"; 32 secondB.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(secondB_Click); 33 34 cmbBox = (CommandBarComboBox)cmdBar.Controls.Add(MsoControlType.msoControlComboBox, missing, missing, missing, missing); 35 cmbBox.Style = MsoComboStyle.msoComboNormal; 36 cmbBox.AddItem("Uno", 1); 37 cmbBox.AddItem("Dos", 2); 38 cmbBox.Change += new _CommandBarComboBoxEvents_ChangeEventHandler(cmbBox_Change); 39 } 40 catch (Exception ex) 41 { 42 throw; 43 } 44 }

 

Además implementamos los eventos Click() de cada uno de los botones y el evento Change() del ComboBox para mostrar un mensaje en cada evento.

1 void cmbBox_Change(CommandBarComboBox Ctrl) 2 { 3 MessageBox.Show(cmbBox.Text); 4 } 5 6 void secondB_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) 7 { 8 MessageBox.Show("click B"); 9 } 10 11 void firstB_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) 12 { 13 MessageBox.Show("click A"); 14 }

 

Pues bien, tampoco es tan dificil con Office 2003 ¿no?

y a que seguramente hay muchas cosas más que se pueden hacer con un poquito más de trabajo :D

 

Saludos

El Bruno

Published miércoles, 28 de febrero de 2007 23:34 by elbruno

Comments

No Comments