private static void OnBeforeQueryStatus(object sender, EventArgs e)
      {
         var command = (CommandImplementationAdapter)sender;

         command.Enabled = false;
         var menu = new OleMenuCommmandAdapter(command);

         try
         {
            command.Implementation.QueryStatus(menu);
            command.Enabled = menu.Enabled;
            command.Visible = menu.Visible;
            command.Text = menu.Text;
            command.Checked = menu.Checked;
         }
         catch (Exception)
         {
            command.Enabled = command.Visible = false;
            throw;
         }
         finally
         {
            command.MatchedCommandId = 0;
         }
      }
      private static void OnExecute(object sender, EventArgs e)
      {
         var command = (CommandImplementationAdapter)sender;
         var menu = new OleMenuCommmandAdapter(command);

         command.Implementation.QueryStatus(menu);

         if (menu.Enabled)
         {
            command.Implementation.Execute(menu);
         }
      }