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.
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.
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).
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
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
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).