var
LineNo: integer;
CM: CodeModule;
NewToolbar: CommandBar;
ToolbarItem: CommandBarControl;
begin
WBk.ConnectTo(Excel.Workbooks[1]);
CM := WBk.VBProject.VBComponents.Item(
'ThisWorkbook').Codemodule;
{ Insert the macro text }
LineNo:=1;
CM.InsertLines(LineNo + 1, 'Public Sub ShowSuccess');
CM.InsertLines(LineNo + 2, ' Msgbox("Made it!")');
CM.InsertLines(LineNo + 3, 'End Sub');
{ Create temporary toolbar - set last parameter to False
to make a permanent toolbar }
NewToolBar := Excel.CommandBars.Add(
'MyToolbar', msoBarTop, False, True);
OleVariant(NewToolbar).Visible := True;
ToolbarItem := NewToolbar.Controls.Add(
msoControlButton, EmptyParam,
EmptyParam, EmptyParam, True);
ToolbarItem.Set_TooltipText('Show success');
OleVariant(ToolbarItem).OnAction :=
'ThisWorkbook.ShowSuccess';
OleVariant(ToolbarItem).FaceID := 2;
------------------
这是我在网上看到过的一段代码,希望能有帮助