Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Commandbar from global temnplate (97/2000)

    Hi all,

    I have created a global template with code that is located in the user's startup folder.

    I use this code to add a menuitem:

    <pre>Sub CreateCommandBar()
    On Error GoTo locerr
    Dim cCommandBar As CommandBar
    Dim iCount As Integer
    CustomizationContext = ThisDocument
    Set cControl = Application.CommandBars("Menu Bar").Controls.Add(Type:= _
    msoControlPopup, Before:=Application.CommandBars("Menu Bar").Controls.Count + 1)
    cControl.Caption = "&Correspondence"
    With cControl.Controls.Add(Type:= _
    msoControlButton, Before:=1)
    .OnAction = "OpenHelp"
    .Caption = "&Help"
    .TooltipText = "Open Help file."
    .Enabled = True
    End With
    '...... more controls added
    </pre>


    My trouble is: the code does get fired (called from an Autoexec sub), the menuitem flashes by and then is gone. I'm SURE my code does not remove the menuitem.

    What gives?????
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Commandbar from global temnplate (97/2000)

    Hi Jan Karel,

    I created a global template in the startup folder, pasted your code into a module, and changed the minimum necessary to let it run (appended End Sub, declared cControl, added an error handling section). I also created an AutoExec macro that calls CreateCommandBar. It works! I even get prompted that the template has changed when I quit Word (since the menu has been added). <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

    Are you sure that the rest of the code doesn't muck things up?

    Added: I tested in Word 97 SR-2 and Word 2002 SP-2.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Commandbar from global temnplate (97/2000)

    Yes, the removecommandbar sub that is in there is ONLY called just before the createcommandbar code, to ensure I have just ONE copy of the control.

    I would very much like to have a tutorial on where and how Word keeps it's commandbar customisations and how to best manage them from a global template that will have no "attached" doc's, but just contains utility coding.

    Basically I wish to show an additional menu item on Word's main Menu Bar, but leave the user's normal template alone. It used to work, but somehow I succeeded in getting the behaviour I described earlier. Maybe caused by a wrong setting of the CustomizationContext during the deletion of the commandbarcontrol ???????
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Commandbar from global temnplate (97/2000)

    Hi Jan, Word is so much friendlier than Excel and you don't need to create command bars on the fly. Command bars - menu items and toolbars - are stored in the active template or in a global template. Open your template, add your new item to the menu bar, save the template. Simply create the menu item or toolbar using Tools, Customise - you don't need to code this at all.

    Once you close the template the additional menu item won't be displayed. However when you create a document based on that template the menu item will appear.

    A global template is always available - it may contain macros, autotext entries, menu modifications and customised toolbars. This gets stored in the Startup folder (See File Locations, in Tools Options) as a .dot file.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Commandbar from global temnplate (97/2000)

    <hr>Word is so much friendlier than Excel<hr>

    I disagree. It would have been a lot easier if Word would not have a bug so that the Temporary argument used in adding a control is ignored and all coded changes to the menubar are permanent.

    Also I find the Customization context thingy quite confusing. One small mistake and your customizations get dumped into your mormal template and nothing seems as it used to be...
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Commandbar from global temnplate (97/2000)

    Jan Karel,

    Karen's point is that commandbar customizations in Word global templates don't need to be made programatically, and in fact are probably better off being done interactively.

    AFAIK the default CustomizationContext, if not specified, will be Normal.dot. It looks like your code is doing the right thing by assigning CustomizationContext to ThisDocument, so it's hard to figure out why it's not working.
    Perhaps you could post the remove commandbar code as well?

    Gary

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Commandbar from global temnplate (97/2000)

    <P ID="edit" class=small>(Edited by jscher2000 on 10-May-03 14:43. Long lines "continued" to fit the average browser window.)</P>I seem to have things working again.

    Key seems to be that I messed up the Normal.dot.

    Current code :

    <pre>Sub CreateCommandBar()
    On Error GoTo locerr
    Dim cCommandBar As CommandBar
    Dim iCount As Integer
    CustomizationContext = ThisDocument
    Set cControl = Application.CommandBars("Menu Bar").Controls.Add(Type:= _
    msoControlPopup, Before:=Application.CommandBars("Menu Bar").Controls.Count + 1)
    cControl.Caption = "&Correspondence"
    With cControl.Controls.Add(Type:= _
    msoControlButton, Before:=1)
    .OnAction = "OpenHelp"
    .Caption = "&Help"
    .TooltipText = "Open Help file."
    .Enabled = True
    End With
    If bDatabase Then
    With cControl.Controls.Add(Type:= _
    msoControlButton, Before:=1)
    .OnAction = "SearchDatabase"
    .Caption = "&FindDoc"
    .TooltipText = "Search for documents in the database."
    .Enabled = bNetwork
    End With
    With cControl.Controls.Add(Type:= _
    msoControlButton, Before:=1)
    .OnAction = "WrapUp"
    .Caption = "F&inalise"
    .TooltipText = "Document ready, place a readonly copy on the " & _
    "network and update database."
    .Enabled = bNetwork
    End With
    End If
    With cControl.Controls.Add(Type:= _
    msoControlButton, Before:=1)
    .OnAction = "UpdateDocumentData"
    .Caption = "&UpDate"
    .TooltipText = "Update the database with current document data."
    End With
    If bUseNetwork Then
    With cControl.Controls.Add(Type:= _
    msoControlButton, Before:=1)
    .OnAction = "RequestDocNumber"
    .Caption = "&DocNumber"
    .TooltipText = "Request a document number."
    .Enabled = bNetwork
    End With
    End If

    With cControl.Controls.Add(Type:= _
    msoControlButton, Before:=1)
    .OnAction = "Main"
    .Caption = "&New Document"
    .TooltipText = "Letter, Fax en Memo Templates"
    End With
    ThisDocument.Saved = True
    TidyUp:
    Exit Sub
    locerr:
    If ReportError(Err.Description, Err.Number, "MainMod", "CreateCommandBar") = _
    vbRetry Then
    Resume
    Else
    Resume TidyUp
    End If
    End Sub
    Sub RemoveCommandBar()
    Dim bDone As Boolean
    On Error Resume Next
    CustomizationContext = ThisDocument
    Do
    With Application.CommandBars("Menu Bar")
    Set cControl = .Controls(.Controls.Count)
    End With
    If cControl.Caption = "&Correspondence" Then
    cControl.Delete
    Else
    bDone = True
    End If
    Loop Until bDone
    CustomizationContext = NormalTemplate
    Do
    With Application.CommandBars("Menu Bar")
    Set cControl = .Controls(.Controls.Count)
    End With
    If cControl.Caption = "&Correspondence" Then
    cControl.Delete
    Else
    bDone = True
    End If
    Loop Until bDone
    ThisDocument.Saved = True
    End Sub

    </pre>


    I first repeat the deletion for both normal.dot and the thisdocument object to be absolutely sure the menu item is removed.
    Then I add it again, using ThisDocument as the Customizationcontext.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •