Create Word-AddIn using C# step by step
This article describes about how to create a Word-AddIn using c# and how to do the string manipulations within a active word document.
Create Word-AddIn using C# step by step
Using this article, you can create customized menus and buttons within a word document. And you can do any type of text manipulation with in a word document.
Then you can create a separate window to display styles and give option to apply styles when they click styles within that styles window.
Do the following steps one by one very carefully.
Open Visual Studio 2005
Create a new project
Select Visual C# Office 2007 Addins in Project Types pane
Select Word Add-in in Templates Pane
Give appropriate name (MyWordAddIn) to the project
Right click of Project Name(MyWordAddIn) and select add new item
Select Ribbon Support in Templates Pane and change ribbon name to MyRibbon
Here uncomment the coding of class ThisAddin within the namespace of MyWordAddIn
Change codes in MyRibbon.xml ( if you you need only )
Add a new class named MyClass within this project
add the folowing codes in the created class name MyClass
using System;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
namespace MyWordAddIn
{
public class MyClass
{
public static Word.Application WrdApp;
}
}
Update the following codes within ThisAddIn.cs
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
namespace MyWordAddIn
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
MyClass.WrdApp = Application;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Update the following codes within the MyRibbon.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
namespace MyWordAddIn
{
// TODO:
// This is an override of the RequestService method in the ThisAddIn class.
// To hook up your custom ribbon uncomment this code.
public partial class ThisAddIn
{
private MyRibbon ribbon;
protected override object RequestService(Guid serviceGuid)
{
if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
{
if (ribbon == null)
ribbon = new MyRibbon();
return ribbon;
}
return base.RequestService(serviceGuid);
}
}
[ComVisible(true)]
public class MyRibbon : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public MyRibbon()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("MyWordAddIn.MyRibbon.xml");
}
#endregion
#region Ribbon Callbacks
public void OnLoad(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
}
public void OnToggleButton1(Office.IRibbonControl control, bool isPressed)
{
MessageBox.Show(MyClass.WrdApp.ActiveDocument.Content.Text);
//if (isPressed)
// MessageBox.Show("Pressed");
//else
// MessageBox.Show("Released");
}
#endregion
#region Helpers
private static string GetResourceText(string resourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] resourceNames = asm.GetManifestResourceNames();
for (int i = 0; i < resourceNames.Length; ++i)
{
if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
{
if (resourceReader != null)
{
return resourceReader.ReadToEnd();
}
}
}
}
return null;
}
#endregion
}
}
Now you are ready to run the project.
Run the project ( click F5)
Word document will open
You can see My Button ( your created button ) within the Add-Ins Menu
If you click the same button,
Whole content of the word document will display in a message box.
Hereafter you can do any string manipulations within the selected word document.
I hope you understand and it will be useful to you.
good one. if you can please add attachment of working code this article. anyway thank you to submit a good article ;)