CustomizationContext - Word2003 - solved!

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

CustomizationContext - Word2003 - solved!

Post by ChrisGreaves »

This has been bugging me since way back here and beyond.

Code: Select all

Sub MakeBarNew()
    If MsgBox("Do you really want to create another menu?", vbYesNo + vbDefaultButton2, "MakeBarNew") = vbYes Then
        Dim dt As Date
        dt = Now()
        Debug.Print dt
        ' We always start off with a fresh version of OUR command bar:
        Application.CustomizationContext = ThisDocument
        Call RemoveBars(strMenuIdentifier)
        Dim cbMe As CommandBar
        Set cbMe = cbGetCommandBar(strMenuIdentifier) ' assumes most controls live on this root - saves time
        ' We use the 1st table in the project as our driver
        Dim tbl As Table
        Set tbl = ThisDocument.Tables(1)
        Dim lngRow As Long
        ' The first row is a heading row
        For lngRow = 2 To tbl.Rows.Count
            Application.StatusBar = lngRow & " / " & tbl.Rows.Count
            Dim rw As Row
            Set rw = tbl.Rows(lngRow)
            Dim dtRow As Date
            dtRow = Time
            Call ProcessMacroTableRow(cbMe, rw)
            If (lngRow / 50) = Int(lngRow / 50) Then
                ThisDocument.Save
            Else
            End If
        Next lngRow
        Debug.Print Format(Time() - dt, "hh:mm:ss")
    Else
    End If
End Sub
I was doing everything right (as I always think!), and yet was ending up with three, sometimes FOUR toolbars, all called 'UNDER" and all doing different things.

Code: Select all

Function RemoveBars(strBarName As String)
    ''' Remove all bars that match the name at the left-hand end
    ''' case INsensitive
    Dim lng As Long
    Application.CustomizationContext = ThisDocument
    For lng = Application.CommandBars.Count To 1 Step -1
        If UCase(Left(Application.CommandBars(lng).Name, Len(strBarName))) = UCase(strBarName) Then
            Application.CommandBars(lng).Delete
        Else
        End If
    Next lng
    Application.CustomizationContext = NormalTemplate
    For lng = Application.CommandBars.Count To 1 Step -1
        If UCase(Left(Application.CommandBars(lng).Name, Len(strBarName))) = UCase(strBarName) Then
            Application.CommandBars(lng).Delete
        Else
        End If
    Next lng
End Function
Yup!
I was treating the settings of "CustomizationContext " as local to the procedure "RemoveBars", whereas, of course, it is an Application setting, hence global.
My new code reads

Code: Select all

Sub MakeBarNew()
    If MsgBox("Do you really want to create another menu?", vbYesNo + vbDefaultButton2, "MakeBarNew") = vbYes Then
        Dim dt As Date
        dt = Now()
        Debug.Print dt
        ' We always start off with a fresh version of OUR command bar:
        Call RemoveBars(strMenuIdentifier)
        Application.CustomizationContext = ThisDocument
etc. etc. etc.
He who plants a seed, plants life.