// get data model from the sample workbook private string GetFirstWorkbookTable() { Excel.Application excel = null; Excel.Workbook workbook = null; Office.COMAddIn addin = null; AddIn.IDataModel dataModel = null; AddIn.IDataTable[] tables = null; try { // open the sample workbook this.Cursor = Cursors.WaitCursor; label1.Text = "Getting Data Model..."; excel = new Excel.Application(); workbook = excel.Workbooks.Open(samplePath); // get Excel add-in COM object addin = excel.COMAddIns.Item("C1.DataEngine.ExcelAddIn"); dataModel = (AddIn.IDataModel)addin.Object; // get data model tables tables = dataModel.GetModelTables(excel.ActiveWorkbook); // import data model tables to C1 DataEngine tables label1.Text = "Importing Data Model..."; dataModel.ExportToDataEngine(tables, dataPath); // initialize C1 DataEngine workspace // that must be done after export since export works in Excel process _c1FlexPivotPage.FlexPivotPanel.Workspace.Initialize += Workspace_Initialize; _c1FlexPivotPage.FlexPivotPanel.Workspace.Init(dataPath); // returns the first imported table name return(tables[0].Alias); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } finally { // release COM objects ReleaseObject(tables); ReleaseObject(dataModel); ReleaseObject(addin); if (workbook != null) { workbook.Close(false); ReleaseObject(workbook); workbook = null; } if (excel != null) { excel.Quit(); ReleaseObject(excel); excel = null; } this.Cursor = Cursors.Default; label1.Text = string.Empty; } }
//private Excel.Application XLApp; private void ButtonInvokeAddIn_Click(object sender, RoutedEventArgs e) { // Creates the text file that the trace listener will write to. //FileStream myTraceLog = new FileStream( @"C:\myTraceLog.txt", FileMode.OpenOrCreate ); // Creates the new trace listener. //TextWriterTraceListener myListener = new TextWriterTraceListener( myTraceLog ); try { XLApp = new Excel.Application() { Visible = true }; //Thread.Sleep( 3000 ); XLApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet); Object addInName = "ComServiceOleMarshal"; Office.COMAddIn addIn = XLApp.COMAddIns.Item(ref addInName); while (utils == null) { Thread.Sleep(100); utils = (ComServiceOleMarshal.IAddInUtilities)addIn.Object; } utils.CurrentShowThread(); ComServiceOleMarshal.SomeObject o = new ComServiceOleMarshal.SomeObject(123, "Hello"); utils.DoSomething(o); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { // Flushes the buffers of all listeners in the Listeners collection. //Trace.Flush(); //Flushes only the buffer of myListener. //myListener.Flush(); //myTraceLog.Close(); //And at the end ( 필터링 : reject callee ) //MessageFilter.Revoke(); //if (XLApp != null) //{ // XLApp.Quit(); // XLApp = null; // utils = null; //} //GC.Collect(); //GC.WaitForPendingFinalizers(); //GC.Collect(); //GC.WaitForPendingFinalizers(); } }
private void btnActivar_Click(object sender, RibbonControlEventArgs e) { // el nombre de una Key debe incluir un root valido. Cursor.Current = Cursors.WaitCursor; const string userRoot = "HKEY_CURRENT_USER"; const string subkey = "Software\\Microsoft\\Office\\Excel\\Addins\\SAT.Dictamenes.SIPRED.Client"; const string keyName = userRoot + "\\" + subkey; object addInName = "SAT.Dictamenes.SIPRED.Client"; Registry.SetValue(keyName, "LoadBehavior", 3); Office.COMAddIn addIn = Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName); addIn.Connect = true; MessageBox.Show("El AddIn [SAT] quedó habilitado, continue el proceso con SIPRED; para abrir archivos XSPR.", "AddIn SAT", MessageBoxButtons.OK, MessageBoxIcon.Information); Cursor.Current = Cursors.Default; }
private void btnDesactivar_Click(object sender, RibbonControlEventArgs e) { // el nombre de una Key debe incluir un root valido. Cursor.Current = Cursors.WaitCursor; const string userRoot = "HKEY_CURRENT_USER"; const string subkey = "Software\\Microsoft\\Office\\Excel\\Addins\\SAT.Dictamenes.SIPRED.Client"; const string keyName = userRoot + "\\" + subkey; object addInName = "SAT.Dictamenes.SIPRED.Client"; Registry.SetValue(keyName, "LoadBehavior", 0); Office.COMAddIn addIn = Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName); addIn.Connect = false; MessageBox.Show("El AddIn [SAT] quedó deshabilitado", "AddIn SAT", MessageBoxButtons.OK, MessageBoxIcon.Information); Excel.Workbook nwbook = Globals.ThisAddIn.Application.Workbooks.Add(System.Type.Missing); nwbook.Activate(); Cursor.Current = Cursors.Default; }
public GUI() { InitializeComponent(); OpenOutlookIfNotRunning(); string methodTag = "GUI"; try { object addInName = "OutlookAddIn"; Outlook.Application outlookApp = new Outlook.Application(); addIn = outlookApp.COMAddIns.Item(ref addInName); outlookApp = null; } catch (COMException ex) { LogWriter.WriteException(TAG, methodTag, ex); Environment.Exit(-1); } }
/// <summary> /// Add a menu item. /// </summary> /// <param name="xlApp">The excel application to add the menu item to.</param> /// <param name="addin">The add-in instance to call when there is a menu item event.</param> /// <param name="menuName">The name of the menu to add the menu item to.</param> /// <param name="menuItemCaption">The caption of the new menu item.</param> /// <param name="menuItemKey">The key of the menu item.</param> /// <returns>The created menu item object.</returns> public static Office.CommandBarButton AddMenuItem(Excel.Application xlApp, Office.COMAddIn addin, string menuName, string menuItemCaption, string menuItemKey) { Office.CommandBar cmdBar; Office.CommandBarButton button; // Make variable for 'missing' arguments. This is not the same as null. object missing = Type.Missing; // Get the "menuName" menu. cmdBar = xlApp.CommandBars[menuName]; // If menu item not found then exit. if (cmdBar == null) { return(null); } // Try to get the "menuItemCaption" menu item. button = cmdBar.FindControl(missing, missing, menuItemKey, missing, missing) as Office.CommandBarButton; // If menu item not found, add it. if (button == null) { // Add new button (menu item). button = cmdBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, menuItemKey, missing, missing) as Office.CommandBarButton; // Set button's Caption, Tag, Style, and OnAction properties. button.Caption = menuItemCaption; button.Tag = menuItemKey; button.Style = Office.MsoButtonStyle.msoButtonCaption; // Use addin argument to return reference to this add-in. button.OnAction = "!<" + addin.ProgId + ">"; } // Return the created menu item. return(button); }
/// <summary> /// Implements the OnConnection method of the IDTExtensibility2 interface. /// Receives notification that the Add-in is being loaded. /// </summary> /// <param term='application'> /// Root object of the host application. /// </param> /// <param term='connectMode'> /// Describes how the Add-in is being loaded. /// </param> /// <param term='addInInst'> /// Object representing this Add-in. /// </param> /// <seealso class='IDTExtensibility2' /> public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { // Store the Excel host application. Exit if host was not Excel. m_xlApp = application as Excel.Application; if (m_xlApp == null) { return; } // If an addInInst object given of the type COMAddin then it was loaded as COM add-in. // If addInInst is the same object as myself, then I'm loaded as Automation add-in. if (addInInst != this) { // Attach myself to the add-in object. // In that way I can call functions of this object from VBA using the add-in collection. Office.COMAddIn cai = addInInst as Office.COMAddIn; cai.Object = this; // Now install menu item and add event handler. m_menuItem = AddInUtils.AddMenuItem(m_xlApp, cai, m_menuName, m_menuItemCaption, m_menuItemKey); m_menuItem.Click += new Office._CommandBarButtonEvents_ClickEventHandler(MyMenuHandler); } }
public void MyTestInitialize() { this.excel = new Excel.Application(); //var dir = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Microsoft Office", "Office14"); //var path = System.IO.Path.Combine(dir, "EXCEL.EXE"); //var process = System.Diagnostics.Process.Start(path); //for (int i = 0; i < 10; i++) //{ // System.Threading.Thread.Sleep(1000); // this.excel = Marshal.GetActiveObject("Excel.Application") as Excel.Application; // if (null != this.excel) // { // break; // } //} Assert.IsNotNull(this.excel); this.excel.Visible = true; try { Office.COMAddIn addin = excel.COMAddIns.Item("SeleniumExcelAddIn"); this.app = (IAppContext)addin.Object; Assert.IsNotNull(this.app); } catch { this.excel.Quit(); throw; } excel.Workbooks.Add(); }
public UnitTest1() { try { _wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"); } catch { System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office 15\root\office15\WINWORD.EXE"); while (true) { try { _wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"); break; } catch { } } } _comAddin = WordApp.COMAddIns.Item("TestAddin.Connect"); _myAddin = (IMyAddIn) ComAddin.Object; }