Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Resize your custom task panes in MS Office applications

4.33/5 (5 votes)
20 Aug 2013CPOL1 min read 21.2K   914  
This article briefly explains about resizing of custom task panes in Execl/Word/powerpoint

Introduction

There is no resize event for custom task panes in Excel/Word/PowerPoint. If you want to resize your custom task panes using COM addin in C# when the parent application (Excel/Word/PowerPoint) is resized i.e., maximized, minimized or windows resolution changed, you are at the correct place.

Background

The basics for creating COM addins in C# for MS Office applications is already available in CodeProject itself. So I assume that the following prerequisites are met,

  1. A custom button is added to MS office ribbon
  2. A custom task pane is created after clicking the custom button
  3. Taskpane contains a user control which has the necessary controls as per your requirement. 

Using the code

I have attached a sample MSI and source code for this sample. This example contains two user controls named MyUserControl and MyUserControl2. First one is dummy control and the second one contains the sample controls like lable and textbox.

The ShowCustomTaskPane function creates a custom task pane with MyUserControl and adds MyUserControl2 dynamically. The "Connect" is derived from "ITaskPane" interface which is used for interaction between Custom Taskpane and MyUserControl2.

When MyUserControl2 loads, its controls' sizes are adjusted according to the TaskPane size. MyUserControl2 constructor stores the current resolution. When the Office application is resized, MyUserControl2_Resize function is called. This function is responsible for adjusting the controls size based on resolution and scroll bar visibility.

C#
private void MyUserControl2_Resize(object sender, EventArgs e)
{
    if (m_IsCtrlLoading)
        return;

    if (this.Width > 300)
    {
        //Dont allow the width of the task pane to be modified. 
        if (m_ObjItaskPane != null)
            m_ObjItaskPane.SetTaskPaneDim(300, 0);
        return;
    }

    if (!(sender is ScrollableControl))
        return;

    // If Scroll bar exists, dont adjust the size of controls.
    // If resolution changed, you must adjust the controls 
    ScrollableControl scrllCtrl = sender as ScrollableControl;
    if (IsResolutionChanged() || scrllCtrl.VerticalScroll.Visible == false)
        AdjustUserControl();
}

If scrollbar in taskbar is visible, we no need to adjust the controls. If resolution is changed, adjusting the controls' size become mandatory. The width of the TaskPane is kept constant because horizontal scroll bar will take care of the control's visibility in TaskPane. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)