CreateButton - EraseButton

L

Lokutus

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
 
C

Chuck Henrich

Add the customization context:

[snip]
On Error GoTo eh
CustomizationContext = ActiveDocument
[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top