none
FindControl returns null when trying to locate my CommandBarPopups to remove them. RRS feed

  • Question

  • I have a problem with Popup command bars in word. Our application needs to add CommandBarPopups to the right click menus in Word.  We add 2 menus with submenus below them.  The code:

    In Application Startup, I set to the Customization context to a dummy dotm that i never save.

    I do this to prevent any writing to normal.dotm when creating the menus.  ( It apparently does not work )

    Next i call LoadContextMenu() below, to remove and create any existing menus.  When i do this, FindControl() in GetCommandBar() below returns null, even though the corrupted Normal.Dotm has duplicate menu items.

    Since i can't find them, RemoveMenus does not find the dead command bars and they are presented to the user.

    Is there any way i can clean out these dead popups?  The only solution i have been able to find is to delete the normal.dotm but that is not desirable.

        ///  Sets up the context menus.
            /// </summary>
            /// <param name="PobjCommandBars">Application Command Bars</param>
            ///
            public void LoadContextMenu(CommandBars PobjCommandBars)
            {
                if (PobjCommandBars != null)
                {
                    CommandBar LobjTableBar = null;
                    if (MbIsExcel)
                    {

                        LobjTableBar = PobjCommandBars["Cell"];
                        // Remove any existing menus and readd.
                        RemoveMenus(LobjTableBar);
                        AddMenus(LobjTableBar);
                        LobjTableBar = null;
                    }
                    else
                    {


                        LobjTableBar = PobjCommandBars["Text"];

                        // Remove any existing menus and readd.
                        RemoveMenus(LobjTableBar);
                        AddMenus(LobjTableBar);
                        LobjTableBar = null;
                        LobjTableBar = PobjCommandBars["Table Text"];

                        // Remove any existing menus and readd.
                        RemoveMenus(LobjTableBar);
                        AddMenus(LobjTableBar);
                        LobjTableBar = null;

                        LobjTableBar = PobjCommandBars["Spelling"];

                        // Remove any existing menus and readd.
                        RemoveMenus(LobjTableBar);
                        AddMenus(LobjTableBar);
                        LobjTableBar = null;

                    }

                }
            }

    private void RemoveMenus(CommandBar PobjTableBar)
            {

          I GET NULL For GetCommandBar() below.  So i can't remove dead menus.

                CommandBarPopup LobjPopup = GetCommandBar(PobjTableBar, MenuTags.DALINK_TABLE);
                if (LobjPopup != null)
                {
                    // Remove all child buttons
                    foreach (CommandBarControl LobjBut in LobjPopup.Controls)
                    {
                        CommandBarButton LobjCmdBut = LobjBut as CommandBarButton;
                        if (LobjCmdBut != null)
                            LobjCmdBut.Click -= new _CommandBarButtonEvents_ClickEventHandler(DALinkMenuClick);
                        LobjBut.Delete(MbUseTempMenus);
                    }
                    // kill parent
                    LobjPopup.Delete(MbUseTempMenus);
                }
                // TB Link popup
                LobjPopup = GetCommandBar(PobjTableBar, MenuTags.TBLINK_TABLE);
                if (LobjPopup != null)
                {
                    foreach (CommandBarControl LobjBut in LobjPopup.Controls)
                    {
                        CommandBarButton LobjCmdBut = LobjBut as CommandBarButton;
                        if (LobjCmdBut != null)
                            LobjCmdBut.Click -= new _CommandBarButtonEvents_ClickEventHandler(DALinkMenuClick);

                        LobjBut.Delete(MbUseTempMenus);
                    }

                    LobjPopup.Delete(MbUseTempMenus);
                }

                MobjDALinkPopup = MobjTBLinkPopup = null;
                McolButtons.Clear();
            }

            private CommandBarPopup GetCommandBar(CommandBar PobjTableBar, string PstrMenuTag)
            {
                object LobjMissing = Type.Missing;
                object LobjTrue = true;
                // See if popup menu exists

    FindControl does not find the duplicated corrupt popups.

                CommandBarPopup LobjPopup =
                    (CommandBarPopup)
                    PobjTableBar.FindControl(MsoControlType.msoControlPopup, LobjMissing, PstrMenuTag, LobjMissing,
                                             LobjTrue);

                // Try another way
                if( LobjPopup == null )
                {
                    foreach (CommandBarControl LobjControl in PobjTableBar.Controls)
                    {
                        Debug.Print(LobjControl.Caption);
                        CommandBarPopup LobjPop = LobjControl as CommandBarPopup;
                        if( LobjPop != null )
                        {
                           
                            if( LobjPop.Tag == PstrMenuTag)
                            {
                                return LobjPop;
                            }
                        }
                    }
                }
                return LobjPopup;
            }

    Tuesday, February 26, 2013 4:55 PM

Answers

  • It turns out i needed to set the customization context to NormalTemplate, then call FindControl().

    The menu items ended up in the normal template somehow and needed to be deleted from there.

    • Marked as answer by C DePalma Thursday, February 28, 2013 2:35 PM
    Thursday, February 28, 2013 2:32 PM

All replies

  • Hi C DePalma,

    Thank you for posting the MSDN Forum.

    Could you please tell me the version of Word and project type you are working on?

    I look forward to your reply.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 28, 2013 10:55 AM
    Moderator
  • It turns out i needed to set the customization context to NormalTemplate, then call FindControl().

    The menu items ended up in the normal template somehow and needed to be deleted from there.

    • Marked as answer by C DePalma Thursday, February 28, 2013 2:35 PM
    Thursday, February 28, 2013 2:32 PM