/// <summary> /// Click event trigger from created buttons. incoming call comes from word application thread. /// </summary> /// <param name="Ctrl"></param> /// <param name="CancelDefault"></param> void commandBarBtn_ClickEvent(NetOffice.OfficeApi.CommandBarButton Ctrl, ref bool CancelDefault) { string message = string.Format("Click from Button {0}.", Ctrl.Caption); MessageBox.Show(message, _progId, MessageBoxButtons.OK, MessageBoxIcon.Information); Ctrl.Dispose(); }
/// <summary> /// creates gui elements /// </summary> private void SetupGui() { /* create commandbar */ Office.CommandBar commandBar = _accessApplication.CommandBars.Add(_toolbarName, MsoBarPosition.msoBarTop, System.Type.Missing, true); commandBar.Visible = true; // add popup to commandbar Office.CommandBarPopup commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _toolbarPopupName; commandBarPop.Tag = _toolbarPopupName; // add a button to the popup Office.CommandBarButton commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _toolbarButtonName; commandBarBtn.Tag = _toolbarButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); /* create menu */ commandBar = _accessApplication.CommandBars["Menu Bar"]; // add popup to menu bar commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _menuName; commandBarPop.Tag = _menuName; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _menuButtonName; commandBarBtn.Caption = _menuButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); }
/// <summary> /// creates gui elements /// </summary> private void SetupGui() { /* * // How to: Add Commands to Shortcut Menus in Word * // http://msdn.microsoft.com/de-de/library/dd554969.aspx */ _wordApplication.CustomizationContext = _normalDotTemplate; /* create commandbar */ Office.CommandBar commandBar = _wordApplication.CommandBars.Add(_toolbarName, MsoBarPosition.msoBarTop, System.Type.Missing, true); commandBar.Visible = true; // add popup to commandbar Office.CommandBarPopup commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _toolbarPopupName; commandBarPop.Tag = _toolbarPopupName; // add a button to the popup Office.CommandBarButton commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _toolbarButtonName; commandBarBtn.Tag = _toolbarButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); /* create menu */ commandBar = _wordApplication.CommandBars["Menu Bar"]; // add popup to menu bar commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _menuName; commandBarPop.Tag = _menuName; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _menuButtonName; commandBarBtn.Tag = _menuButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); /* create context menu */ commandBarPop = (Office.CommandBarPopup)_wordApplication.CommandBars["Text"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _contextName; commandBarPop.Tag = _contextName; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = _contextMenuButtonName; commandBarBtn.Tag = _contextMenuButtonName; commandBarBtn.FaceId = 9; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); _normalDotTemplate.Saved = true; }
private void CreateTemporaryUserInterface() { /* * // How to: Add Commands to Shortcut Menus in Excel * // http://msdn.microsoft.com/en-us/library/0batekf4.aspx */ /* create commandbar */ Office.CommandBar commandBar = _excelApplication.CommandBars.Add(_toolbarName, MsoBarPosition.msoBarTop, System.Type.Missing, true); commandBar.Visible = true; // add popup to commandbar Office.CommandBarPopup commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _toolbarPopupName; commandBarPop.Tag = _toolbarPopupName; // add a button to the popup Office.CommandBarButton commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _toolbarButtonName; commandBarBtn.Tag = _toolbarButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); /* create menu */ commandBar = _excelApplication.CommandBars["Worksheet Menu Bar"]; // add popup to menu bar commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _menuName; commandBarPop.Tag = _menuName; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _menuButtonName; commandBarBtn.Tag = _menuButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); /* create context menu */ commandBarPop = (Office.CommandBarPopup)_excelApplication.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _contextName; commandBarPop.Tag = _contextName; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = _contextMenuButtonName; commandBarBtn.Tag = _contextMenuButtonName; commandBarBtn.FaceId = 9; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); }
private void buttonStartExample_Click(object sender, EventArgs e) { // start access _accessApplication = new Access.Application(); Office.CommandBarButton commandBarBtn = null; // create database name string fileExtension = GetDefaultExtension(_accessApplication); string documentFile = string.Format("{0}\\Example05{1}", _hostApplication, fileExtension); // delete old database if exists if (System.IO.File.Exists(documentFile)) { System.IO.File.Delete(documentFile); } // create database DAO.Database newDatabase = _accessApplication.DBEngine.Workspaces[0].CreateDatabase(documentFile, LanguageConstants.dbLangGeneral); // add a commandbar popup Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_accessApplication.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; #region few words, how to access the picture /* * 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 */ #endregion #region CommandBarButton // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(_hostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion // make visible _accessApplication.Visible = true; buttonStartExample.Enabled = false; buttonQuitExample.Enabled = true; }
/// <summary> /// Click event trigger from created buttons. incoming call comes from excel application thread. /// </summary> /// <param name="Ctrl"></param> /// <param name="CancelDefault"></param> private void commandBarBtn_ClickEvent(NetOffice.OfficeApi.CommandBarButton Ctrl, ref bool CancelDefault) { try { string message = string.Format("Click from Button {0}.", Ctrl.Caption); MessageBox.Show(message, _progId, MessageBoxButtons.OK, MessageBoxIcon.Information); Ctrl.Dispose(); } catch (Exception exception) { string message = string.Format("An error occured.{0}{0}{1}", Environment.NewLine, exception.Message); MessageBox.Show(message, _progId, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void SetupGui() { /* create commandbar */ Office.CommandBar commandBar = _outlookApplication.ActiveExplorer().CommandBars.Add("工具栏名称", MsoBarPosition.msoBarTop, System.Type.Missing, true); commandBar.Visible = true; // add popup to commandbar //Office.CommandBarPopup commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); //commandBarPop.Caption = CultureRes.ProductTitle; //commandBarPop.Tag = CultureRes.ProductTitle; // add a button to the popup LogonBtn = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true); LogonBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; LogonBtn.Picture = PictureConverter.IconToPicture(Properties.Resources.SampleIcon2); LogonBtn.Mask = PictureConverter.ImageToPicture(Properties.Resources.sampleicon2Mask); LogonBtn.Caption = "按钮"; //LogonBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(LoginBtn_ClickEvent); }
private void buttonStartExample_Click(object sender, EventArgs e) { // start access _accessApplication = new Access.Application(); CommonUtils utils = new CommonUtils(_accessApplication); Office.CommandBarButton commandBarBtn = null; // add a commandbar popup Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_accessApplication.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, null, null, null, true); commandBarPopup.Caption = "commandBarPopup"; #region few words, how to access the picture /* * 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 */ #endregion #region CommandBarButton // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, null, null, null, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion // make visible _accessApplication.Visible = true; buttonStartExample.Enabled = false; buttonQuitExample.Enabled = true; }
/// <summary> /// creates gui elements /// </summary> private void SetupGui() { /* * // How to: Add Commands to Shortcut Menus in Outlook * // http://office.microsoft.com/en-us/outlook-help/HV080807142.aspx */ /* create commandbar */ Office.CommandBar commandBar = _outlookApplication.ActiveExplorer().CommandBars.Add(_toolbarName, MsoBarPosition.msoBarTop, System.Type.Missing, true); commandBar.Visible = true; // add popup to commandbar Office.CommandBarPopup commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _toolbarPopupName; commandBarPop.Tag = _toolbarPopupName; // add a button to the popup Office.CommandBarButton commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _toolbarButtonName; commandBarBtn.Tag = _toolbarButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); /* create menu */ commandBar = _outlookApplication.ActiveExplorer().CommandBars["Menu Bar"]; // add popup to menu bar commandBarPop = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPop.Caption = _menuName; commandBarPop.Tag = _menuName; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPop.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.FaceId = 9; commandBarBtn.Caption = _menuButtonName; commandBarBtn.Tag = _menuButtonName; commandBarBtn.ClickEvent += new NetOffice.OfficeApi.CommandBarButton_ClickEventHandler(commandBarBtn_ClickEvent); }
private void Create(Vbe.VBE application) { var commandBars = application.CommandBars; Office.CommandBar commandBar = commandBars.Add("Share code from file system", MsoBarPosition.msoBarTop, null, true); commandBar.Visible = true; Office.CommandBarButton exportButton = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, null, null, null, true); exportButton.Style = MsoButtonStyle.msoButtonIconAndCaption; exportButton.FaceId = 9; exportButton.Caption = "Export Vba Code Modules"; exportButton.Visible = true; exportButton.ClickEvent += ExportButton_ClickEvent; Office.CommandBarButton importButton = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, null, null, null, true); importButton.Style = MsoButtonStyle.msoButtonIconAndCaption; importButton.FaceId = 9; importButton.Caption = "Import Vba Code Modules"; importButton.Visible = true; importButton.ClickEvent += ImportButton_ClickEvent; commandBars.Dispose(false); }
//private void RemoveUserInterface() //{ // // _excelApplication.CommandBars("Cell").Reset(); //} #region UI Trigger /// <summary> /// Click event trigger from created buttons. incoming call comes from excel application thread. /// </summary> /// <param name="Ctrl"></param> /// <param name="CancelDefault"></param> private void commandBarBtn_ClickEvent(NetOffice.OfficeApi.CommandBarButton Ctrl, ref bool CancelDefault) { try { Excel.Worksheet workSheet = (Excel.Worksheet)_excelApplication.ActiveWorkbook.ActiveSheet; workSheet.Cells[1, 1].Value = "ExcelVersion"; workSheet.Cells[1, 2].Value = _excelApplication.Version; // create a utils instance, not need for but helpful to keep the lines of code low NetOffice.ExcelApi.Tools.CommonUtils utils = new NetOffice.ExcelApi.Tools.CommonUtils(_excelApplication); // draw back color and perform the BorderAround method workSheet.Range("$B2:$B5").Interior.Color = utils.Color.ToDouble(Color.DarkGreen); workSheet.Range("$B2:$B5").BorderAround(XlLineStyle.xlContinuous, NetOffice.ExcelApi.Enums.XlBorderWeight.xlMedium, NetOffice.ExcelApi.Enums.XlColorIndex.xlColorIndexAutomatic); MessageBox.Show(_excelApplication.Version, "ExcelVersion", MessageBoxButtons.OK, MessageBoxIcon.Information); Ctrl.Dispose(); } catch (Exception exception) { string message = string.Format("An error occured.{0}{0}{1}", Environment.NewLine, exception.Message); MessageBox.Show(message, _progId, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public virtual void Click(NetOffice.OfficeApi.CommandBarButton ctrl, bool cancelDefault) { InvokerService.InvokeInternal.ExecuteMethod(this, "Click", ctrl, cancelDefault); }
private void buttonStartExample_Click(object sender, EventArgs e) { // start word and turn off msg boxes _wordApplication = new Word.Application(); _wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; Office.CommandBar commandBar = null; Office.CommandBarButton commandBarBtn = null; // add a new document _wordApplication.Documents.Add(); Word.Template normalDotTemplate = GetNormalDotTemplate(); _wordApplication.CustomizationContext = normalDotTemplate; // add a commandbar popup Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_wordApplication.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, MsoBarPosition.msoBarTop, System.Type.Missing, 1, true); commandBarPopup.Caption = "commandBarPopup"; #region few words, how to access the picture /* * 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 */ #endregion #region CommandBarButton // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new toolbar // add a new toolbar commandBar = _wordApplication.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true); commandBar.Visible = true; // add a button to the toolbar commandBarBtn = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 3; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); // add a dropdown box to the toolbar commandBarPopup = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup, we use an own icon for the button commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new ContextMenu // add a commandbar popup commandBarPopup = (Office.CommandBarPopup)_wordApplication.CommandBars["Text"].Controls.Add(MsoControlType.msoControlPopup, MsoBarPosition.msoBarTop, false, 1, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 9; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion normalDotTemplate.Saved = true; // make visible & set buttons _wordApplication.Visible = true; buttonStartExample.Enabled = false; buttonQuitExample.Enabled = true; }
private void commandBarBtn_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault) { textBoxEvents.BeginInvoke(_updateDelegate, new object[] { "Click called." }); Ctrl.Dispose(); }
private void ImportButton_ClickEvent(NetOffice.OfficeApi.CommandBarButton Ctrl, ref bool cancelDefault) { ImportRequested?.Invoke(); }
public TestResult DoTest() { Excel.Application application = null; DateTime startTime = DateTime.Now; try { Bitmap iconBitmap = new Bitmap(System.Reflection.Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("ExcelTestsCSharp.Test08.bmp")); application = COMObject.Create <Excel.Application>(COMObjectCreateOptions.CreateNewCore); application.DisplayAlerts = false; application.Workbooks.Add(); Excel.Worksheet sheet = application.Workbooks[1].Sheets[1] as Excel.Worksheet; // add a commandbar popup Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)application.CommandBars["Worksheet Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; #region CommandBarButton // add a button to the popup Office.CommandBarButton commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(iconBitmap); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new toolbar // add a new toolbar Office.CommandBar commandBar = application.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true); commandBar.Visible = true; // add a button to the toolbar commandBarBtn = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 3; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); // add a dropdown box to the toolbar commandBarPopup = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup, we use an own icon for the button commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(iconBitmap); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new ContextMenu // add a commandbar popup commandBarPopup = (Office.CommandBarPopup)application.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 9; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(); application.Dispose(); } } }
void commandBarBtn_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault) { Ctrl.Dispose(); }
private void buttonStartExample_Click(object sender, EventArgs e) { // start excel and turn off msg boxes _excelApplication = new Excel.Application(); _excelApplication.DisplayAlerts = false; Office.CommandBar commandBar = null; Office.CommandBarButton commandBarBtn = null; // add a new workbook Excel.Workbook workBook = _excelApplication.Workbooks.Add(); // add a commandbar popup Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_excelApplication.CommandBars["Worksheet Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; #region few words, how to access the picture /* * 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 */ #endregion #region CommandBarButton // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(_hostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new toolbar // add a new toolbar commandBar = _excelApplication.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true); commandBar.Visible = true; // add a button to the toolbar commandBarBtn = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 3; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); // add a dropdown box to the toolbar commandBarPopup = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup, we use an own icon for the button commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(_hostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new ContextMenu // add a commandbar popup commandBarPopup = (Office.CommandBarPopup)_excelApplication.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 9; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Display info Excel.Worksheet sheet = (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"; sheet.Cells[5, 2].Value = "they are not persistant and needs no unload event or something like this"; sheet.Cells[6, 2].Value = "you can also create persistant menus if you want"; #endregion // make visible & set buttons _excelApplication.Visible = true; buttonStartExample.Enabled = false; buttonQuitExample.Enabled = true; }
private void buttonStartExample_Click(object sender, EventArgs e) { // start powerpoint _powerApplication = new PowerPoint.Application(); Office.CommandBar commandBar = null; Office.CommandBarButton commandBarBtn = null; // add a new presentation with one new slide PowerPoint.Presentation presentation = _powerApplication.Presentations.Add(MsoTriState.msoTrue); PowerPoint.Slide slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank); // add a commandbar popup Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_powerApplication.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; #region few words, how to access the picture /* * 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 */ #endregion #region CommandBarButton // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new toolbar // add a new toolbar commandBar = _powerApplication.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true); commandBar.Visible = true; // add a button to the toolbar commandBarBtn = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 3; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); // add a dropdown box to the toolbar commandBarPopup = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup, we use an own icon for the button commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new ContextMenu // add a commandbar popup commandBarPopup = (Office.CommandBarPopup)_powerApplication.CommandBars["Frames"].Controls.Add( MsoControlType.msoControlPopup, Missing.Value, Missing.Value, Missing.Value, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, Missing.Value, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 9; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion // make visible & set buttons _powerApplication.Visible = MsoTriState.msoTrue; buttonStartExample.Enabled = false; buttonQuitExample.Enabled = true; }
private void buttonStartExample_Click(object sender, EventArgs e) { // start outlook _outlookApplication = new Outlook.Application(); Office.CommandBar commandBar = null; Office.CommandBarButton commandBarBtn = null; Outlook._NameSpace outlookNS = _outlookApplication.GetNamespace("MAPI"); Outlook.MAPIFolder inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox); inboxFolder.Display(); // add a commandbar popup Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_outlookApplication.ActiveExplorer().CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; #region few words, how to access the picture /* * 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 */ #endregion #region CommandBarButton // add a button to the popup commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(_hostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion #region Create a new toolbar // add a new toolbar commandBar = _outlookApplication.ActiveExplorer().CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true); commandBar.Visible = true; // add a button to the toolbar commandBarBtn = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; commandBarBtn.FaceId = 3; commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); // add a dropdown box to the toolbar commandBarPopup = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarPopup.Caption = "commandBarPopup"; // add a button to the popup, we use an own icon for the button commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true); commandBarBtn.Style = MsoButtonStyle.msoButtonIconAndCaption; commandBarBtn.Caption = "commandBarButton"; Clipboard.SetDataObject(_hostApplication.DisplayIcon.ToBitmap()); commandBarBtn.PasteFace(); commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click); #endregion // set buttons buttonStartExample.Enabled = false; buttonQuitExample.Enabled = true; }