Shahid Riaz Bhatti

if(my.work == “Interesting” || my.availableTime > my.workHours) { this.blog.Post();}

How to Remove command bar button from Add-Ins

May 04
by Shahid Riaz Bhatti 4. May 2009 10:05
 

Remove command bar from Add-ins in MS Word

 

Some time we need to develop some add-ins to fulfill our requirements in different software (e.g. MS Word). Few days back I was developing an Add-ins for MS word. The requirement was to add a button in a command bar of the MS-Word. I successfully added button in the command bar and also wrote code against the event handler of that button. But unfortunately whenever I ran the MS word, my code started to add the same button in command bar again and again. To overcome this problem I wrote code which forcefully removed that custom button from the command bar on the closing of the ms word.

The code which was removing that button on the closing of the word was in the Application_DocumentBeforeClose event of Add-in. But that code didn’t give me the expected output.

There was nothing wrong in the code, because I debugged the code and in the shutdown event I made sure that the command button exists in the command bar. If it exists then delete the button. But again that code didn’t work. And interesting thing was that the code segment which was checking that the command button exists in the command bar always gave me true and in the true section I was deleting that button, but I don’t know that why it didn’t work. I was deleting the command button by using the ID of the button something like this:

1.     Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)

2.     this.Application.CommandBars.ActiveMenuBar.FindControl(Office.

3.     MsoControlType.msoControlPopup, System.Type.Missing, IDOfControl, true, true );

4.     if (foundMenu != null)

5.     {

6.       foundMenu.Delete(false);

7.     }

The above code didn’t work L 

In line 3, I used the IDOfControl to find the command bar button, but it didn’t find that control. Though I tried to use the Id.delete() method, but it also didn’t work for me. Then I changed the Line number 3 and instead of using the ID of the control to find the command bar button, I used the tag of that control and this change in code successfully find the control and this change also make the Line 6 to give the expected output.  So if you are also facing this kind of situation then try to use the tag of the button instead of its ID. I am sure it will work. Well I was developing the add-in for office 2007. This code will probably work in other version of MS word too. The reason that this code work is given below: 

Add-in Express identifies all its controls (commandbar controls) through the use of the ControlTag property (the Tag property of the CommandBarControl interface). The value of this property is generated automatically and you don’t need to change it. For your own needs, use the Tag property instead.       

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | C# | Tips and Tricks | Tips and Tricks

Comments

Add comment


(Will show your Gravatar icon)  

Enter the word
9420


biuquote
  • Comment
  • Preview
Loading



RecentComments

Comment RSS

Most comments

supplynflshop supplynflshop
51 comments
tiffany-bracelets tiffany-bracelets
39 comments
AVI to iPad AVI to iPad
36 comments