public void Run() { // NetOffice Core supports so-called managed C# dynamic // with proxy management services. No need for additional NetOffice Api assemblies. // NetOffice want convert a proxy to COMDynamicObject each time if its failed to resolve // a corresponding wrapper type. // Note: Reference to Microsoft.CSharp is required. dynamic application = new COMDynamicObject("Excel.Application"); application.DisplayAlerts = false; var book = application.Workbooks.Add(); foreach (var sheet in book.Sheets) { Console.WriteLine(sheet); } // quit and dispose all open proxies application.Quit(); application.Dispose(); // -- no proxies open anymore -- HostApplication.ShowFinishDialog(); }
/// <summary> /// Creates a new ICOMObject based on classType of comProxy /// </summary> /// <param name="caller">parent there have created comProxy</param> /// <param name="comProxy">new created proxy</param> /// <param name="allowDynamicObject">allow to create a COMDynamicObject instance if its failed to resolve the wrapper type</param> /// <returns>corresponding wrapper class instance or plain COMObject</returns> /// <exception cref="ArgumentNullException">comProxy arguments is null</exception> /// <exception cref="CreateInstanceException">throws when its failed to create new instance</exception> /// <exception cref="FactoryException">throws when its failed to find the corresponding factory. this indicates a missing netoffice api assembly</exception> /// <exception cref="NetOfficeInitializeException">unexpected initialization error. see inner exception(s) for details</exception> public virtual ICOMObject CreateObjectFromComProxy(ICOMObject caller, object comProxy, bool allowDynamicObject) { if (null == caller) { throw new ArgumentNullException("caller"); } CheckInitialize(); try { ICOMObject result = null; Guid typeId = Guid.Empty; Guid componentId = Guid.Empty; CoreTypeExtensions.GetComponentAndTypeId(this, comProxy, ref componentId, ref typeId); lock (_createComObjectLock) { // get type factory first to handle possible duplicate type ITypeFactory factoryInfo = CoreFactoryExtensions.GetTypeFactory(this, caller, comProxy, componentId, typeId, false); if (null != factoryInfo) { TypeInformation typeInfo = CoreTypeExtensions.GetTypeInformationForUnknownObject(this, factoryInfo, typeId, comProxy); if (null == typeInfo) { throw new FactoryException(String.Format("Unable to resolve proxy type:{0}", ComTypes.TypeDescriptor.GetFullComponentClassName(comProxy))); } result = CoreCreateExtensions.CreateInstance(this, typeInfo, caller, comProxy); //result = InternalObjectActivator.TryReplaceInstance(caller, result); } else { result = CoreCreateExtensions.TryCreateObjectByResolveEvent(this, caller, null, comProxy); if (null == result) { if (allowDynamicObject && Settings.EnableDynamicObjects) { result = InternalObjectActivator.RaiseCreateCOMDynamic(caller, comProxy); if (null == result) { result = new COMDynamicObject(caller, comProxy); } } else { throw new FactoryException(String.Format("Unable to resolve proxy type:{0}", ComTypes.TypeDescriptor.GetFullComponentClassName(comProxy))); } } } return(result); } } catch (Exception throwedException) { Console.WriteException(throwedException); throw; } }
internal void Run() { dynamic application = new COMDynamicObject("Excel.Application"); application.DisplayAlerts = false; var book = application.Workbooks.Add(); Excel.Workbook convertedBook = book as Excel.Workbook; Console.WriteLine("book converted {0}", null != convertedBook); foreach (var sheet in book.Sheets) { Console.WriteLine(sheet); } application.Quit(); application.Dispose(); }
internal void Run() { try { NetOffice.Settings.Default.EnableAutomaticQuit = true; using (dynamic application = new COMDynamicObject("Excel.Application")) { application.Visible = true; application.Workbooks.Add(); dynamic dynamicBooks = new COMDynamicObject(application.Workbooks.UnderlyingObject); var book = dynamicBooks[1]; bool bookActive = application.ActiveWorkbook == application.Workbooks[1]; } } catch (Exception exception) { Console.WriteLine(exception); } }
internal void Test() { try { Type wordType = System.Type.GetTypeFromProgID("Word.Application", true); object interopProxy = Activator.CreateInstance(wordType); COMDynamicObject.TryConvertFailResult = true; dynamic application = new COMDynamicObject(interopProxy); application.Visible = true; application.DisplayAlerts = 0; var document = application.Documents.Add(); application.Selection.TypeText("Hello World"); int left = 0; int top = 0; int width = 0; int height = 0; dynamic window = application.ActiveWindow; dynamic range = application.Selection.Range; window.GetPoint(out left, out top, out width, out height, range); MessageBox.Show(string.Format("GetPoint returns Left:{0} Top:{1} Width:{2} Height:{3}", left, top, width, height)); document.Saved = true; application.Quit(); application.Dispose(); } catch (Exception exception) { Console.WriteLine(exception); } Console.WriteLine("Press any key."); Console.ReadKey(); }