From 38392fa441ea8b3ba89b3a397aad33efcae81d7d Mon Sep 17 00:00:00 2001 From: Benoit Patra Date: Tue, 26 Aug 2014 16:17:37 +0200 Subject: [PATCH 1/4] remove dead code --- .../ExcelDna.Integration/ExcelCommandBars.cs | 48 ------------------- 1 file changed, 48 deletions(-) diff --git a/Source/ExcelDna.Integration/ExcelCommandBars.cs b/Source/ExcelDna.Integration/ExcelCommandBars.cs index f872effe..2bfd8ae6 100644 --- a/Source/ExcelDna.Integration/ExcelCommandBars.cs +++ b/Source/ExcelDna.Integration/ExcelCommandBars.cs @@ -38,29 +38,6 @@ namespace ExcelDna.Integration.CustomUI { public delegate Bitmap GetImageDelegate(string imageName); - //public class ExcelCommandBars - //{ - // public ExcelCommandBars() - // { - // } - - // public virtual string GetCustomUI() - // { - // } - - // public virtual object GetImage(string imageName) - // { - - // } - - // public CommandBarControls Controls - // { - // get - // { - // } - // } - //} - public static class ExcelCommandBarUtil { // List of loaded CustomUI @@ -571,16 +548,6 @@ public int Count return Convert.ToInt32(i); } } - - //public event EventHandler OnUpdate - //{ - // add - // { - // } - // remove - // { - // } - //} } public class CommandBarControl @@ -941,11 +908,6 @@ public void RemovePopup(string name) } } - //public class CommandBarButtonClickEventArgs : EventArgs - //{ - // public bool CancelDefault; - //} - public class CommandBarButton : CommandBarControl { internal CommandBarButton(object commandBarCom) @@ -1003,16 +965,6 @@ public string ShortcutText ComObjectType.InvokeMember("ShortcutText", BindingFlags.SetProperty, null, ComObject, new object[] { value }); } } - - //public event EventHandler Click - //{ - // add - // { - // } - // remove - // { - // } - //} } public class CommandBarPopup : CommandBarControl From 087b61b30af2e6976635c4f6cccc3bd8bb0343cf Mon Sep 17 00:00:00 2001 From: Benoit Patra Date: Tue, 26 Aug 2014 16:21:17 +0200 Subject: [PATCH 2/4] refactoring: use method for retrieving CommandBar from the CommandBars list from name --- .../ExcelDna.Integration/ExcelCommandBars.cs | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/Source/ExcelDna.Integration/ExcelCommandBars.cs b/Source/ExcelDna.Integration/ExcelCommandBars.cs index 2bfd8ae6..7ad8e8c9 100644 --- a/Source/ExcelDna.Integration/ExcelCommandBars.cs +++ b/Source/ExcelDna.Integration/ExcelCommandBars.cs @@ -122,15 +122,7 @@ private static void AddCommandBarControls(Application excelApp, XmlNodeList xmlN if (childNode.Name == "commandBar") { string barName = childNode.Attributes["name"].Value; - CommandBar bar = null; - for (int i = 1; i <= excelApp.CommandBars.Count; i++) - { - if (excelApp.CommandBars[i].Name == barName) - { - bar = excelApp.CommandBars[i]; - break; - } - } + CommandBar bar = GetCommandBarFromName(excelApp, barName); if (bar != null) { AddControls(bar.Controls, childNode.ChildNodes, getImage); @@ -165,15 +157,7 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x if (childNode.Name == "commandBar") { string barName = childNode.Attributes["name"].Value; - CommandBar bar = null; - for (int i = 1; i <= excelApp.CommandBars.Count; i++) - { - if (excelApp.CommandBars[i].Name == barName) - { - bar = excelApp.CommandBars[i]; - break; - } - } + CommandBar bar = GetCommandBarFromName(excelApp, barName); if (bar != null) { RemoveControls(bar.Controls, childNode.ChildNodes); @@ -187,6 +171,20 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x } } + private static CommandBar GetCommandBarFromName(Application excelApp, string barName) + { + CommandBar bar = null; + for (int i = 1; i <= excelApp.CommandBars.Count; i++) + { + if (excelApp.CommandBars[i].Name == barName) + { + bar = excelApp.CommandBars[i]; + break; + } + } + return bar; + } + private static void AddControls(CommandBarControls parentControls, XmlNodeList xmlNodes, GetImageDelegate getImage) { foreach (XmlNode childNode in xmlNodes) From b4bae3fc543db3f6eab443b9b322fd273226e5a6 Mon Sep 17 00:00:00 2001 From: Benoit Patra Date: Tue, 26 Aug 2014 16:42:58 +0200 Subject: [PATCH 3/4] Add the possibility to recover a command bar using its ID. --- .../ExcelDna.Integration/ExcelCommandBars.cs | 57 +++++++++++++++---- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/Source/ExcelDna.Integration/ExcelCommandBars.cs b/Source/ExcelDna.Integration/ExcelCommandBars.cs index 7ad8e8c9..95911a61 100644 --- a/Source/ExcelDna.Integration/ExcelCommandBars.cs +++ b/Source/ExcelDna.Integration/ExcelCommandBars.cs @@ -121,8 +121,8 @@ private static void AddCommandBarControls(Application excelApp, XmlNodeList xmlN { if (childNode.Name == "commandBar") { - string barName = childNode.Attributes["name"].Value; - CommandBar bar = GetCommandBarFromName(excelApp, barName); + string barName; + CommandBar bar = GetCommandBarFromIdOrName(excelApp, childNode.Attributes, out barName); if (bar != null) { AddControls(bar.Controls, childNode.ChildNodes, getImage); @@ -156,8 +156,8 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x { if (childNode.Name == "commandBar") { - string barName = childNode.Attributes["name"].Value; - CommandBar bar = GetCommandBarFromName(excelApp, barName); + string barName; + CommandBar bar = GetCommandBarFromIdOrName(excelApp, childNode.Attributes, out barName); if (bar != null) { RemoveControls(bar.Controls, childNode.ChildNodes); @@ -171,18 +171,44 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x } } - private static CommandBar GetCommandBarFromName(Application excelApp, string barName) + //We cannot rely only on name to recover the proper CommandBar so we have the possibility to use the ID (which is used in priority). + //Indeed there are two CommandBar for "Cell" see http://msdn.microsoft.com/en-us/library/office/gg469862(v=office.14).aspx + //However, at the time of the writing there is a mistake: "Application.CommandBars(Application.CommandBars("Cell").Index + 3)" is false in practice + private static CommandBar GetCommandBarFromIdOrName(Application excelApp,XmlAttributeCollection nodeAttributes, out string barName) { - CommandBar bar = null; - for (int i = 1; i <= excelApp.CommandBars.Count; i++) + var id = nodeAttributes["id"]; + var name = nodeAttributes["name"]; + if(name ==null) throw new ArgumentException("commandBar attributes must contain name"); + + barName = name.Value; + if (id != null) { - if (excelApp.CommandBars[i].Name == barName) + string barId = id.Value; + CommandBar bar = null; + for (int i = 1; i <= excelApp.CommandBars.Count; i++) { - bar = excelApp.CommandBars[i]; - break; + if (excelApp.CommandBars[i].Id == barId) + { + bar = excelApp.CommandBars[i]; + break; + } } + return bar; + } + else + { + + CommandBar bar = null; + for (int i = 1; i <= excelApp.CommandBars.Count; i++) + { + if (excelApp.CommandBars[i].Name == barName) + { + bar = excelApp.CommandBars[i]; + break; + } + } + return bar; } - return bar; } private static void AddControls(CommandBarControls parentControls, XmlNodeList xmlNodes, GetImageDelegate getImage) @@ -470,6 +496,15 @@ public string Name } } + public string Id + { + get + { + object controls = ComObjectType.InvokeMember("Id", BindingFlags.GetProperty, null, ComObject, null); + return controls.ToString(); + } + } + public bool Visible { get From 710e909a042629c7c4de3d5e96b93ba81fdce48c Mon Sep 17 00:00:00 2001 From: Benoit Patra Date: Tue, 26 Aug 2014 16:42:58 +0200 Subject: [PATCH 4/4] CommandBar using its ID: - Id property part of CommandBarControl - Only CommandBarPopup objects have an Id when looking for CommandBar with a given Id --- .../ExcelDna.Integration/ExcelCommandBars.cs | 80 ++++++++++++++++--- 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/Source/ExcelDna.Integration/ExcelCommandBars.cs b/Source/ExcelDna.Integration/ExcelCommandBars.cs index 7ad8e8c9..4b85391b 100644 --- a/Source/ExcelDna.Integration/ExcelCommandBars.cs +++ b/Source/ExcelDna.Integration/ExcelCommandBars.cs @@ -121,8 +121,8 @@ private static void AddCommandBarControls(Application excelApp, XmlNodeList xmlN { if (childNode.Name == "commandBar") { - string barName = childNode.Attributes["name"].Value; - CommandBar bar = GetCommandBarFromName(excelApp, barName); + string barName; + CommandBar bar = GetCommandBarFromIdOrName(excelApp, childNode.Attributes, out barName); if (bar != null) { AddControls(bar.Controls, childNode.ChildNodes, getImage); @@ -156,8 +156,8 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x { if (childNode.Name == "commandBar") { - string barName = childNode.Attributes["name"].Value; - CommandBar bar = GetCommandBarFromName(excelApp, barName); + string barName; + CommandBar bar = GetCommandBarFromIdOrName(excelApp, childNode.Attributes, out barName); if (bar != null) { RemoveControls(bar.Controls, childNode.ChildNodes); @@ -171,18 +171,50 @@ private static void RemoveCommandBarControls(Application excelApp, XmlNodeList x } } - private static CommandBar GetCommandBarFromName(Application excelApp, string barName) + //We cannot rely only on name to recover the proper CommandBar so we have the possibility to use the ID (which is used in priority). + //Indeed there are two CommandBar for "Cell" see http://msdn.microsoft.com/en-us/library/office/gg469862(v=office.14).aspx + //However, at the time of the writing there is a mistake: "Application.CommandBars(Application.CommandBars("Cell").Index + 3)" is false in practice + // Note that the Id property is only available + private static CommandBar GetCommandBarFromIdOrName(Application excelApp, XmlAttributeCollection nodeAttributes, out string barName) { - CommandBar bar = null; - for (int i = 1; i <= excelApp.CommandBars.Count; i++) + var id = nodeAttributes["id"]; + var name = nodeAttributes["name"]; + if(name ==null) throw new ArgumentException("commandBar attributes must contain name"); + + barName = name.Value; + if (id != null) { - if (excelApp.CommandBars[i].Name == barName) + string barId = id.Value; + CommandBar bar = null; + for (int i = 1; i <= excelApp.CommandBars.Count; i++) { - bar = excelApp.CommandBars[i]; - break; + var currentBar = excelApp.CommandBars[i]; + if (currentBar.Type == MsoBarType.msoBarTypePopup) + { + var currentBarPopup = new CommandBarPopup(currentBar.GetComObject()); + if (currentBarPopup.Id == barId) + { + bar = currentBar; + break; + } + } + } + return bar; + } + else + { + + CommandBar bar = null; + for (int i = 1; i <= excelApp.CommandBars.Count; i++) + { + if (excelApp.CommandBars[i].Name == barName) + { + bar = excelApp.CommandBars[i]; + break; + } } + return bar; } - return bar; } private static void AddControls(CommandBarControls parentControls, XmlNodeList xmlNodes, GetImageDelegate getImage) @@ -482,6 +514,14 @@ public bool Visible } } + public MsoBarType Type + { + get + { + return (MsoBarType)ComObjectType.InvokeMember("Type", BindingFlags.GetProperty, null, ComObject, null); + } + } + public CommandBarControl FindControl(object type, object id, object tag, object visible, object recursive) { object result = ComObjectType.InvokeMember("FindControl", BindingFlags.InvokeMethod, null, ComObject, new object[] { type, id, tag, visible, recursive }); @@ -742,7 +782,16 @@ public int Index return (int)ComObjectType.InvokeMember("Index", BindingFlags.GetProperty, null, ComObject, null); } } - + + public string Id + { + get + { + object controls = ComObjectType.InvokeMember("Id", BindingFlags.GetProperty, null, ComObject, null); + return controls.ToString(); + } + } + public void Delete(object Temporary) { ComObjectType.InvokeMember("Delete", BindingFlags.InvokeMethod, null, ComObject, new object[] { Temporary }); @@ -1049,4 +1098,11 @@ public enum MsoControlType msoControlWorkPane = 25, msoControlAutoCompleteCombo = 26, } + + public enum MsoBarType + { + msoBarTypeNormal = 0, + msoBarTypeMenuBar = 1, + msoBarTypePopup = 2, + } }