Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommandBar by ID #1

Closed
wants to merge 5 commits into from
Closed

CommandBar by ID #1

wants to merge 5 commits into from

Conversation

bpatra
Copy link

@bpatra bpatra commented Aug 26, 2014

There are two CommandBar with name "Cell": one for the usual right click on a cell and one for the "PageBreakView" see this article. When passing to the method ExcelCommandBarUtil.LoadCommandBar an xml argument of the form

<commandBars xmlns='http://schemas.excel-dna.net/office/2003/01/commandbars' > 
  <commandBar name='Cell'>
  <button caption='Hello' enabled='true' onAction='SayHello'/>
  </commandBar>
</commandBars>"

the first CommandBar whose name is "Cell" is returned. We have found situations where this first CommandBar is the "Cell" for PageBreakView and not the usual one.

In this pull request we add the possibility to specify the id of the CommandBar to avoid such situations. If an attribute id is present then it will be used to retrieve the CommandBar instead of name. If no id attribute is present then the name is used as a fallback (to avoid breaking existing code). Note that the attribute name is mandatory in both cases.

Now if you want to retrieve the usual "Cell" CommandBar you can use its id 424.

<commandBars xmlns='http://schemas.excel-dna.net/office/2003/01/commandbars' > 
  <commandBar name='Cell' id='424'>
  <button caption='Hello' enabled='true' onAction='SayHello'/>
  </commandBar>
</commandBars>"

@govert
Copy link
Member

govert commented Aug 26, 2014

Thank you - I'll have a look.

@govert
Copy link
Member

govert commented Sep 30, 2014

Hi Benoit,
I'm a bit confused by the CommandBar.Id property that you're checking. Looking in the COM object model reference, it doesn't look like CommandBar actually has this property, rather CommandBarControl has an Id. I guess some CommandBars are actually CommandBarPopup controls, so they might have the Id property that way.
Do you think there's a way to put the Id property on CommandBarControl, which seems more correct, and still have your Id-lookup functionality?
-Govert

@adefalque
Copy link

Hi Govert,

Following your comment, and since I had the occasion to work with Benoit, I propose you the following modification to Benoit's code.

  • Id property moved to CommandBarControl class
  • Expose the Type property on CommandBar:class
        public MsoBarType Type
        {
            get
            {
                return (MsoBarType)ComObjectType.InvokeMember("Type", BindingFlags.GetProperty, null, ComObject, null);
            }
        }
  • Changed GetCommandBarFromIdOrName code to:
...
            if (id != null)
            {
                string barId = id.Value;
                CommandBar bar = null;
                for (int i = 1; i <= excelApp.CommandBars.Count; i++)
                {
                    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;
            }
...

Does it look ok to you?

Alexandre

@govert
Copy link
Member

govert commented Oct 10, 2014

Thanks Alexandre - I think that will be great.
I'll let you know when I've added it in the main repository on CodePlex.

bpatra and others added 2 commits October 13, 2014 08:58
- Id property part of CommandBarControl
- Only CommandBarPopup objects have an Id when looking for CommandBar with a given Id
…into commandBarByID

Conflicts:
	Source/ExcelDna.Integration/ExcelCommandBars.cs
@bpatra
Copy link
Author

bpatra commented Oct 13, 2014

The remote branch commandBarByID benefits from these changes merging the pull request now should take the five commit the last one being made by Alexandre..

@govert
Copy link
Member

govert commented Mar 22, 2015

OK - this is finally integrated. Sorry it took so long - I'm moving the main repository over to GitHub at last.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants