Setting the context of a CommandBar (Word 2000)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Setting the context of a CommandBar (Word 2000)

Post by ChrisGreaves »

How to add a Commandbar and set the .Context to other than Normal.dot?

From Word 2000 VBA help:
Context Property: Returns or sets a string that determines where a command bar will be saved. The string is defined and interpreted by the application. Read/write String. Remarks: You can set the Context property only for custom command bars. This property will fail if the application doesn't recognize the context string, or if the application doesn't support changing context strings programmatically.

Code: Select all

With cbResult
    .Name = strIdentifier
    .Visible = True
    .Position = msoBarTop
    Dim strContext As String
    strContext = .Context
    .Context = strContext ' cannot set the context to its current value
    .Context = ThisDocument.FullName ' cannot set the context to a new value
End With
Yet the code pasted above (document attached) fails on both lines that attempt to set the context.
That the statement ".Context = strContext" fails astounds me - if you can't set the context to its existing value, what hope have I? :shrug:

The recorded macro (document attached) is from this end-user choosing Format, Styles, Oragnizer, Toolbars, and moving the toolbar from Normal.dot to the document file.
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Setting the context of a CommandBar (Word 2000)

Post by HansV »

I've always used the global CustomizationContext property for this purpose. This is an object that can be a Document or Template.

Examples:

CustomizationContext = ActiveDocument.AttachedTemplate
...
CustomizationContext = NormalTemplate

(Note that although it is an object, you don't need to use the keyword Set)
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Setting the context of a CommandBar (Word 2000)

Post by ChrisGreaves »

HansV wrote:I've always used the global CustomizationContext property for this purpose.
Hans, thanks for this.
It took me a few tries until I realized that the statement went OUTSIDE and BEFORE the "CommandBars.Add", but all is working just hunky-dory now.

Code: Select all

If cbResult Is Nothing Then
    Application.CustomizationContext = ThisDocument
    Set cbResult = Application.CommandBars.Add
    With cbResult
        .Name = strIdentifier
        .Visible = True
        .Position = msoBarTop
        Dim strContext As String
        strContext = .Context
'''''''    Application.CustomizationContext = ThisDocument
    End With
Else
End If
May I ask why you use this method? Is it because you found that the local version doesn't work, or did you just start using it and never saw a reason to switch?
An expensive day out: Wallet and Grimace

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Setting the context of a CommandBar (Word 2000)

Post by HansV »

I first learned about CustomizationContext (I can't remember how), and since it did what I wanted, I didn't look further.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Setting the context of a CommandBar (Word 2000)

Post by ChrisGreaves »

HansV wrote:... since it did what I wanted, I didn't look further.
Thanks Hans.
An expensive day out: Wallet and Grimace

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Setting the context of a CommandBar (Word 2000)

Post by StuartR »

But do see the warning in Post 11071.
I recommend setting CustomizationContext back to the normal template before you exit.
StuartR


User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Setting the context of a CommandBar (Word 2000)

Post by ChrisGreaves »

StuartR wrote:But do see the warning in ..
Stuart, thanks for this. Too, I should have paid more attention to Hans's
Post 13724 above.

Code: Select all

If cbResult Is Nothing Then
    Application.CustomizationContext = ThisDocument
    Set cbResult = Application.CommandBars.Add
    With cbResult
        .Name = strIdentifier
        .Visible = True
        .Position = msoBarTop
        Dim strContext As String
        strContext = .Context
    End With
    Application.CustomizationContext = NormalTemplate  '  But do see the warning in Post 11071. 
Else
End If
An expensive day out: Wallet and Grimace

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Setting the context of a CommandBar (Word 2000)

Post by ChrisGreaves »

Posted FYI
ChrisGreaves wrote:I should have paid more attention to Hans's
Several months pass ...
I've been plagued by repeated instances of my "Under" toolbar appearing.
I've
  • deleted them through the user interface,
  • saved Normal.dot,
  • written macros to remove superfluous toolbars on the template being opened,
  • deleted the infamous \Data\ from the registry,
  • run VBA Code Cleaner,
every trick I know.
I had not run out of tricks when this morning I decided to rebuild the template from scratch.

I used Rob Bovey's code cleaner to store the module files in a fresh folder, used Word to create and save a new template, imported the modules to the new template, saved same and then ran the toolbar-creation macro.
It seems to have worked this time.

My theory is this: While I was messing around NOT understanding Context, and in the days immediately following, I was probably corrupting the template somehow so that Word just could not rid itself of 4 (count 'em) "Under" toolbars.
An expensive day out: Wallet and Grimace