Hi,
I have created a new button in CommandBar:

Sub CreateButton()
Dim nBtn As Office.CommandBarButton
Dim cBar As Office.CommandBar
Dim BtnExists As Boolean
Dim Pos As Integer
Dim i As Integer

On Error GoTo eh

Set cBar = Application.CommandBars("File")
Let BtnExists = False

For i = 1 To cBar.Controls.Count
If cBar.Controls(i).Caption = "MyButton" Then
Let BtnExists = True
End If
If cBar.Controls(i).Caption = "Save &As..." Then
Let Pos = i + 1
End If
Next

If BtnExists Then
Set nBtn = cBar.Controls("MyButton")
Else
Set nBtn = cBar.Controls.Add(msoControlButton)
Let nBtn.Caption = "MyButton"
End If
Call nBtn.Move(Before:=Pos)
Let nBtn.OnAction = "RunAPI"
Exit Sub
eh:
MsgBox ("Error " & CStr(Err.Number) & ": " & Error)
End Sub


Then I need to erase that button while closing document. I tried either
to reset CommandBar (cBar.Reset) or call Delete method (Call
nBtn.Delete(False)) on that button. Works fine, but only for current
session. After that, when I open Word, button is still there.

Any suggestion? I need to erase the button permanently.

Thanks in advance. Jiri

RE: CreateButton - EraseButton by ChuckHenrich

ChuckHenrich
Wed Aug 17 09:51:06 CDT 2005

Add the customization context:

[snip]
On Error GoTo eh
CustomizationContext = ActiveDocument
[snip]
--
Chuck Henrich
www.ProductivityApps.com


"Lokutus" wrote:

> Hi,
> I have created a new button in CommandBar:
>
> Sub CreateButton()
> Dim nBtn As Office.CommandBarButton
> Dim cBar As Office.CommandBar
> Dim BtnExists As Boolean
> Dim Pos As Integer
> Dim i As Integer
>
> On Error GoTo eh
>
> Set cBar = Application.CommandBars("File")
> Let BtnExists = False
>
> For i = 1 To cBar.Controls.Count
> If cBar.Controls(i).Caption = "MyButton" Then
> Let BtnExists = True
> End If
> If cBar.Controls(i).Caption = "Save &As..." Then
> Let Pos = i + 1
> End If
> Next
>
> If BtnExists Then
> Set nBtn = cBar.Controls("MyButton")
> Else
> Set nBtn = cBar.Controls.Add(msoControlButton)
> Let nBtn.Caption = "MyButton"
> End If
> Call nBtn.Move(Before:=Pos)
> Let nBtn.OnAction = "RunAPI"
> Exit Sub
> eh:
> MsgBox ("Error " & CStr(Err.Number) & ": " & Error)
> End Sub
>
>
> Then I need to erase that button while closing document. I tried either
> to reset CommandBar (cBar.Reset) or call Delete method (Call
> nBtn.Delete(False)) on that button. Works fine, but only for current
> session. After that, when I open Word, button is still there.
>
> Any suggestion? I need to erase the button permanently.
>
> Thanks in advance. Jiri
>
>