/*
* StyleChooser - C# example
* http://pschmid.net
* Patrick D. Schmid
*/
namespace StyleChooser
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup project
// by right clicking the project in the Solution Explorer, then choosing install.
#endregion
///
/// The object for implementing an Add-in.
///
///
[GuidAttribute("4ECA8ACE-6217-45C2-BD00-0BC8D85D831A"), ProgId("StyleChooser.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2, IRibbonExtensibility
{
///
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
///
public Connect()
{
}
///
/// Implements the OnConnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being loaded.
///
///
/// Root object of the host application.
///
///
/// Describes how the Add-in is being loaded.
///
///
/// Object representing this Add-in.
///
///
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
string version;
bool nonRibbonApp=false;
//assign application and add-in instance objects
applicationObject = (Microsoft.Office.Interop.Word.Application)application;
addInInstance = (Microsoft.Office.Core.COMAddIn)addInInst;
//retrieve Word version
version=applicationObject.Version;
//account for any possible text in the Word version string that could fail the
//detection of the correct version. Get only the major version number (12)
version=version.Split(' ')[0].Split('.')[0];
//check if the major version number is < 12. We need try-catch as this could
//fail if version is not a pure number
try
{
if (Convert.ToDouble(version)<12)
nonRibbonApp=true;
}
catch
{
nonRibbonApp=true;
}
//if we don't have Word 12 or later, unload the add-in
if (nonRibbonApp)
addInInstance.Connect=false;
}
///
/// Implements the OnDisconnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being unloaded.
///
///
/// Describes how the Add-in is being unloaded.
///
///
/// Array of parameters that are host application specific.
///
///
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
}
///
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
/// Receives notification that the collection of Add-ins has changed.
///
///
/// Array of parameters that are host application specific.
///
///
public void OnAddInsUpdate(ref System.Array custom)
{
}
///
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface.
/// Receives notification that the host application has completed loading.
///
///
/// Array of parameters that are host application specific.
///
///
public void OnStartupComplete(ref System.Array custom)
{
}
///
/// Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
/// Receives notification that the host application is being unloaded.
///
///
/// Array of parameters that are host application specific.
///
///
public void OnBeginShutdown(ref System.Array custom)
{
}
public string GetCustomUI(string RibbonID)
{
//get the current assembly
System.Reflection.Assembly assembly=System.Reflection.Assembly.GetExecutingAssembly();
//debug print all names of all embedded resources
//string [] t=assembly.GetManifestResourceNames;
//foreach (string i in t)
// System.Diagnostics.Debug.WriteLine(i);
//access the RibbonX.xml file
Stream resource=assembly.GetManifestResourceStream("StyleChooser.RibbonX.xml");
if (resource!=null)
{
//read the file from the stream and make sure to close the stream
StreamReader addInXml=new StreamReader(resource);
String xml=addInXml.ReadToEnd();
addInXml.Close();
//return the RibbonX code to Office
return xml;
}
//if there is a problem with loading the resource file, return an empty XML file to avoid errors
return "";
}
//callback for onAction
public void onAction(IRibbonControl control)
{
if (control.Id=="aboutStyleChooser")
System.Windows.Forms.MessageBox.Show("pschmid.net StyleChooser Example", "StyleChooser Add-In");
}
//return the number of items in comboBox
public int GetItemCount(IRibbonControl control)
{
if (control.Id=="comboStyleChooser")
{
Microsoft.Office.Interop.Word.Document document=((Microsoft.Office.Interop.Word.Window)control.Context).Document;
createStylesTable(document);
return ((ArrayList)documents[document]).Count;
}
return 0;
}
//create a table of all styles a user can use for a particular document
private void createStylesTable(Microsoft.Office.Interop.Word.Document document)
{
ArrayList styles=new ArrayList(document.Styles.Count);
foreach (Microsoft.Office.Interop.Word.Style i in document.Styles)
{
if (!i.Hidden && i.InUse)
styles.Add(i.NameLocal.Trim());
}
documents[document]=styles;
}
//return the label for one item in the comboBox
public string GetItemLabel(IRibbonControl control, int index)
{
if (control.Id=="comboStyleChooser")
{
Microsoft.Office.Interop.Word.Document document=((Microsoft.Office.Interop.Word.Window)control.Context).Document;
return (string)((ArrayList)documents[document])[index];
}
return "";
}
public void onLoad(IRibbonUI Ribbon)
{
ribbon=Ribbon;
applicationObject.WindowSelectionChange+=new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(applicationObject_WindowSelectionChange);
}
//determines the currently active style
public string GetText(IRibbonControl control)
{
if (control.Id=="comboStyleChooser")
{
Microsoft.Office.Interop.Word.Selection selection=(Microsoft.Office.Interop.Word.Selection)applicationObject.Selection;
return ((Microsoft.Office.Interop.Word.Style)selection.get_Style()).NameLocal.Trim();
}
return "";
}
//change the style when user picks a different one in comboBox
public void onChange(IRibbonControl control, string text)
{
if (control.Id=="comboStyleChooser")
{
Microsoft.Office.Interop.Word.Selection selection=(Microsoft.Office.Interop.Word.Selection)applicationObject.Selection;
object obj=text;
selection.set_Style(ref obj);
}
}
//event to change currently shown style
private void applicationObject_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)
{
ribbon.InvalidateControl("comboStyleChooser");
}
private Microsoft.Office.Interop.Word.Application applicationObject;
private Microsoft.Office.Core.COMAddIn addInInstance;
private IRibbonUI ribbon;
private Hashtable documents=new Hashtable(10);
}
}