LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
external usenet poster
 
Posts: 3
Default Office COM Add-in by Using Visual C#

Hi,

My requirement is to develop a addin for excel. I read the following
article from MS, did according to it, still I don't get a button on the
toolbar.

Can some genius help ? I have attached the .cs file.

Thank you

Ganesh

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using Microsoft.Office.Core;
using Extensibility;

namespace MyExcelAddIn
{
//[GuidAttribute("8CA56E2C-EAAB-4B0A-ABB2-0E49A2E89251"), ProgId("MyExcelAddIn.Connect")]
[ComVisible(true), Guid("8CA56E2C-EAAB-4B0A-ABB2-0E49A2E89251"), ProgId("MyExcelAddIn.Connect"), ClassInterface(ClassInterfaceType.AutoDual)]
public class Connect : _IDTExtensibility2
{
private CommandBarButton MyButton;

private object applicationObject;
private object addInInstance;

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref object[] custom)
{
System.Windows.Forms.MessageBox.Show("Connected");

applicationObject = application;
addInInstance = addInInst;

if (connectMode != ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}


}

public void OnDisconnection(ext_DisconnectMode disconnectMode, ref object[] custom)
{
if (disconnectMode != ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}

public void OnAddInUpdate(ref object[] custom)
{

}

public void OnBeginShutdown(ref object[] custom)
{
object omissing = System.Reflection.Missing.Value;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
MyButton = null;
}

public void OnStartupComplete(ref object[] custom)
{
CommandBars oCommandBars;
CommandBar oStandardBar;

try
{
oCommandBars = (CommandBars)applicationObject.GetType().InvokeMem ber("CommandBars", BindingFlags.GetProperty, null, applicationObject, null);
}
catch (Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer = applicationObject.GetType().InvokeMember("ActiveEx plorer", BindingFlags.GetProperty, null, applicationObject, null);
oCommandBars = (CommandBars)oActiveExplorer.GetType().InvokeMembe r("CommandBars", BindingFlags.GetProperty, null, oActiveExplorer, null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch (Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];
}
catch (Exception)
{
object omissing = System.Reflection.Missing.Value;
MyButton = (CommandBarButton)oStandardBar.Controls.Add(1, omissing, omissing, omissing, omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "My Custom Button";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
MyButton.OnAction = "!<MyExcelAddIn.Connect";

MyButton.Visible = true;
MyButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_Clic kEventHandler(this.MyButton_Click);


object oName = applicationObject.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, applicationObject, null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " + oName.ToString(), "MyCOMAddin");
oStandardBar = null;
oCommandBars = null;
}

private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was Clicked", "MyCOMAddin");
}


[ComRegisterFunctionAttribute]
public static void Register(Type t)
{
string guid = t.GUID.ToString("B");
string name = t.FullName;
//\Software\Microsoft\Office\OfficeApp\Addins\
RegistryKey rkClass = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Micro soft\Office\Excel\Addins\" + name);
rkClass.SetValue("Description", name);
rkClass.SetValue("FriendlyName", name);
rkClass.SetValue("LoadBehavior", 2);
rkClass.SetValue("CommandLineSafe", 0);
MessageBox.Show(name + " Registered");
}

[ComUnregisterFunctionAttribute]
public static void Unregister(Type t)
{
string guid = t.GUID.ToString("B");
string name = t.FullName;
Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Micro soft\Office\Excel\Addins").DeleteSubKeyTree(name);
MessageBox.Show(name + " Unregistered");
}
}

/// <summary
/// Import COM interface
/// </summary
[ComImport, Guid("B65AD801-ABAF-11D0-BB8B-00A0C90F2744"), InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface _IDTExtensibility2
{

void OnConnection([In, MarshalAs(UnmanagedType.IDispatch)] object application,
[In] ext_ConnectMode connectMode,
[In, MarshalAs(UnmanagedType.IDispatch)] object addInInst,
[In, MarshalAs(UnmanagedType.SafeArray)] ref object[] custom);

void OnDisconnection([In] ext_DisconnectMode disconnectMode,
[In, MarshalAs(UnmanagedType.SafeArray)] ref object[] custom);

void OnAddInUpdate([In, MarshalAs(UnmanagedType.SafeArray)] ref object[] custom);

void OnBeginShutdown([In, MarshalAs(UnmanagedType.SafeArray)] ref object[] custom);

void OnStartupComplete([In, MarshalAs(UnmanagedType.SafeArray)] ref object[] custom);

}

public enum ext_ConnectMode : uint
{
ext_cm_AfterStartup = 0,
ext_cm_Startup = 1,
ext_cm_External = 2,
ext_cm_CommandLine = 3,
ext_cm_Solution = 4,
ext_cm_UISetup = 5
}

public enum ext_DisconnectMode : uint
{
ext_dm_HostShutdown = 0,
ext_dm_UserClosed = 1,
ext_dm_UISetupComplete = 2,
ext_dm_SolutionClosed = 3
}
}

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
changing the visual basic in office 2003 to visual studio net bigdaddy3 Excel Discussion (Misc queries) 1 September 13th 05 10:57 AM
visual studio tools for office Art Excel Programming 1 April 25th 05 04:21 PM
Visual Studio - Office 2003 Art Excel Programming 1 July 15th 04 01:42 PM
Visual Studio Tools for Office Mohan Late Excel Programming 0 October 15th 03 10:53 AM
Visual Studio for Office ?? Damir Sudarevic Excel Programming 1 September 18th 03 04:09 PM


All times are GMT +1. The time now is 02:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"