Creating CommandBar at runtime?

Add-in Express™ Support Service
That's what is more important than anything else

Creating CommandBar at runtime?
 
Steve Weixel




Posts: 14
Joined: 2005-05-04
I'm trying to make an addin that adds a popup to the main menu in Word and Excel. I saw another forum post that said to create a ADX command bar with the built-in command bar name, and that works, for only one or the other because the names are different between Word and Excel ("Menu Bar" vs. "Worksheet Menu Bar"). I was thinking that if I could just create the command bar at runtime I could determine which name to use based on the application, but I can't get it to actually work. I can't even get any command bar to add at runtime. Am I just missing something or is it not possible? How else could I have an item on the menu (e.g. Tools) in both Excel and Word?

Thanks,
Steve
Posted 04 May, 2005 18:58:45 Top
Steve Weixel




Posts: 14
Joined: 2005-05-04
I should add that I'm using 2.1 because I can't get into the premium area to get the 2.2 preview :(
Posted 04 May, 2005 19:01:30 Top
Aleksey Zhigar


Add-in Express team


Posts: 28
Joined: 2005-02-03
To get the 2.2 preview the premium area is not necessary
http://www.add-in-express.com/downloads/

To determine a host application you are to use the HostType property of the AddinModule.
E.g. see the code below

		
private void AddinModule_AddinInitialize(object sender, System.EventArgs e)
{			
  switch(this.HostType)       
  {         
    case AddinExpress.MSO.ADXOfficeHostApp.ohaExcel:   
      adxCommandBar1.CommandBarName = "Worksheet Menu Bar";	
    break;                  

    case AddinExpress.MSO.ADXOfficeHostApp.ohaWord:            
      adxCommandBar1.CommandBarName = "Menu Bar";				
    break;                  
    }
}
Posted 05 May, 2005 05:35:03 Top
Steve Weixel




Posts: 14
Joined: 2005-05-04
Thanks, that did it. That solves my immediate issue, but I would still like to know if it is possible to create a commandbar at runtime or can it only be done in the designer?
Posted 05 May, 2005 17:39:37 Top
Aleksey Zhigar


Add-in Express team


Posts: 28
Joined: 2005-02-03
At runtime you can create any number of Commandbars in the AddinInitialize event.
Posted 06 May, 2005 06:40:12 Top
Steve Weixel




Posts: 14
Joined: 2005-05-04
I see now what was happening... I did not have any controls in the designed, so the System.ComponentModel.Container object was not being created, and thus when I tried to create a commandbar at runtime it did nothing. As long as I have a container object it works.
Posted 09 May, 2005 15:04:54 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Steve.

You can return the System.ComponentModel.Container object in the GetContainer method of the AddinModule. See the "ADX automatic code" region of the AddinModule. Then in the AddinInitialize event handler you can create your command bars at runtime.
Posted 10 May, 2005 16:22:39 Top
Steve Weixel




Posts: 14
Joined: 2005-05-04
Hy Segey,

Yes, the problem is that in in InitializeComponent, the line "Me.components = New System.ComponentModel.Container" is removed if you add controls (e.g. commandbar) in the designer, then remove the controls so that there is nothing in the designer. Therefore the function GetContainer returns "components" which was never constructed. When constructing the commandbar in AddinInitialize, I do pass it GetContainer, but being nothing it causes an exception. My current solution is to make sure that I have a control in the designer, but I could just as well have constructed the "components" container in my constructor.

My add-in is a VB.net project if that helps you track down the probelm, as you must have different code generation for each language and you may not have the problem with c# projects. My issue is resolved as far as I'm concerned but I like to leave a full record of the problem and solution/workaround on forums to help others should they stumble across it.

Thanks,
Steve
Posted 11 May, 2005 12:20:47 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Thank you Steve for your help. I think we will add some methods in ADX to manage command bars and controls at runtime.

Posted 11 May, 2005 13:10:28 Top
Sven Heitmann




Posts: 193
Joined: 2004-08-23
Steve, you can also add your code to the constructor directly.
InitializeCpmonent is just for the designer.
Best regards,

Sven Heitmann
Posted 12 May, 2005 04:08:10 Top