Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
|
|||
|
|||
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 } } |
#2
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
|
|||
|
|||
Office COM Add-in by Using Visual C#
Martin Fishlock wrote:
Ganesh, You cannot attach file pls paste it. Also what was the reference. Sorry 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 } } |
#3
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
|
|||
|
|||
Office COM Add-in by Using Visual C#
Hi Ganesh,
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. With which version of Excel are you testing? Have you tried adding any debugging information so that you can trace the path the code is following when Excel loads the Add-in (so that you can determine where it might be failing)? Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005) http://www.word.mvps.org This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-) |
#4
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
|
|||
|
|||
Office COM Add-in by Using Visual C#
Hi Madiya,
If we can not attach a file to the post (as usual), why there is link in the original post of Ganesh? I clicked on the link and got the prompt to download the file. Has Google changed the policy? I am using IE5.5 to view all the post on google groups. Whether or not you can attach a file depends on the interface you use to connect to the newsgroups. And please note that these newsgroups are NOT hosted by Google, but by Microsoft :-) Google just provides a (very good) search engine and archives for the Microsoft newsgroups. It's important to keep in mind that many people using off-line newsreaders (rather than a web interface) filter out messages with attachments. It's often better to upload pictures or files to a website, then provide a link to that. Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005) http://www.word.mvps.org This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-) |
#5
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
|
|||
|
|||
Office COM Add-in by Using Visual C#
Cindy,
Thanks for reply. You mean to say that if we use the ng at microsoft directly (web interface), we can attach the files. Am I correct or I misunderstood you. Just curios. Regards, Madiya Cindy M. wrote: Hi Madiya, If we can not attach a file to the post (as usual), why there is link in the original post of Ganesh? I clicked on the link and got the prompt to download the file. Has Google changed the policy? I am using IE5.5 to view all the post on google groups. Whether or not you can attach a file depends on the interface you use to connect to the newsgroups. And please note that these newsgroups are NOT hosted by Google, but by Microsoft :-) Google just provides a (very good) search engine and archives for the Microsoft newsgroups. It's important to keep in mind that many people using off-line newsreaders (rather than a web interface) filter out messages with attachments. It's often better to upload pictures or files to a website, then provide a link to that. Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005) http://www.word.mvps.org This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-) |
#6
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
|
|||
|
|||
Office COM Add-in by Using Visual C#
Hi Madiya,
You mean to say that if we use the ng at microsoft directly (web interface), we can attach the files. Am I correct or I misunderstood you. Just curios. No, the web interface on the micrsoft site doesn't provide this. Most off-line newsreaders probably would (mine does, for example and so does Outlook Express I believe). Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005) http://www.word.mvps.org This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-) |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
changing the visual basic in office 2003 to visual studio net | Excel Discussion (Misc queries) | |||
visual studio tools for office | Excel Programming | |||
Visual Studio - Office 2003 | Excel Programming | |||
Visual Studio Tools for Office | Excel Programming | |||
Visual Studio for Office ?? | Excel Programming |