• Custom Menu Showing Selected (Word XP)

    Author
    Topic
    #386850

    I have created 7 List Template Macros which I have added to a drop-down menu in a toolbar. I was wondering if it was possible to include a checkmark next to the last selected item from the menu so the user is aware of the current list template in use. Thanx in advance for any help… trish

    Viewing 0 reply threads
    Author
    Replies
    • #672803

      The checkmark next to a menu item is governed by its State property. If State = msoButtonDown, there is a checkmark, and if State = msoButtonUp, there isn’t.
      You’d have to add code to your macros that sets the State for each of the menu items. How to do that depends on how you organized your code.

      • #681223

        Hi Hans… well I ended up creating a toolbar from code – oh my gosh – I have figured out how to do almost everything, with the exception of this .state = msobuttonup/down/mixed! Cannot believe that this feature has me pickled… I’ve included the code that creates the toolbar which is an AutoOpen event on the template (so when the template attaches, the toolbar is created). The toolbar has customized images on it and I’m creating a ‘dummy’ toolbar to create these images from files (new XP IPictureDisp – member of Stdole library) and the using .copyface and .pasteface to change image (I could not figure out how to include in the commandbarbutton statement)… STILL LEARNING… Anyhow, back to the .state – what I’m looking for is IF the user selects the item on the dropdown menu, I want a checkmark placed next to the item. IF the user selects another menu item, I want the checkmark to move and appear next to the last item that they have selected. I’m also confused if this state is run here or in a different location (as a Function??). I’ve attached the code… If you have any suggestions/ideas with regard to my code, I would love to hear… thanx in advance… trish

        • #681235

          1. I don’t understand why you need a dummy toolbar to get the images. Since you’ve worked out how to get them, you might as well apply that code to the Numbering toolbar immediately.

          2. Your code suggests that you don’t have Option Explicit – there are undeclared variables in the code. May I strongly recommend that you turn this on? It’ll save you a lot of grief. Select Tools | Options… in the Visual Basic Editor, and check Require Declarations (or something similar, I don’t know the exact caption in English.)

          3. You may want to set the CustomizationContext to ThisDocument, because otherwise, the new commandbar will be stored in Normal.dot (unless that is your intention, of course.)

          4. I would set the Tag property for each of the items in the Schemes popup to a unique value; it can be a number (1, 2, 3, …), or a descriptive string. Then, you can write one procedure that acts as OnAction argument for all items. The item clicked by the user is CommandBars.ActionControl; you can inspect its Tag property. Loop through the items of the popup and set the State to msoButtonUp, then set it to msoButtonDown for the ActionControl. To perform the desired action for each item, use a Select Case statement.

          Assume that you set the OnAction property for each item of the popup to “SchemeAction”, and that you have assigned numeric Tags. The code for SchemeAction could look like this:

          Sub SchemeAction()
          Dim oCmdBar As Office.CommandBar
          Dim oCmdBarButton As Office.CommandBarButton
          Dim oCmdBarPop As Office.CommandBarPopup
          Dim oCmdBarSubPop As Office.CommandBarButton

          Set oCmdBar = Application.CommandBars(“Numbering”)
          Set oCmdBarPop = oCmdBar.Controls(“Schemes”)

          ‘ Set State
          For Each oCmdBarSubPop In oCmdBarPop.Controls
          oCmdBarSubPop.State = msoButtonUp
          Next oCmdBarSubPop
          CommandBars.ActionControl.State = msoButtonDown

          ‘ Action to perform
          Select Case CommandBars.ActionControl.Tag
          Case 1
          ‘ Code for first item
          Case 2
          ‘ Code for second item
          ‘ etc.
          End Select
          End Sub

          • #681811

            Hi Hans… thanx over and over again for all your assistance to me!!! kiss
            With regard to your feedback:
            1. For some stupid reason (I’m tooo inexperienced to figure it out) I cannot set the picture property under the commandbarbutton… I get this error msg: Run-time error ‘-2147467259(80004005); Method ‘Picture’ of object’_CommandBarButton failed.
            I have changed the code to read as follows, and continue to have the same error msg that I had last week:

            Dim objPicture As IPictureDisp

            Set objPicture1 = stdole.StdFunctions.LoadPicture(“c:atk imagesrestartno.bmp”)
            Set oCmdBarButton = oCmdBar.Controls.Add(msoControlButton, Before:=11)
            With oCmdBarButton
            .Picture = objPicture1
            .Visible = True
            .Caption = “Restart Numbering”
            .BeginGroup = True
            .OnAction = “RestartNo”
            .TooltipText = “Restart Numbering”
            End With
            I found an KB Article ( http://support.microsoft.com/?kbid=286460%5B/url%5D ) that explains that the IPictureDisp interface cannot be marshalled across process boundaries… I have no clue what this means confused… So if I create the dummy bar and copyface/pasteface it works fine? Can you explain what I am doing wrong or what this means?
            2. I did include the Option Explicit as you suggested… I read up on it and was unaware of it’s importance. The only problem is that the commandbar runs on an AutoOpen event and Option Explicit is stored under general declarations. Should I be doing something different?
            3. I did include the CustomizationContext.Active Document as you suggested… I do not want the toolbar to be stored in Normal… only when the template attaches.
            4. I did get my msobuttonup/down state working using your suggested Select Case statement (not used before)… I ended up performing a ‘call’ procedure under the Select Case to run the macro for the selected numbering scheme:

            Select Case CommandBars.ActionControl.Tag
            Case “SchemeArticleNo1”
            Call SchemeArticleNo1
            Is this correct?

            Once again Hans, thank you for all your help… I would be lost without you! hairout

            • #681819

              Hello Trish,

              The reason you can’t set the picture property for the buttons on the Numbering toolbar is because you set .Protection = msoBarNoCustomize for this toolbar immediately after creating it, so you can’t customize the pictures. You should set the Protection property after making all other changes. Then, you can move all the code for setting pictures down, and you won’t need the dummy toolbar any more.

              Requiring declarations in Tools | Options… only sets Option Explicit in all new modules. You’ll have to set it yourself at the top of all existing modules.

              Your case statement (the part of it that you posted) looks OK.

            • #681827

              Hi Hans… thanx for getting back to me so soon! I should have known better with the NoCustomization… I already ran into another problem with that… Thank you for pointing this out to me stupidme .

              Sorry, I’m not getting this Option Explicit… I did type it in at the top of my module, but it gives me the following error: Compile Error: invalid inside procedure.

              Private Sub Document_Open()
              ‘Creates the Numbering toolbar required to run
              ‘the Schemes module of this template
              ‘Recorded by Trish Krokosh, May 30, 2003

              Option Explicit

              Dim oCmdBar As Office.CommandBar
              Dim oCmdBarButton As Office.CommandBarButton
              Dim oCmdBarPop As Office.CommandBarPopup
              Dim oCmdBarSubPop As Office.CommandBarButton
              Dim objPicture As IPictureDisp

              The procedure I want is Document_Open() or AutoOpen (). The examples which I have seen with Option Explicit change it from a Document_Open event to a General Declaration. How do I declare both?

            • #681832

              Hi Trish,

              Option Explicit is a module-level setting. You must put it at the very top of a module, before all declarations, subs and functions, not within a sub or function. What it does is require you to declare all variables explicitly throughout the module. If you don’t have Option Explicit, the following is valid:

              Sub DoSomething
              lngNumber = 123

              A new variable lngNumber will be created on the fly and given the value 123. Great, I can hear you think, what’s wrong with that? Well, for instance that you are not protected against typos:

              Sub DoSomething
              lngNumber = 123

              MsgBox lngNunber

              You made a small mistake while typing, and VBA will create a new variable lngNunber for you. Since it has not been assigned a value explicitly, it is 0, so MsgBox displays 0 instead of 123. There is no error message to warn you of this. In more complicated code, you might not notice a mistake like this, and trust the outcome.

              Now, suppose you have Option Explicit at the top of the module. If you don’t declare lngNumber, VBA will complain that is is unknown. So you insert Dim lngNumber As Long:

              Sub DoSomething
              Dim lngNumber As Long
              lngNumber = 123

              MsgBox lngNunber

              VBA will still complain because lngNunber is not defined. So you are forced to recognize the typo.

              In your case, you should have something like this:

              Option Explicit

              Private Sub Document_Open()
              ‘Creates the Numbering toolbar required to run
              ‘the Schemes module of this template
              ‘Recorded by Trish Krokosh, May 30, 2003

              Dim oCmdBar As Office.CommandBar
              Dim oCmdBarButton As Office.CommandBarButton
              Dim oCmdBarPop As Office.CommandBarPopup
              Dim oCmdBarSubPop As Office.CommandBarButton
              Dim objPicture As IPictureDisp

            • #682167

              I understand much better now… thank you very much Hans… thankyou… After your explanation, I’m thinking that this should be placed above all my code, since I did not have the Option, Require Variable Declaration turned on… this is what I will do… I don’t think that I’ve told you lately that I love you… here you go… hugs and kiss !!! I’m finished with this module and going on to the next… accessing an Access query to populate a template! Wish me luck… you’ll probably hear from me again! Cheers! trish

            • #682207

              I’m beginning to feel like a pest… I made all the changes and tested this morning…
              1. I included Option Explicit at the top of the code and it placed a line underneath (to me indicating a new procedure…is this how it should be acting?
              2. I’m having a problem with the msobuttonup/down, the state is not changing when a document is new/opened. I need the state to initially be set to msobuttonup in all documents… can you help?? Here’s the code which you so graciously helped me with:

              Sub SchemeAction()

              Dim oCmdBar As Office.CommandBar
              Dim oCmdBarButton As Office.CommandBarButton
              Dim oCmdBarPop As Office.CommandBarPopup
              Dim oCmdBarSubPop As Office.CommandBarButton
              Dim objPicture As stdole.IPictureDisp

              Set oCmdBar = Application.CommandBars(“Numbering”)
              Set oCmdBarPop = oCmdBar.Controls(“Schemes”)

              ‘Set State
              For Each oCmdBarSubPop In oCmdBarPop.Controls
              oCmdBarSubPop.State = msoButtonUp
              Next oCmdBarSubPop
              CommandBars.ActionControl.State = msoButtonDown

              Select Case CommandBars.ActionControl.Tag
              Case “SchemeArticleNo1”
              Call SchemeArticleNo1
              Case “SchemeArticleNo2”
              Call SchemeArticleNo2
              Case “SchemeFFN1”
              Call SchemeFFN1
              Case “SchemeFFN2”
              Call SchemeFFN2
              Case “SchemeFFN3”
              Call SchemeFFN3
              Case “SchemeFFN4”
              Call SchemeFFN4
              Case “SchemeStandard”
              Call SchemeStandard
              End Select
              End Sub

            • #682234

              Hi Trish,

              1. By default, VBA will display a line after the general declaration section and after each function and procedure in a module. If you don’t like it, select Tools | Options… (in the Visual Basic Editor); the bottommost check box in the Editor tab determines whether you get these lines.

              2. You can add code to set the state to “up” in the Document_New and Document_Open (or AutoNew and AutoOpen) macros. You can call a common procedure from both (note that this is identical to a part of SchemeAction; you could call StatesUp from there too):

              Sub StatesUp()
              Dim oCmdBar As Office.CommandBar
              Dim oCmdBarButton As Office.CommandBarButton
              Dim oCmdBarPop As Office.CommandBarPopup
              Dim oCmdBarSubPop As Office.CommandBarButton

              Set oCmdBar = Application.CommandBars(“Numbering”)
              Set oCmdBarPop = oCmdBar.Controls(“Schemes”)

              ‘Set State
              For Each oCmdBarSubPop In oCmdBarPop.Controls
              oCmdBarSubPop.State = msoButtonUp
              Next oCmdBarSubPop
              End Sub

            • #682235

              Additional remark: if you want the “state” of the buttons to change automatically when you switch back and forth from document to document, it becomes a lot more complicated. You’d have to store the states of all buttons in the document, and write application-level event handlers. This is not trivial, see for example Selectively disable menu option (Word 97/2000).

            • #682263

              You’re right… scratchOK, I had to rethink this requirement… I have included the .state up code in Document_Close event which will clean up the checkmark when the user closes. The problem now is if the user selects a scheme, I don’t want the state to change in that document, even if they close it and reopen it. If they open a new document I want the state to change to msobuttonup and if they open an existing document but it has never had a scheme applied, I want the state to change to msobuttonup. The checkmark is not that important and I’m beginning to feel a little overwhelmed – thank god for Hans! surrender The checkmark’s purpose is to identify to the user the last listtemplate (scheme) which was selected in the document (for document sharing, shortcut key, etc.)… so… what I’m thinking is just writing more code with a new button on the toolbar or you helped me create a msgbox which lists the current templates in the activedocument – is it possible to include more text to identify the ‘active scheme’?

              Sub ListSchemes()
              ‘Brings a message box with a list of current schemes being
              ‘used in document

              Dim strList As String
              Dim strActList as String
              Dim lst As ListTemplate

              strList = “Schemes in Current Document:”
              For Each lst In ActiveDocument.ListTemplates
              strList = strList & vbCrLf & lst.Name
              Next lst
              StrActList = “Active Scheme in Current Document:”
              ?

              MsgBox strList
              MsgBox strActList End Sub

              What are your thoughts or suggestions in this regard… Thanx so very much for all your help Hans… woops

            • #682265

              I’m sorry, others will have to take over here. Up to now, your questions were more or less general Office VBA questions, but you’re going into the untamed jungle of Word’s List Templates now – uncharted territory for me.

            • #682292

              Trish,

              Haven’t followed this thread closely, nor seen the code you’re using for creating the named list templates, but it sounds like it would be hard to identify the “active list template” in a document, upon opening the document – if a document has more more than one list template in use somewhere in its contents, how do you define what is the “active” one?

              If by active list template, you mean the last one interactively used in that document by a previous user, perhaps you can store the name of that list template using a docvariable (Hans’ favored method) or a custom document property (my favored method). This would involve just adding a line of code to store this value, in the code you’re using to apply the list template.
              Then in Document_Open, you have code that looks up the stored value – any chance of doing something like that?

              Gary

            • #682317

              Gary… welcome to my nightmare! evilgrin … Poor Hans… I feel bad for bugging him so much… in your statement: If by active list template, you mean the last one interactively used in that document by a previous user YES… THIS IS WHAT I’M LOOKING FOR! Then you continue… perhaps you can store the name of that list template using a docvariable (Hans’ favored method) or a custom document property (my favored method). This would involve just adding a line of code to store this value, in the code you’re using to apply the list template. Then in Document_Open, you have code that looks up the stored value – any chance of doing something like that? OK – Sounds great… if you read through the thread, you will find that I am a beginner at all this! I am going to read up on docvariables and custom document property and then try to figure out… you will probably hear back from me… thanx for the pointer in the right direction! read dizzy

    Viewing 0 reply threads
    Reply To: Custom Menu Showing Selected (Word XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: