' customize classic UI in Excel
' taken from Example09.vb
'
Private Sub Example09Main()

 Dim commandBar As Office.CommandBar = Nothing
 Dim commandBarBtn As Office.CommandBarButton = Nothing

 ' start excel and turn off msg boxes
 _excelApplication = New Excel.Application()
 _excelApplication.DisplayAlerts = False

 ' add a new workbook
 Dim workBook As Excel.Workbook = _excelApplication.Workbooks.Add()

 ' add a commandbar popup
 Dim commandBarPopup As Office.CommandBarPopup = _
_excelApplication.CommandBars.Item("Worksheet Menu Bar").Controls.Add( _
MsoControlType.msoControlPopup) commandBarPopup.Caption = "commandBarPopup" ' you can see we use an own icon via .PasteFace() ' is not possible from outside process boundaries to use the
' PictureProperty directly. the reason for is IPictureDisp:
' http://support.microsoft.com/kb/286460/de
' its not important is early or late binding or managed or unmanaged,
' the behaviour is always the same
' For example, a COMAddin running as InProcServer and can access the Picture Property ' add a button to the popup commandBarBtn = commandBarPopup.Controls.Add(MsoControlType.msoControlButton) commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption commandBarBtn.Caption = "commandBarButton" Clipboard.SetDataObject(Me.Icon.ToBitmap()) commandBarBtn.PasteFace() Dim clickHandler As Office.CommandBarButton_ClickEventHandler = _
AddressOf Me.commandBarBtn_Click AddHandler commandBarBtn.ClickEvent, clickHandler 'add a new toolbar commandBar = _excelApplication.CommandBars.Add("MyCommandBar", _
MsoBarPosition.msoBarTop, False, True) commandBar.Visible = True ' add a button to the toolbar commandBarBtn = commandBar.Controls.Add(MsoControlType.msoControlButton) commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption commandBarBtn.Caption = "commandBarButton" commandBarBtn.FaceId = 3 clickHandler = AddressOf Me.commandBarBtn_Click AddHandler commandBarBtn.ClickEvent, clickHandler ' add a dropdown box to the toolbar commandBarPopup = commandBar.Controls.Add(MsoControlType.msoControlPopup) commandBarPopup.Caption = "commandBarPopup" ' add a button to the popup, we use an own icon for the button commandBarBtn = commandBarPopup.Controls.Add(MsoControlType.msoControlButton) commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption commandBarBtn.Caption = "commandBarButton" Clipboard.SetDataObject(Me.Icon.ToBitmap()) commandBarBtn.PasteFace() clickHandler = AddressOf Me.commandBarBtn_Click AddHandler commandBarBtn.ClickEvent, clickHandler ' create context menu commandBarPopup = _excelApplication.CommandBars.Item("Cell").Controls.Add( _
MsoControlType.msoControlPopup) commandBarPopup.Caption = "commandBarPopup" ' add a button to the popup commandBarBtn = commandBarPopup.Controls.Add(MsoControlType.msoControlButton) commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption commandBarBtn.Caption = "commandBarButton" commandBarBtn.FaceId = 9 clickHandler = AddressOf Me.commandBarBtn_Click AddHandler commandBarBtn.ClickEvent, clickHandler Dim sheet As Excel.Worksheet = workBook.Worksheets(1) sheet.Cells(2, 2).Value = "this excel instance contains 3 custom menus" sheet.Cells(3, 2).Value = "the main menu, the toolbar menu and the cell context menu" sheet.Cells(4, 2).Value = "in this case the menus are temporaily created"
' make visible _excelApplication.Visible = True End Sub

Last edited May 14, 2012 at 5:03 AM by SebastianDotNet, version 7

Comments

No comments yet.