How to develop an MS Word/MS Office Add-in using Visual Studio 2010 and C#.NET - Video Tutorial<

Rate this article
4.50 out of 5
Developing MS Office based addins or any Microsoft Aplication addins is allways challenging task back in the old COM technology days. With the introduction of Dot net technology, development of addins using VSTO and C#.NET is now a relatively easy task. This article and the accompaneying video and source code will show you how to develop a very basic MS Office add-in using Visual studio 2010 and C#.NET. This article can also be used for developing other Microsoft application add-ins as the basic add-in architecture of most of the Microsoft products are more or less the same and the same framework and the API calls applies for all the products except the products Microsoft bought from third parties. 



Source Code
Ribbon.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.IO;
using Word = Microsoft.Office.Interop.Word;

namespace WordAddInPublishing
{
    public partial class Ribbon
    {
        private void Ribbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void btnPDF_Click(object sender, RibbonControlEventArgs e)
        {

            string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string fileName = "QuickExport.pdf";

            Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(
                Path.Combine(desktopFolder, fileName),
                Word.WdExportFormat.wdExportFormatPDF,
                OpenAfterExport: true);
        }

        private void btnXps_Click(object sender, RibbonControlEventArgs e)
        {
            string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string fileName = "QuickExport.xps";

            Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(
                Path.Combine(desktopFolder, fileName),
                Word.WdExportFormat.wdExportFormatXPS,
                OpenAfterExport: true);
        }
    }
}
Ribbon.cs

namespace WordAddInPublishing
{
    partial class Ribbon : Microsoft.Office.Tools.Ribbon.RibbonBase
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        public Ribbon()
            : base(Globals.Factory.GetRibbonFactory())
        {
            InitializeComponent();
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tabPublishing = this.Factory.CreateRibbonTab();
            this.groupSaveAs = this.Factory.CreateRibbonGroup();
            this.btnXps = this.Factory.CreateRibbonButton();
            this.btnPDF = this.Factory.CreateRibbonButton();
            this.tabPublishing.SuspendLayout();
            this.groupSaveAs.SuspendLayout();
            // 
            // tabPublishing
            // 
            this.tabPublishing.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office;
            this.tabPublishing.Groups.Add(this.groupSaveAs);
            this.tabPublishing.Label = "Publishing";
            this.tabPublishing.Name = "tabPublishing";
            // 
            // groupSaveAs
            // 
            this.groupSaveAs.Items.Add(this.btnXps);
            this.groupSaveAs.Items.Add(this.btnPDF);
            this.groupSaveAs.Label = "Save As";
            this.groupSaveAs.Name = "groupSaveAs";
            // 
            // btnXps
            // 
            this.btnXps.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
            this.btnXps.Image = global::WordAddInPublishing.Properties.Resources.XPS_icon_48x48;
            this.btnXps.Label = "XPS";
            this.btnXps.Name = "btnXps";
            this.btnXps.ShowImage = true;
            this.btnXps.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.btnXps_Click);
            // 
            // btnPDF
            // 
            this.btnPDF.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
            this.btnPDF.Image = global::WordAddInPublishing.Properties.Resources.PDFicon_48x48;
            this.btnPDF.Label = "PDF";
            this.btnPDF.Name = "btnPDF";
            this.btnPDF.ShowImage = true;
            this.btnPDF.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.btnPDF_Click);
            // 
            // Ribbon
            // 
            this.Name = "Ribbon";
            this.RibbonType = "Microsoft.Word.Document";
            this.Tabs.Add(this.tabPublishing);
            this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.Ribbon_Load);
            this.tabPublishing.ResumeLayout(false);
            this.tabPublishing.PerformLayout();
            this.groupSaveAs.ResumeLayout(false);
            this.groupSaveAs.PerformLayout();

        }

        #endregion

        internal Microsoft.Office.Tools.Ribbon.RibbonTab tabPublishing;
        internal Microsoft.Office.Tools.Ribbon.RibbonGroup groupSaveAs;
        internal Microsoft.Office.Tools.Ribbon.RibbonButton btnPDF;
        internal Microsoft.Office.Tools.Ribbon.RibbonButton btnXps;
    }

    partial class ThisRibbonCollection
    {
        internal Ribbon Ribbon
        {
            get { return this.GetRibbon<Ribbon>(); }
        }
    }
}
ThisAddIn.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;

namespace WordAddInPublishing
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}



Download Source Code

Joined Date :14-Nov-2012
Articles
Posted
60
Messages
Posted
3

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.




   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments
   Design  HTML

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud