<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Office Development with Visual Studio : Word 2003</title><link>http://blogs.msdn.com/vsto/archive/tags/Word+2003/default.aspx</link><description>Tags: Word 2003</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Clearing Off Custom Menu Items in Word (Norm Estabrook)</title><link>http://blogs.msdn.com/vsto/archive/2009/04/14/clearing-off-custom-menu-items-in-word-norm-estabrook.aspx</link><pubDate>Tue, 14 Apr 2009 21:37:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9548974</guid><dc:creator>VSTO Team</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/vsto/comments/9548974.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vsto/commentrss.aspx?PostID=9548974</wfw:commentRss><description>&lt;p&gt;Last month I posted &lt;a href="http://blogs.msdn.com/vsto/archive/2009/03/06/my-word-add-in-creates-duplicate-menu-items-make-it-stop-norm-estabrook.aspx"&gt;this article&lt;/a&gt; that described how to prevent your add-in from creating duplicate menu items in Word.&amp;#160; If you have been experimenting with customization contexts, you might have several menu items that appear when you right click a document. The article that I posted shows how to prevent this from happening for your users, but what about removing the items that appear in your instance of Word – the one that you use for testing? &lt;/p&gt;  &lt;p&gt;To clear those off, just add a bit of code to the startup event handler of any old Word add-in.&amp;#160; Set the customization context to each possible culprit (template, document, attached template etc.) and then call the &lt;strong&gt;Reset&lt;/strong&gt; method. Be sure to save the template or document after words.&amp;#160; &lt;/p&gt;  &lt;p&gt;Note - I wouldn’t recommend that you put this code into an add-in that you send out to users as this code will remove all customizations in each context (Even ones that your add-in has not created!).&amp;#160; However, it is a cool way to clear up left over menu items from the instance of Word that you use for testing.&lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;ResetShortcutMenu()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CustomizationContext = myApplication.ActiveDocument;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;].Reset();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.ActiveDocument.Save();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CustomizationContext = myApplication.ActiveDocument.get_AttachedTemplate();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;].Reset();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ((Word.&lt;span style="color: #2b91af"&gt;Template&lt;/span&gt;)myApplication.ActiveDocument.get_AttachedTemplate()).Save();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CustomizationContext = customTemplate;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;].Reset();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; customTemplate.Save();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CustomizationContext = myApplication.NormalTemplate;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;].Reset();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; myApplication.NormalTemplate.Save();     &lt;br /&gt;}&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9548974" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vsto/archive/tags/VSTO/default.aspx">VSTO</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Norm+Estabrook/default.aspx">Norm Estabrook</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Office+2007/default.aspx">Office 2007</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Office+2003/default.aspx">Office 2003</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Word+Object+Model/default.aspx">Word Object Model</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Word+2007/default.aspx">Word 2007</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Word+2003/default.aspx">Word 2003</category><category domain="http://blogs.msdn.com/vsto/archive/tags/VS2008/default.aspx">VS2008</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Office+Development/default.aspx">Office Development</category></item><item><title>My Word Add-In Creates Duplicate Menu Items. Make it Stop! (Norm Estabrook)</title><link>http://blogs.msdn.com/vsto/archive/2009/03/06/my-word-add-in-creates-duplicate-menu-items-make-it-stop-norm-estabrook.aspx</link><pubDate>Fri, 06 Mar 2009 20:33:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9463406</guid><dc:creator>VSTO Team</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/vsto/comments/9463406.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vsto/commentrss.aspx?PostID=9463406</wfw:commentRss><description>&lt;p&gt;So I want my add-in to place a custom command into the shortcut menu. The shortcut menu is that cool menu that appears when you right-click a document. Great, so I read some articles in MSDN, write some code, run the add-in and voila there it is! I give it to my buddy, he is proud of my accomplishment and and installs my add-in.&amp;#160; Now he hates me because every time he opens up Word, a duplicate menu appears.&amp;#160; Where did I go wrong? &lt;/p&gt;  &lt;p&gt;Well actually, I didn’t do anything wrong.&amp;#160; It’s just that Word requires a little more attention when it comes to handling menus. I guess you can say that Word is a bit more “needy” than other Office applications. But being “higher maintenance” does not have to mean “higher maintenance costs”.&amp;#160; Hopefully this post will get your friend talking to you again.&lt;/p&gt;  &lt;h3&gt;My code&lt;/h3&gt;  &lt;p&gt;Here is the code that did not work for me.&amp;#160; BTW – I will paste in both &lt;font color="#808080"&gt;Visual Basic&lt;/font&gt; and &lt;font color="#808080"&gt;C#&lt;/font&gt; examples for this post.&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;[Visual Basic]&lt;/font&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Private &lt;/span&gt;MyApplication &lt;span style="color: blue"&gt;As &lt;/span&gt;Word.Application
&lt;span style="color: blue"&gt;Private WithEvents &lt;/span&gt;myControl &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.CommandBarButton

&lt;span style="color: blue"&gt;Private Sub &lt;/span&gt;ThisAddIn_Startup _
(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;sender &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;e &lt;span style="color: blue"&gt;As &lt;/span&gt;System.EventArgs) &lt;span style="color: blue"&gt;Handles Me&lt;/span&gt;.Startup

    MyApplication = &lt;span style="color: blue"&gt;Me&lt;/span&gt;.Application
    AddMenuItem()

&lt;span style="color: blue"&gt;End Sub

Private Sub &lt;/span&gt;AddMenuItem()

    &lt;span style="color: blue"&gt;Dim &lt;/span&gt;menuItem &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.MsoControlType = _
        Office.MsoControlType.msoControlButton

    myControl = &lt;span style="color: blue"&gt;CType&lt;/span&gt;(MyApplication.CommandBars(&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;).Controls.Add _
       (menuItem, 1, &lt;span style="color: blue"&gt;True&lt;/span&gt;), Office.CommandBarButton)

    myControl.Style = Office.MsoButtonStyle.msoButtonCaption
    myControl.Caption = &lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item&amp;quot;
    &lt;/span&gt;myControl.Tag = &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;

&lt;/span&gt;&lt;span style="color: blue"&gt;End Sub


Sub &lt;/span&gt;myControl_Click(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;Ctrl &lt;span style="color: blue"&gt;As &lt;/span&gt;Microsoft.Office.Core.CommandBarButton, _
                    &lt;span style="color: blue"&gt;ByRef &lt;/span&gt;CancelDefault &lt;span style="color: blue"&gt;As Boolean&lt;/span&gt;) &lt;span style="color: blue"&gt;Handles &lt;/span&gt;myControl.Click

    System.Windows.Forms.MessageBox.Show(&lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item clicked&amp;quot;&lt;/span&gt;)

&lt;span style="color: blue"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[C#]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;Word.&lt;span style="color: #2b91af"&gt;Application &lt;/span&gt;myApplication;
&lt;span style="color: blue"&gt;private &lt;/span&gt;Office.&lt;span style="color: #2b91af"&gt;CommandBarButton &lt;/span&gt;myControl;

&lt;span style="color: blue"&gt;private void &lt;/span&gt;ThisAddIn_Startup(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, System.&lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
{
    myApplication = &lt;span style="color: blue"&gt;this&lt;/span&gt;.Application;
    AddMenuItem(); 
}
 
&lt;span style="color: blue"&gt;private void &lt;/span&gt;AddMenuItem()
{
   Office.&lt;span style="color: #2b91af"&gt;MsoControlType &lt;/span&gt;menuItem = 
        Office.&lt;span style="color: #2b91af"&gt;MsoControlType&lt;/span&gt;.msoControlButton;

    myControl = 
        (Office.&lt;span style="color: #2b91af"&gt;CommandBarButton&lt;/span&gt;)myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;].Controls.Add
        (menuItem,missing, missing, 1, &lt;span style="color: blue"&gt;true&lt;/span&gt;);

    myControl.Style = Office.&lt;span style="color: #2b91af"&gt;MsoButtonStyle&lt;/span&gt;.msoButtonCaption;
    myControl.Caption = &lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item&amp;quot;&lt;/span&gt;;
    myControl.Tag = &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;&lt;/span&gt;;

    myControl.Click += 
        &lt;span style="color: blue"&gt;new &lt;/span&gt;Microsoft.Office.Core.&lt;span style="color: #2b91af"&gt;_CommandBarButtonEvents_ClickEventHandler
            &lt;/span&gt;(myControl_Click);

}

&lt;span style="color: blue"&gt;void &lt;/span&gt;myControl_Click(Microsoft.Office.Core.&lt;span style="color: #2b91af"&gt;CommandBarButton &lt;/span&gt;Ctrl, 
    &lt;span style="color: blue"&gt;ref bool &lt;/span&gt;CancelDefault)
{
    System.Windows.Forms.&lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item clicked&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;

&lt;h3&gt;Why my code does not work as expected&lt;/h3&gt;

&lt;p&gt;Here is one issue I can see right off the bat. Note this line of code for adding a control:&lt;/p&gt;

&lt;p&gt;myControl = &lt;span style="color: blue"&gt;CType&lt;/span&gt;(MyApplication.CommandBars(&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;).Controls.Add _ (menuItem, 1, &lt;span style="color: blue"&gt;True&lt;/span&gt;), Office.CommandBarButton) &lt;/p&gt;

&lt;p&gt;I set the last parameter of the &lt;strong&gt;Add &lt;/strong&gt;method to &lt;strong&gt;True&lt;/strong&gt;.&amp;#160; This value specifies that I want my control to be temporary. I am trying to tell Word &lt;strong&gt;not&lt;/strong&gt; to save the control so that duplicate menu items won’t be added every time Word opens.&amp;#160; Only there is a problem here.&amp;#160; Word &lt;strong&gt;ignores&lt;/strong&gt; this parameter (at least for controls in a CommandBar collection anyway). So I can keep it set to &lt;strong&gt;true&lt;/strong&gt;, but it really means &lt;strong&gt;false&lt;/strong&gt;. Lovely.&lt;/p&gt;

&lt;p&gt;So what is happening? Well, Word is actually saving your new menu command to the Normal.dot template every time a new instance of Word opens – hence the duplicates.&lt;/p&gt;

&lt;h3&gt;What can I do about this?&lt;/h3&gt;

&lt;p&gt;There are probably a billion creative ways to stop the duplicate menus from appearing, but here are &lt;u&gt;three&lt;/u&gt; tips that work really well. Here they are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Check for duplicates &lt;strong&gt;before &lt;/strong&gt;adding an item (control) to a menu. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Always&lt;/strong&gt; set the customization context of the application to the same document or template before adding or deleting a control. &lt;/li&gt;

  &lt;li&gt;Because there are no temporary commands in Word, &lt;strong&gt;use a custom template&lt;/strong&gt; to save the commands. &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Tip # 1: Check for Duplicates:&lt;/h4&gt;

&lt;p&gt;This one is pretty easy.&amp;#160; Just add code to your add-in that looks for a control that has the same &lt;strong&gt;tag&lt;/strong&gt; as the control you are about to add.&amp;#160; If one exists, perform one of the following actions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Don’t add the control (It is already there). &lt;/li&gt;

  &lt;li&gt;Delete the control. Then you can add it. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I modified my code to &lt;strong&gt;delete&lt;/strong&gt; the control. Here is my code.&lt;/p&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[Visual Basic]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Private Sub &lt;/span&gt;RemoveExistingMenuItem()

    &lt;span style="color: blue"&gt;Dim &lt;/span&gt;contextMenu &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.CommandBar = _
    MyApplication.CommandBars(&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;)

    MyApplication.CustomizationContext = customTemplate

    &lt;span style="color: blue"&gt;Dim &lt;/span&gt;control &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.CommandBarButton = contextMenu.FindControl _
        (Office.MsoControlType.msoControlButton, System.Type.Missing, _
         &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;True&lt;/span&gt;, &lt;span style="color: blue"&gt;True&lt;/span&gt;)

    &lt;span style="color: blue"&gt;If Not &lt;/span&gt;(control &lt;span style="color: blue"&gt;Is Nothing&lt;/span&gt;) &lt;span style="color: blue"&gt;Then
        &lt;/span&gt;control.Delete(&lt;span style="color: blue"&gt;True&lt;/span&gt;)
    &lt;span style="color: blue"&gt;End If

End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[C#]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;RemoveExistingMenuItem()
{
    Office.&lt;span style="color: #2b91af"&gt;CommandBar &lt;/span&gt;contextMenu = myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;];
    myApplication.CustomizationContext = customTemplate;

    Office.&lt;span style="color: #2b91af"&gt;CommandBarButton &lt;/span&gt;control = 
        (Office.&lt;span style="color: #2b91af"&gt;CommandBarButton&lt;/span&gt;)contextMenu.FindControl
        (Office.&lt;span style="color: #2b91af"&gt;MsoControlType&lt;/span&gt;.msoControlButton, missing,
        &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;true&lt;/span&gt;, &lt;span style="color: blue"&gt;true&lt;/span&gt;);

    &lt;span style="color: blue"&gt;if &lt;/span&gt;((control != &lt;span style="color: blue"&gt;null&lt;/span&gt;))
    {
        control.Delete(&lt;span style="color: blue"&gt;true&lt;/span&gt;);
    }

}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h4&gt;Tip #2: Set the Customization Context&lt;/h4&gt;

&lt;p&gt;The customization context of the application tells Word where to save your customizations. To specify the customization context, set the &lt;a href="http://msdn.microsoft.com/en-us/library/aa205536(office.10).aspx"&gt;CustomizationContext&lt;/a&gt; property of the &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._application(VS.80).aspx"&gt;Application&lt;/a&gt; object.&lt;/p&gt;

&lt;p&gt;By default, Word uses the &lt;strong&gt;Normal.dot&lt;/strong&gt; template as it’s customization context.&amp;#160; This is not reliable and can change. If you do not explicitly set the context, you might search for controls saved to one context such as a document, delete controls from another context such as a custom template and then add the control to another context such as Normal.dot.&lt;/p&gt;

&lt;p&gt;To avoid these issues, &lt;strong&gt;always &lt;/strong&gt;set the customization context of the application to the same document or template every time you search for, delete, or add controls to a menu.&lt;/p&gt;

&lt;p&gt;Note that in a Word document-level customization, it is probably best to set the customization context to the active document.&amp;#160; That way when the user uninstalls the customization, the document and the menu commands that pertain to that document disappear as expected.&lt;/p&gt;

&lt;p&gt;In Word application-level add-in, the best practice is to use a &lt;strong&gt;custom template&lt;/strong&gt; for reasons mentioned later on in this post.&lt;/p&gt;

&lt;p&gt;In the following example, I highlighted in bold the line that sets the customization context.&amp;#160; Further along in this post, I will show you where I got &lt;strong&gt;customTemplate&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[Visual Basic]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Private Sub &lt;/span&gt;AddMenuItem()

&lt;strong&gt;    &lt;font size="4"&gt;MyApplication.CustomizationContext = customTemplate&lt;/font&gt;&lt;/strong&gt;

    &lt;span style="color: blue"&gt;Dim &lt;/span&gt;menuItem &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.MsoControlType = _
        Office.MsoControlType.msoControlButton

    myControl = &lt;span style="color: blue"&gt;CType&lt;/span&gt;(MyApplication.CommandBars(&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;).Controls.Add _
       (menuItem, 1, &lt;span style="color: blue"&gt;True&lt;/span&gt;), Office.CommandBarButton)

    myControl.Style = Office.MsoButtonStyle.msoButtonCaption
    myControl.Caption = &lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item&amp;quot;
    &lt;/span&gt;myControl.Tag = &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;
    &lt;/span&gt;customTemplate.Saved = &lt;span style="color: blue"&gt;True
&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;    GC&lt;/span&gt;.Collect()
&lt;span style="color: blue"&gt;
End Sub&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[C#]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;AddMenuItem()
{
    &lt;font size="4"&gt;&lt;strong&gt;myApplication.CustomizationContext = customTemplate;&lt;/strong&gt;&lt;/font&gt;
    Office.&lt;span style="color: #2b91af"&gt;MsoControlType &lt;/span&gt;menuItem = 
        Office.&lt;span style="color: #2b91af"&gt;MsoControlType&lt;/span&gt;.msoControlButton;

    myControl = 
        (Office.&lt;span style="color: #2b91af"&gt;CommandBarButton&lt;/span&gt;)myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;].Controls.Add
        (menuItem,missing, missing, 1, &lt;span style="color: blue"&gt;true&lt;/span&gt;);

    myControl.Style = Office.&lt;span style="color: #2b91af"&gt;MsoButtonStyle&lt;/span&gt;.msoButtonCaption;
    myControl.Caption = &lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item&amp;quot;&lt;/span&gt;;
    myControl.Tag = &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;&lt;/span&gt;;

    myControl.Click += 
        &lt;span style="color: blue"&gt;new &lt;/span&gt;Microsoft.Office.Core.&lt;span style="color: #2b91af"&gt;_CommandBarButtonEvents_ClickEventHandler
            &lt;/span&gt;(myControl_Click);

    customTemplate.Saved = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;    GC&lt;/span&gt;.Collect();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;Also – a quick tip&lt;/strong&gt; – set the &lt;strong&gt;Saved&lt;/strong&gt; property of the template to &lt;strong&gt;true &lt;/strong&gt;after you add the control.&amp;#160; This stops that annoying prompt from appearing that asks if you would like to save your customizations to the template.&lt;/p&gt;

&lt;h4&gt;Tip #3: Use a Custom Template&lt;/h4&gt;

&lt;p&gt;It is very difficult to delete a control &lt;strong&gt;before&lt;/strong&gt; the add-in shuts down.&amp;#160; For example, if you attempt to delete a control in the &lt;strong&gt;ThisAddIn_Shutdown&lt;/strong&gt; event handler, you will receive a not so helpful COM exception.&amp;#160; You will get similar results in the &lt;strong&gt;Quit&lt;/strong&gt; event of Word.&amp;#160; &lt;/p&gt;

&lt;p&gt;That is because the template that you are using as your application’s customization context is not writable in either of those event handlers.&amp;#160; So if you cannot easily delete the control when Word closes, that means that the control will always live inside of the template.&lt;/p&gt;

&lt;p&gt;This is a problem if you are using Normal.dot to persist the controls.&amp;#160; Here is why.&amp;#160; Let’s say the user decides that he does not want to see your command in a menu anymore.&amp;#160; With a caption such as “My Menu Item”, can you really blame him? So the user uninstalls your add-in. However, the menu command still lives in Normal.dot! When that user opens his document in Word, &amp;quot;My Menu Item” still appears. Doooh! Here comes the support calls!&lt;/p&gt;

&lt;p&gt;The way around this is to provide your own custom template to store customizations such as custom menus and menu items.&amp;#160; Your setup application can remove the template along with add-in. That way when the user uninstalls the add-in, they also remove the template that contains the menu items. &lt;/p&gt;

&lt;p&gt;In the following example, I retrieve a custom template from the users documents folder. Yes, your setup application will probably use a different location to place the custom template, but this is just for an example.&lt;/p&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[Visual Basic]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Private Sub &lt;/span&gt;GetCustomTemplate()&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;    Dim &lt;/span&gt;TemplatePath &lt;span style="color: blue"&gt;As String &lt;/span&gt;= &lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.GetFolderPath _
        (&lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;SpecialFolder&lt;/span&gt;.MyDocuments) + &lt;span style="color: #a31515"&gt;&amp;quot;\MyCustomTemplate.dotx&amp;quot;
&lt;/span&gt;&lt;span style="color: blue"&gt;    Dim &lt;/span&gt;install &lt;span style="color: blue"&gt;As Boolean &lt;/span&gt;= &lt;span style="color: blue"&gt;True

    For Each &lt;/span&gt;installedTemplate &lt;span style="color: blue"&gt;As &lt;/span&gt;Word.&lt;span style="color: #2b91af"&gt;Template &lt;/span&gt;&lt;span style="color: blue"&gt;In &lt;/span&gt;MyApplication.Templates
        &lt;span style="color: blue"&gt;If &lt;/span&gt;installedTemplate.FullName = &lt;span style="color: blue"&gt;DirectCast&lt;/span&gt;(TemplatePath, &lt;span style="color: blue"&gt;String&lt;/span&gt;) &lt;span style="color: blue"&gt;Then
            &lt;/span&gt;install = &lt;span style="color: blue"&gt;False
&lt;/span&gt;        &lt;span style="color: blue"&gt;End If
    Next

    If &lt;/span&gt;install = &lt;span style="color: blue"&gt;True Then
        &lt;/span&gt;MyApplication.AddIns.Add(TemplatePath.ToString(), &lt;span style="color: blue"&gt;True&lt;/span&gt;)
&lt;span style="color: blue"&gt;    End If

&lt;/span&gt;    customTemplate = MyApplication.Templates(TemplatePath)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[C#]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;GetCustomTemplate()
{
    &lt;span style="color: blue"&gt;object &lt;/span&gt;TemplatePath = &lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.GetFolderPath
        (&lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;SpecialFolder&lt;/span&gt;.MyDocuments) +
        &lt;span style="color: #a31515"&gt;&amp;quot;\\MyCustomTemplate.dotx&amp;quot;&lt;/span&gt;;
    &lt;span style="color: blue"&gt;object &lt;/span&gt;install = &lt;span style="color: blue"&gt;true&lt;/span&gt;;
    &lt;/pre&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(Word.&lt;span style="color: #2b91af"&gt;Template &lt;/span&gt;installedTemplate &lt;span style="color: blue"&gt;in &lt;/span&gt;myApplication.Templates)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(installedTemplate.FullName == (&lt;span style="color: blue"&gt;string&lt;/span&gt;)TemplatePath)
        {
            install = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
        }
    }
    &lt;span style="color: blue"&gt;if &lt;/span&gt;((&lt;span style="color: blue"&gt;bool&lt;/span&gt;)install)
    {
        myApplication.AddIns.Add(TemplatePath.ToString(), &lt;span style="color: blue"&gt;ref &lt;/span&gt;install);
    }
    customTemplate = myApplication.Templates.get_Item(&lt;span style="color: blue"&gt;ref &lt;/span&gt;TemplatePath);

}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h4&gt;&lt;font color="#808080"&gt;&lt;font color="#000000"&gt;Drum roll please .. I present the complete example&lt;/font&gt;&lt;/font&gt;&lt;/h4&gt;

&lt;p&gt;To provide context, here is the complete sample:&lt;/p&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[Visual Basic]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;Public Class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ThisAddIn

    &lt;/span&gt;&lt;span style="color: blue"&gt;Private &lt;/span&gt;MyApplication &lt;span style="color: blue"&gt;As &lt;/span&gt;Word.Application
    &lt;span style="color: blue"&gt;Private WithEvents &lt;/span&gt;myControl &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.&lt;span style="color: #2b91af"&gt;CommandBarButton
    &lt;/span&gt;&lt;span style="color: blue"&gt;Private &lt;/span&gt;customTemplate &lt;span style="color: blue"&gt;As &lt;/span&gt;Word.&lt;span style="color: #2b91af"&gt;Template

    &lt;/span&gt;&lt;span style="color: blue"&gt;Private Sub &lt;/span&gt;ThisAddIn_Startup _
    (&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;sender &lt;span style="color: blue"&gt;As Object&lt;/span&gt;, &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;e &lt;span style="color: blue"&gt;As &lt;/span&gt;System.&lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;) &lt;span style="color: blue"&gt;Handles Me&lt;/span&gt;.Startup

        MyApplication = &lt;span style="color: blue"&gt;Me&lt;/span&gt;.Application

        GetCustomTemplate()
        RemoveExistingMenuItem()
        AddMenuItem()

    &lt;span style="color: blue"&gt;End Sub

    Private Sub &lt;/span&gt;GetCustomTemplate()
        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;TemplatePath &lt;span style="color: blue"&gt;As String &lt;/span&gt;= &lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.GetFolderPath _
            (&lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;SpecialFolder&lt;/span&gt;.MyDocuments) + &lt;span style="color: #a31515"&gt;&amp;quot;\MyCustomTemplate.dotx&amp;quot;
        &lt;/span&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;install &lt;span style="color: blue"&gt;As Boolean &lt;/span&gt;= &lt;span style="color: blue"&gt;True

        For Each &lt;/span&gt;installedTemplate &lt;span style="color: blue"&gt;As &lt;/span&gt;Word.&lt;span style="color: #2b91af"&gt;Template &lt;/span&gt;&lt;span style="color: blue"&gt;In &lt;/span&gt;MyApplication.Templates
            &lt;span style="color: blue"&gt;If &lt;/span&gt;installedTemplate.FullName = &lt;span style="color: blue"&gt;DirectCast&lt;/span&gt;(TemplatePath, &lt;span style="color: blue"&gt;String&lt;/span&gt;) &lt;span style="color: blue"&gt;Then
                &lt;/span&gt;install = &lt;span style="color: blue"&gt;False
            End If
        Next

        If &lt;/span&gt;install = &lt;span style="color: blue"&gt;True Then
            &lt;/span&gt;MyApplication.AddIns.Add(TemplatePath.ToString(), &lt;span style="color: blue"&gt;True&lt;/span&gt;)
        &lt;span style="color: blue"&gt;End If

        &lt;/span&gt;customTemplate = MyApplication.Templates(TemplatePath)

    &lt;span style="color: blue"&gt;End Sub

    Private Sub &lt;/span&gt;RemoveExistingMenuItem()

        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;contextMenu &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.&lt;span style="color: #2b91af"&gt;CommandBar &lt;/span&gt;= _
        MyApplication.CommandBars(&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;)

        MyApplication.CustomizationContext = customTemplate

        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;control &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.&lt;span style="color: #2b91af"&gt;CommandBarButton &lt;/span&gt;= contextMenu.FindControl _
            (Office.&lt;span style="color: #2b91af"&gt;MsoControlType&lt;/span&gt;.msoControlButton, System.&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;.Missing, _
             &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;True&lt;/span&gt;, &lt;span style="color: blue"&gt;True&lt;/span&gt;)

        &lt;span style="color: blue"&gt;If Not &lt;/span&gt;(control &lt;span style="color: blue"&gt;Is Nothing&lt;/span&gt;) &lt;span style="color: blue"&gt;Then
            &lt;/span&gt;control.Delete(&lt;span style="color: blue"&gt;True&lt;/span&gt;)
        &lt;span style="color: blue"&gt;End If

    End Sub


    Private Sub &lt;/span&gt;AddMenuItem()

        MyApplication.CustomizationContext = customTemplate

        &lt;span style="color: blue"&gt;Dim &lt;/span&gt;menuItem &lt;span style="color: blue"&gt;As &lt;/span&gt;Office.&lt;span style="color: #2b91af"&gt;MsoControlType &lt;/span&gt;= _
            Office.&lt;span style="color: #2b91af"&gt;MsoControlType&lt;/span&gt;.msoControlButton

        myControl = &lt;span style="color: blue"&gt;CType&lt;/span&gt;(MyApplication.CommandBars(&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;).Controls.Add _
           (menuItem, 1, &lt;span style="color: blue"&gt;True&lt;/span&gt;), Office.&lt;span style="color: #2b91af"&gt;CommandBarButton&lt;/span&gt;)

        myControl.Style = Office.&lt;span style="color: #2b91af"&gt;MsoButtonStyle&lt;/span&gt;.msoButtonCaption
        myControl.Caption = &lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item&amp;quot;
        &lt;/span&gt;myControl.Tag = &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;
        &lt;/span&gt;customTemplate.Saved = &lt;span style="color: blue"&gt;True

        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;GC&lt;/span&gt;.Collect()

    &lt;span style="color: blue"&gt;End Sub

    Sub &lt;/span&gt;myControl_Click(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;Ctrl &lt;span style="color: blue"&gt;As &lt;/span&gt;Microsoft.Office.Core.&lt;span style="color: #2b91af"&gt;CommandBarButton&lt;/span&gt;, _
                        &lt;span style="color: blue"&gt;ByRef &lt;/span&gt;CancelDefault &lt;span style="color: blue"&gt;As Boolean&lt;/span&gt;) &lt;span style="color: blue"&gt;Handles &lt;/span&gt;myControl.Click

        System.Windows.Forms.&lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item clicked&amp;quot;&lt;/span&gt;)

    &lt;span style="color: blue"&gt;End Sub

    Private Sub &lt;/span&gt;ThisAddIn_Shutdown() &lt;span style="color: blue"&gt;Handles Me&lt;/span&gt;.Shutdown

    &lt;span style="color: blue"&gt;End Sub

End Class
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;font color="#808080"&gt;[C#]&lt;/font&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ThisAddIn
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private &lt;/span&gt;Word.&lt;span style="color: #2b91af"&gt;Application &lt;/span&gt;myApplication;
    &lt;span style="color: blue"&gt;private &lt;/span&gt;Office.&lt;span style="color: #2b91af"&gt;CommandBarButton &lt;/span&gt;myControl;
    &lt;span style="color: blue"&gt;private &lt;/span&gt;Word.&lt;span style="color: #2b91af"&gt;Template &lt;/span&gt;customTemplate;

    &lt;span style="color: blue"&gt;private void &lt;/span&gt;ThisAddIn_Startup(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, System.&lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
    {
        myApplication = &lt;span style="color: blue"&gt;this&lt;/span&gt;.Application;

        GetCustomTemplate();
        RemoveExistingMenuItem();
        AddMenuItem();
    }

    &lt;span style="color: blue"&gt;private void &lt;/span&gt;GetCustomTemplate()
    {
        &lt;span style="color: blue"&gt;object &lt;/span&gt;TemplatePath = &lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.GetFolderPath
            (&lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;SpecialFolder&lt;/span&gt;.MyDocuments) +
            &lt;span style="color: #a31515"&gt;&amp;quot;\\MyCustomTemplate.dotx&amp;quot;&lt;/span&gt;;
        &lt;span style="color: blue"&gt;object &lt;/span&gt;install = &lt;span style="color: blue"&gt;true&lt;/span&gt;;
        &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(Word.&lt;span style="color: #2b91af"&gt;Template &lt;/span&gt;installedTemplate &lt;span style="color: blue"&gt;in &lt;/span&gt;myApplication.Templates)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(installedTemplate.FullName == (&lt;span style="color: blue"&gt;string&lt;/span&gt;)TemplatePath)
            {
                install = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
            }
        }
        &lt;span style="color: blue"&gt;if &lt;/span&gt;((&lt;span style="color: blue"&gt;bool&lt;/span&gt;)install)
        {
            myApplication.AddIns.Add(TemplatePath.ToString(), &lt;span style="color: blue"&gt;ref &lt;/span&gt;install);
        }
        customTemplate = myApplication.Templates.get_Item(&lt;span style="color: blue"&gt;ref &lt;/span&gt;TemplatePath);

    }

    &lt;span style="color: blue"&gt;private void &lt;/span&gt;RemoveExistingMenuItem()
    {
        Office.&lt;span style="color: #2b91af"&gt;CommandBar &lt;/span&gt;contextMenu = myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;];
        myApplication.CustomizationContext = customTemplate;

        Office.&lt;span style="color: #2b91af"&gt;CommandBarButton &lt;/span&gt;control =
            (Office.&lt;span style="color: #2b91af"&gt;CommandBarButton&lt;/span&gt;)contextMenu.FindControl
            (Office.&lt;span style="color: #2b91af"&gt;MsoControlType&lt;/span&gt;.msoControlButton, missing,
            &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;true&lt;/span&gt;, &lt;span style="color: blue"&gt;true&lt;/span&gt;);

        &lt;span style="color: blue"&gt;if &lt;/span&gt;((control != &lt;span style="color: blue"&gt;null&lt;/span&gt;))
        {
            control.Delete(&lt;span style="color: blue"&gt;true&lt;/span&gt;);
        }

    }

    &lt;span style="color: blue"&gt;private void &lt;/span&gt;AddMenuItem()
    {
        myApplication.CustomizationContext = customTemplate;
        Office.&lt;span style="color: #2b91af"&gt;MsoControlType &lt;/span&gt;menuItem =
            Office.&lt;span style="color: #2b91af"&gt;MsoControlType&lt;/span&gt;.msoControlButton;

        myControl =
            (Office.&lt;span style="color: #2b91af"&gt;CommandBarButton&lt;/span&gt;)myApplication.CommandBars[&lt;span style="color: #a31515"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;].Controls.Add
            (menuItem, missing, missing, 1, &lt;span style="color: blue"&gt;true&lt;/span&gt;);

        myControl.Style = Office.&lt;span style="color: #2b91af"&gt;MsoButtonStyle&lt;/span&gt;.msoButtonCaption;
        myControl.Caption = &lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item&amp;quot;&lt;/span&gt;;
        myControl.Tag = &lt;span style="color: #a31515"&gt;&amp;quot;MyMenuItem&amp;quot;&lt;/span&gt;;

        myControl.Click +=
            &lt;span style="color: blue"&gt;new &lt;/span&gt;Microsoft.Office.Core.&lt;span style="color: #2b91af"&gt;_CommandBarButtonEvents_ClickEventHandler
                &lt;/span&gt;(myControl_Click);

        customTemplate.Saved = &lt;span style="color: blue"&gt;true&lt;/span&gt;;

        &lt;span style="color: #2b91af"&gt;GC&lt;/span&gt;.Collect();

    }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;myControl_Click(Microsoft.Office.Core.&lt;span style="color: #2b91af"&gt;CommandBarButton &lt;/span&gt;Ctrl,
        &lt;span style="color: blue"&gt;ref bool &lt;/span&gt;CancelDefault)
    {
        System.Windows.Forms.&lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color: #a31515"&gt;&amp;quot;My Menu Item clicked&amp;quot;&lt;/span&gt;);
    }
    &lt;span style="color: blue"&gt;private void &lt;/span&gt;ThisAddIn_Shutdown(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, System.&lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
    {
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Now you can issue a patch to your buddy so that he has only one “My Menu Item” appearing in his shortcut menu.&amp;#160; Although .. I am not sure what “My Menu Item” is really suppose to do … 

&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9463406" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vsto/archive/tags/Norm+Estabrook/default.aspx">Norm Estabrook</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Command+bars/default.aspx">Command bars</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Office+2007/default.aspx">Office 2007</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Office+2003/default.aspx">Office 2003</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Word+2007/default.aspx">Word 2007</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Word+2003/default.aspx">Word 2003</category><category domain="http://blogs.msdn.com/vsto/archive/tags/add-ins/default.aspx">add-ins</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Office+14/default.aspx">Office 14</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Office+2010/default.aspx">Office 2010</category></item><item><title>Visual Studio Command Bar for Arranging Controls on Documents and Worksheets (McLean Schofield)</title><link>http://blogs.msdn.com/vsto/archive/2008/04/10/visual-studio-command-bar-for-arranging-controls-on-documents-and-worksheets.aspx</link><pubDate>Fri, 11 Apr 2008 00:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8376521</guid><dc:creator>VSTO Team</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/vsto/comments/8376521.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vsto/commentrss.aspx?PostID=8376521</wfw:commentRss><description>&lt;P&gt;One of the lesser-known features of Visual Studio Tools for Office is the &lt;STRONG&gt;Microsoft Office Word&lt;/STRONG&gt; and &lt;STRONG&gt;Microsoft Office Excel&lt;/STRONG&gt; command bar. When you are developing a document-level project for Word or Excel (for example, a Word Document or Excel Workbook project) and you have the document or one of your worksheets open in the designer, this command bar appears in Visual Studio, just above the designer. In Excel, this command bar looks like the following by default. 
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_10.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=168 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_1.png" width=497 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_1.png"&gt;&lt;/A&gt;&amp;nbsp; 
&lt;P&gt;The first several buttons in this command bar are for changing the keyboard scheme and mapping XML to the document or worksheet. This post is about the purpose of the rest of the buttons. 
&lt;P&gt;If you are designing a custom UI for your document or worksheet that includes Windows Forms controls that you add by using the designer, you can use these buttons to arrange the controls with a single click. When you select multiple controls in the designer, these buttons become enabled. 
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image16.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image16.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=37 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image16_thumb.png" width=263 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image16_thumb.png"&gt;&lt;/A&gt;&amp;nbsp; 
&lt;P&gt;From left to right, here are the buttons you can use: 
&lt;UL&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Align Lefts&lt;/STRONG&gt;. This button moves all of the controls to the left, so that they are aligned with the left side of the left-most control.&lt;/P&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Align Centers&lt;/STRONG&gt;. This button moves all of the controls left or right so that they are aligned around the vertical axis of the widest control.&lt;/P&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Align Rights&lt;/STRONG&gt;. This button moves all of the controls to the right, so that they are aligned with the right side of the right-most control.&lt;/P&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Align Tops&lt;/STRONG&gt;. This button moves all of the controls up, so that they are aligned with the top side of the upper-most control.&lt;/P&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Align Middles&lt;/STRONG&gt;. This button moves all of the controls up or down, so that they are aligned around the horizontal axis of the tallest control.&lt;/P&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Align Bottoms&lt;/STRONG&gt;. This button moves all of the controls down, so that they are aligned with the bottom side of the bottom-most control.&lt;/P&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Make Horizontal Spacing Equal&lt;/STRONG&gt;. The left-most and right-most control remain in place; the controls in between move horizontally so that there is equal horizontal space between each control.&lt;/P&gt;
&lt;LI&gt;&lt;STRONG&gt;Make Vertical Spacing Equal&lt;/STRONG&gt;. The top-most and bottom-most control remain in place; the controls in between move vertically so that there is equal vertical space between each control.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;A special note about Word.&lt;/STRONG&gt; In Word projects, the alignment buttons are enabled only if the selected controls are &lt;EM&gt;not&lt;/EM&gt; in line with text. By default, controls that you add to a Word document at design time are in line with text. To change the layout style of the control, right-click the control and then click &lt;STRONG&gt;Format Control&lt;/STRONG&gt;. Then, on the &lt;STRONG&gt;Layout&lt;/STRONG&gt; tab, select a wrapping style other than &lt;STRONG&gt;In line with text&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image55.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image55.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=202 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image55_thumb.png" width=461 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image55_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Examples.&lt;/STRONG&gt; Enough words! Here are some examples that show these alignment options in action. These examples are pretty contrived, but they should give you an idea of the effects of each option.&lt;/P&gt;
&lt;P&gt;Given the following buttons on a worksheet:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image19.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image19.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=232 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image19_thumb.png" width=384 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image19_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Align Lefts&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_16.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=244 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_7.png" width=240 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Align Centers&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_18.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=244 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_8.png" width=238 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_8.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And &lt;STRONG&gt;Align Rights&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_20.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_20.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=244 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_9.png" width=240 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image_thumb_9.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Given the following buttons on a worksheet:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image40.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image40.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=221 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image40_thumb.png" width=398 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image40_thumb.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Align Tops&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image43.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image43.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=116 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image43_thumb.png" width=400 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image43_thumb.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Align Middles&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image37.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image37.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=136 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image37_thumb.png" width=406 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image37_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And &lt;STRONG&gt;Align Bottoms&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image46.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image46.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=134 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image46_thumb.png" width=400 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image46_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Given the following buttons on a worksheet:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image49.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image49.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=56 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image49_thumb.png" width=566 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image49_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Make Horizontal Spacing Equal&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image52.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image52.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=54 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image52_thumb.png" width=576 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image52_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Finally, given the following buttons on a worksheet:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image64.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image64.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=306 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image64_thumb.png" width=173 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image64_thumb.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Make Vertical Spacing Equal&lt;/STRONG&gt; rearranges the controls as follows:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image67.png" mce_href="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image67.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=297 alt=image src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image67_thumb.png" width=186 border=0 mce_src="http://blogs.msdn.com/blogfiles/vsto/WindowsLiveWriter/HiddenVSTOFeature_9861/image67_thumb.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope these options come in handy then next time you are adding controls to a document or worksheet in VSTO.&lt;/P&gt;
&lt;P&gt;-------------------------------------------------&lt;/P&gt;
&lt;P&gt;McLean Schofield, Programming Writer&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8376521" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vsto/archive/tags/Command+bars/default.aspx">Command bars</category><category domain="http://blogs.msdn.com/vsto/archive/tags/McLean+Schofield/default.aspx">McLean Schofield</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Excel+2003/default.aspx">Excel 2003</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Word+2007/default.aspx">Word 2007</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Word+2003/default.aspx">Word 2003</category><category domain="http://blogs.msdn.com/vsto/archive/tags/Excel+2007/default.aspx">Excel 2007</category></item></channel></rss>