Skip to content

Commit 34b2ca0

Browse files
committed
delete named commandbar before creating it, if it already exists
1 parent 1f8537d commit 34b2ca0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBars.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
56
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
67
using Rubberduck.VBEditor.SafeComWrappers.MSForms;
78
using Rubberduck.VBEditor.SafeComWrappers.Office.Core.Abstract;
@@ -17,14 +18,33 @@ public CommandBars(Microsoft.Office.Core.CommandBars target)
1718

1819
public ICommandBar Add(string name)
1920
{
21+
DeleteExistingCommandBar(name);
2022
return new CommandBar(Target.Add(name, Temporary:true));
2123
}
2224

2325
public ICommandBar Add(string name, CommandBarPosition position)
2426
{
27+
DeleteExistingCommandBar(name);
2528
return new CommandBar(Target.Add(name, position, Temporary: true));
2629
}
2730

31+
private void DeleteExistingCommandBar(string name)
32+
{
33+
try
34+
{
35+
var existing = Target[name];
36+
if (existing != null)
37+
{
38+
existing.Delete();
39+
Marshal.FinalReleaseComObject(existing);
40+
}
41+
}
42+
catch
43+
{
44+
// specified commandbar didn't exist
45+
}
46+
}
47+
2848
public ICommandBarControl FindControl(int id)
2949
{
3050
return new CommandBarControl(Target.FindControl(Id:id));

0 commit comments

Comments
 (0)