Create CustomTaskPanes in Office AddIns with C#

To create you own workspace in an Office application like Outlook you need a CustomTaskPane. To call a CustomTaskPane you need to create at first a UserControl. In that UserControl you can build your own layout. After that you need to create a class called TaskPanes. This class contains two functions, one to create a CustomTaskPane and one to remove it. In the create-function you are creating an object of your UserControl and a CustomTaskPane-object. By creating the CustomTaskPane you are giving the UserControl and a headline over.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class TaskPanes
{
 public void createTaskPane()
 {
  //Globals.ThisAddIn is a automatical created class for the add in
  Globals.ThisAddIn.myUserControl = new myUserControl();
  
  //call your custom UserControl and give it a headline
  Globals.ThisAddIn.myCustomTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(Globals.ThisAddIn.myUserControl, "Task Pane");

  //important is that you give a with and make it visible
  Globals.ThisAddIn.myCustomTaskPane.Width = 405;
  Globals.ThisAddIn.myCustomTaskPane.Visible = true;           
 }

 public void removeTaskPane()
 {
  Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.myCustomTaskPane);
 }
}

The next step is to find a generated class called 'ThisAddIn'. You can find it in the projectexplorer.






This class is the main class of an add in in Office. In here you can add some code. You need to add two public objects. One is your UserControl and the other one is the CustomTaskPane. Also you need to call the TaskPane-class. After that you need to search the startup event. This one is event is the first event that will be called. Call the createTaskPane function of the TaskPane class in this event and the result will be your own CustomTaskPane.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
TaskPanes taskpanes = new TaskPanes();
public Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
public myUserControl myUserControl;

//the startup event of addins
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
 //create a custom taskpane
 taskpanes.createTaskPane();          
}

Comments

Popular posts from this blog

How to support multiple languages in WPF