public void CTPFactoryAvailable(object CTPFactoryInst) { try { Office.ICTPFactory ctpFactory = new NetOffice.OfficeApi.ICTPFactory(_outlookApplication, CTPFactoryInst); Office._CustomTaskPane taskPane = ctpFactory.CreateCTP(typeof(Addin).Assembly.GetName().Name + ".SampleControl", "NetOffice Sample Pane(CS4)", Type.Missing); taskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight; taskPane.Width = 300; taskPane.Visible = true; _sampleControl = taskPane.ContentControl as SampleControl; ctpFactory.Dispose(); } catch (Exception exception) { string message = string.Format("An error occured.{0}{0}{1}", Environment.NewLine, exception.Message); MessageBox.Show(message, _prodId, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void CTPFactoryAvailable(object CTPFactoryInst) { try { Office.ICTPFactory ctpFactory = new NetOffice.OfficeApi.ICTPFactory(_accessApplication, CTPFactoryInst); Office._CustomTaskPane taskPane = ctpFactory.CreateCTP(typeof(Addin).Assembly.GetName().Name + ".SampleControl", "NetOffice Sample Pane(CS35)", Type.Missing); taskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight; taskPane.Width = 300; taskPane.Visible = true; _sampleControl = taskPane.ContentControl as SampleControl; ctpFactory.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); } }
/// <summary> /// Create taskpanes /// </summary> /// <typeparam name="T">Taskpane interface type from</typeparam> /// <typeparam name="N">Current host application</typeparam> /// <param name="factory">current used factory</param> /// <param name="ctpFactoryInst">taskpane creator given from office application</param> /// <param name="taskPanes">taskpane you want to create</param> /// <param name="taskPaneInstances">create taskpane instances</param> /// <param name="onError">Error callback if somthings fails</param> /// <param name="application">host application in base definition</param> public void CreateCustomPanes <T, N>(Core factory, object ctpFactoryInst, OfficeApi.Tools.CustomTaskPaneCollection taskPanes, List <T> taskPaneInstances, NetOffice.Tools.OnErrorHandler onError, COMObject application) where T : class where N : COMObject { OfficeApi.ICTPFactory TaskPaneFactory = new NetOffice.OfficeApi.ICTPFactory(factory, null, ctpFactoryInst); try { foreach (TaskPaneInfo item in taskPanes) { string title = item.Title; OfficeApi.CustomTaskPane taskPane = CreateCTP(factory, TaskPaneFactory, item.Type.FullName, title, onError); if (null == taskPane) { continue; } Type taskPaneType = taskPane.GetType(); item.Pane = taskPane; item.AssignEvents(); item.IsLoaded = true; switch (taskPane.DockPosition) { case NetOffice.OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionLeft: case NetOffice.OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionRight: taskPane.Width = item.Width >= 0 ? item.Width : TaskPaneInfo.DefaultSize; break; case NetOffice.OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionTop: case NetOffice.OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionBottom: taskPane.Height = item.Height >= 0 ? item.Height : TaskPaneInfo.DefaultSize; break; case NetOffice.OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionFloating: item.Width = item.Width >= 0 ? item.Width : TaskPaneInfo.DefaultSize; taskPane.Height = item.Height >= 0 ? item.Height : TaskPaneInfo.DefaultSize; break; default: break; } T pane = taskPane.ContentControl as T; if (null != pane) { taskPaneInstances.Add(pane); object[] argumentArray = new object[0]; if (item.Arguments != null) { argumentArray = item.Arguments; } try { OfficeApi.Tools.ITaskPaneConnection <N> foo = pane as OfficeApi.Tools.ITaskPaneConnection <N>; foo.OnConnection(application as N, taskPane, argumentArray); } catch (Exception exception) { factory.Console.WriteException(exception); } } foreach (KeyValuePair <string, object> property in item.ChangedProperties) { if (property.Key == "Title") { continue; } try { if (property.Key == "Width") // avoid to set width in top and bottom align { object outValue = null; item.ChangedProperties.TryGetValue("DockPosition", out outValue); if (null != outValue) { OfficeApi.Enums.MsoCTPDockPosition position = (OfficeApi.Enums.MsoCTPDockPosition)Enum.Parse(typeof(OfficeApi.Enums.MsoCTPDockPosition), outValue.ToString()); if (position == OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionTop || position == OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionBottom) { continue; } } } if (property.Key == "Height") // avoid to set height in left and right align { object outValue = null; item.ChangedProperties.TryGetValue("DockPosition", out outValue); if (null == outValue) { outValue = OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionRight; // NetOffice default position if unset } OfficeApi.Enums.MsoCTPDockPosition position = (OfficeApi.Enums.MsoCTPDockPosition)Enum.Parse(typeof(OfficeApi.Enums.MsoCTPDockPosition), outValue.ToString()); if (position == OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionLeft || position == OfficeApi.Enums.MsoCTPDockPosition.msoCTPDockPositionRight) { continue; } } taskPaneType.InvokeMember(property.Key, System.Reflection.BindingFlags.SetProperty, null, taskPane, new object[] { property.Value }); } catch { factory.Console.WriteLine("Failed to set TaskPane property {0}", property.Key); } } } } catch (Exception) { throw; } finally { if (null != TaskPaneFactory) { TaskPaneFactory.Dispose(); TaskPaneFactory = null; } } }