Getting a toolbar button to remain with document

gijimbo

Board Regular
Joined
Feb 3, 2010
Messages
130
Back in the days of pre-2007 Office, it was easy to add a custom toolbar and toolbar button to a document template that would also be in any documents created from that template. Now I've got 2007 and try as I might, I just can't get that toolbar button (that runs a macro) to stick.

I have a template file with form fields in it. Some documents created from this template may require lines (with more information) to be added at a later date. The procedure for this is to unprotect the document, add the necessary lines and information, then reprotect the document to allow only form fields to be edited. The problem with this is that when the document is protected again, all the previous form fields that had been filled out, return to their default values. In VBA I know this can be avoided when you add the "NoReset:=True" parameter to the ".Protect" function. As far as I know there is no option to do this with the standard non-vba method of toggling document protection. To get around this, I have written a simple macro that toggles the document protection without resetting form fields.
Code:
Sub ToggleProtection()    
    With ActiveDocument
    If .ProtectionType = wdAllowOnlyFormFields Then
        'Remove protection
        .Unprotect
    Else
        'Re-protect
        .Protect _
            Type:=wdAllowOnlyFormFields, _
            NoReset:=True
    End If
    End With
End Sub
Here's what I want to do:
Add a button to the template and all documents based on the template that calls the custom "protection toggle" macro.

Quick launch bar method (can not use):
I understand that you can add a button to the quick launch bar and "tie" it to the document. That method would work excellent but unfortunately it only works with the new xml based word files. My file MUST be '.dot' format, not '.dotm' because there are still a large number of pre-2007 users that will be using the template and the documents generated from the template. Because of this, I cannot place the button in the QL bar for this document.

Add-ins ribbon tab method (where legacy office custom toolbars go):
All the 'dot' templates that had been created before with custom toolbars still work in Word 2007. The toolbars just show up in the Add-ins ribbon which is perfectly fine. My plan was to do this same thing with my new template but unfortunately you can't just make the add-ins tab appear and if it's already there (due to other add-ins being used) you can't just right click and choose to add a new button. However I know you can still use VBA to put it there. Here's the code I'm using to "create" the button since 2007 no longer allows you to just add it.
Code:
Private Sub Document_New()
    'Call AddProtectionToggleButton
    Dim cbar1 As CommandBar
    Dim cbutton As CommandBarButton
    
    On Error Resume Next
    Application.CommandBars("Toggles").Delete
    On Error GoTo 0
    
    Set cbar1 = Application.CommandBars.Add _
        (Name:="Toggles", Temporary:=True)
    cbar1.Visible = True
    Set cbutton = cbar1.Controls.Add _
        (Type:=msoControlButton, ID:=1)
    cbutton.FaceId = 225
    cbutton.Caption = "Toggle Protection"
    cbutton.OnAction = "ToggleProtection"
    
    'frmNewDrawing.Hide
    frmNewDrawing.Show
End Sub
Now I was hoping that by having this code in the "Document_New" sub routine, the button would be in all documents made from the template but here's what happens: The new documents do indeed show the button when created but when you save, close and reopen them, the button (along with the add-ins tab) is gone.

Does anyone know a way to make it 'stick'?

Another problem with doing it this way is that the template file itself does not show show the button. I could make a macro that calls a sub with the same code as shown above (minus the frmNewDrawing.Show) but all users will have to run this macro every time they use the template. This is a little counter intuitive since the user could just call the ToggleProtection macro anyways but I would much rather have the button there (for users who are not comfortable with or don't know how to use macros).
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Wow! After messing around a WHOLE lot more, I've found the solution which actually works out even better than I had hoped!

Before I address the solution directly, I will mention that although placing the "button creation" code in the "document_open" sub will sort-of solve the problem, I found another issue caused by having this code in either of the two subs. You will be prompted to save changes to the template any time you make a new document or open one already created.

The true solution is MUCH smoother, simpler, light weight and robust.
Note this line in my code above:
Code:
Temporary:=True
This was in my code simply because I had found other code that used it. I still got curios though and changed it to:
Code:
Temporary:=False
Then only ran the "button creation" code once, then saved the document.

That did it! The button is now only tied to the template and its children. Now I can actually remove the button creation code all together (or comment it out if I want to change it later) and it will be just like all the other old style templates that had been created with pre-2007 office.

Not sure who all was interested but there's the solution.:)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,609
Messages
6,185,985
Members
453,333
Latest member
BioCoder84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top