Office 2007 Word plug-in

Ez a program a word dokumentumok szétválogatására készült. Egy Ribbon komponens mintaprogram.

connect.cs

namespace MoveDocument
{
    using System;
    using Extensibility;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using Microsoft.Office.Core;
 
	[GuidAttribute("8010903C-B552-43C6-8C6F-8302B7126A40"), ProgId("MoveDocument.Connect")]
	public class Connect : Object, Extensibility.IDTExtensibility2, IRibbonExtensibility
	{
 
        string IRibbonExtensibility.GetCustomUI(string RibbonID)
        {
            return Properties.Resources.Ribbon;
        }
 
        public Connect()
		{
		}
 
	public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref Array custom)
	{
		applicationObject = (Microsoft.Office.Interop.Word.Application) application;
                addInInstance = addInInst;
	}
 
	public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref Array custom)
	{
            if (disconnectMode != ext_DisconnectMode.ext_dm_HostShutdown)
            {
                OnBeginShutdown(ref custom);
            }
            applicationObject = null;
		}
 
	public void OnAddInsUpdate(ref Array custom)
	{
	}
 
	public void OnStartupComplete(ref Array custom)
	{
	}
 
 
        public void OnActionKorte(IRibbonControl control)
        {
            Microsoft.Office.Interop.Word.Document activeDoc = applicationObject.ActiveDocument;
            if (activeDoc != null)
            {
                string sourceFileName = activeDoc.FullName;
                string fileName = activeDoc.Name;
                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
                object routeDocument = true;
 
                activeDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument);
                System.IO.File.Move(sourceFileName, "c:\\temp\\korte\\" + fileName);
            }
        }
 
        public void OnActionTotalAlma(IRibbonControl control)
        {
            Microsoft.Office.Interop.Word.Document activeDoc = applicationObject.ActiveDocument;
            if (activeDoc != null)
            {
                string sourceFileName = activeDoc.FullName;
                string fileName = activeDoc.Name;
                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
                object routeDocument = true;
 
                activeDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument);
                System.IO.File.Move(sourceFileName, "c:\\temp\\alma\\" + fileName);
            }
        }
 
        public void OnActionDelete(IRibbonControl control)
        {
            Microsoft.Office.Interop.Word.Document activeDoc = applicationObject.ActiveDocument;
            if (activeDoc != null)
            {
                string sourceFileName = activeDoc.FullName;
                string fileName = activeDoc.Name;
                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
                object routeDocument = true;
 
                activeDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument);
                System.IO.File.Delete(sourceFileName);
            }
        }
 
        public void OnBeginShutdown(ref Array custom)
	{
	}
 
	private Microsoft.Office.Interop.Word.Application applicationObject;
	private object addInInstance;
	}
}

Ribbon.xml

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon startFromScratch="false">
    <tabs>
      <tab id="tab1" label="Categories" >
        <group id="group1" label="Category group">
          <button id="button1" label="alma"
            size="large" imageMso="HyperlinkInsert"
            onAction="OnActionAlma"
            tag="http://www.microsoft.com" />
          <button id="button2" label="korte"
            size="large" imageMso="HyperlinkInsert"
            onAction="OnActionKorte"
            tag="http://www.microsoft.com" />
          <button id="button3" label="delete"
            size="large" imageMso="HyperlinkInsert"
            onAction="OnActionDelete"
            tag="http://www.microsoft.com" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

A Ribbon.xml fájlt hozzá kell adni az erőforrásokhoz. A SolutionExplorer/MoveDocument - properties menüpontban a Resources fülnél megnyíló ablakra rámozgatva az XML fájlt meg is történt a hozzáadás.