D
David
OK I nabbed this code from this board and tweaked it to suit my needs.
This is a document, not a template.
It correctly installs the button in the document only once as the code
indicates. Multiple runs of the code only install one button.
The problem is when I close the document, and I'm back to a blank Word
desktop, there are as many buttons on the bar as times I've run the
code.
I need the button to be available in the document only.
Code is in Module 1.
Thanks,
David
Public Sub AutoOpen()
'Check for Protect
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
'install the button
Dim ctl As Office.CommandBarControl 'where the button will be
Dim ctlButton As Office.CommandBarButton
'before adding the button, verify if it is already installed ...
Set ctl = CommandBars("standard").Controls(CommandBars("standard").Controls.Count)
If ctl.Caption <> "Run TTD" Then
Set ctlButton = CommandBars("Standard").Controls.Add
With ctlButton
.Caption = "Run TTD"
.TooltipText = "Click to create Time Tracking Document"
.OnAction = "Main" 'the procedure to be call when a click
....
.Style = msoButtonCaption
End With
Else
'we are adding nothing to the toolBar, it is already here
End If
'Protect doc
ActiveDocument.Protect (wdAllowOnlyFormFields)
Main
End Sub
This is a document, not a template.
It correctly installs the button in the document only once as the code
indicates. Multiple runs of the code only install one button.
The problem is when I close the document, and I'm back to a blank Word
desktop, there are as many buttons on the bar as times I've run the
code.
I need the button to be available in the document only.
Code is in Module 1.
Thanks,
David
Public Sub AutoOpen()
'Check for Protect
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
'install the button
Dim ctl As Office.CommandBarControl 'where the button will be
Dim ctlButton As Office.CommandBarButton
'before adding the button, verify if it is already installed ...
Set ctl = CommandBars("standard").Controls(CommandBars("standard").Controls.Count)
If ctl.Caption <> "Run TTD" Then
Set ctlButton = CommandBars("Standard").Controls.Add
With ctlButton
.Caption = "Run TTD"
.TooltipText = "Click to create Time Tracking Document"
.OnAction = "Main" 'the procedure to be call when a click
....
.Style = msoButtonCaption
End With
Else
'we are adding nothing to the toolBar, it is already here
End If
'Protect doc
ActiveDocument.Protect (wdAllowOnlyFormFields)
Main
End Sub