How to: Maintain Position Information for Custom Toolbars between Outlook Sessions

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2003

  • Outlook 2007

For more information, see Features Available by Application and Project Type.

This example demonstrates one possible way to save and restore the position of an Outlook custom toolbar. The code creates a toolbar and saves its position in a user settings file when the user exits Outlook. Restarting Outlook recreates the toolbar and sets its position based on the saved settings.

Office.CommandBar commandBar;
Office.CommandBarButton button;

private const string TOOLBARNAME = "ExampleBar";
private Outlook.Explorer explorer;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    explorer = this.Application.ActiveExplorer();

    ((Outlook.ExplorerEvents_Event)explorer).Close += 
        new Microsoft.Office.Interop.Outlook.ExplorerEvents_CloseEventHandler(ThisAddIn_Close);

    if (explorer != null)
    {
        commandBar = this.Application.ActiveExplorer().CommandBars.Add(TOOLBARNAME,
            Office.MsoBarPosition.msoBarFloating, false, true);

        button = commandBar.Controls.Add(
            Office.MsoControlType.msoControlButton,
            System.Type.Missing, System.Type.Missing,
            1, true) as Office.CommandBarButton;

        button.Style =
            Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
        button.Caption = "Button 1";
        button.Tag = "newButton";

        button.Click += new Microsoft.Office.Core.
            _CommandBarButtonEvents_ClickEventHandler(button_Click);

        LoadCommandBarSettings();
    }
}

private void LoadCommandBarSettings()
{
    Microsoft.Office.Core.MsoBarPosition position =
       (Microsoft.Office.Core.MsoBarPosition)Properties.Settings
       .Default["CommandBarPosition"];

    commandBar.Position = position;

    int rowIndex =
        Convert.ToInt32(
        Properties.Settings.Default["CommandBarRowIndex"]);

    commandBar.RowIndex = rowIndex;

    int top =
        Convert.ToInt32(
        Properties.Settings.Default["CommandBarTop"]);

    commandBar.Top =
        top != 0 ? top : System.Windows.Forms.Screen.PrimaryScreen.
        Bounds.Height / 2;

    int left =
        Convert.ToInt32(
        Properties.Settings.Default["CommandBarLeft"]);

    commandBar.Left =
        left != 0 ? left : System.Windows.Forms.Screen.PrimaryScreen.
        Bounds.Width / 2;

    bool visible = Convert.ToBoolean(
        Properties.Settings.Default["CommandBarVisible"]);

    commandBar.Visible = visible;
}

private void SaveCommandBarSettings()
{
    Properties.Settings.Default["CommandBarTop"] = commandBar.Top;
    Properties.Settings.Default["CommandBarLeft"] = commandBar.Left;
    Properties.Settings.Default["CommandBarVisible"] =
        commandBar.Visible;
    Properties.Settings.Default["CommandBarPosition"] =
        (int)commandBar.Position;
    Properties.Settings.Default["CommandBarRowIndex"] =
        commandBar.RowIndex;
    Properties.Settings.Default.Save();
}

void button_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
    ref bool CancelDefault)
{
    System.Windows.Forms.MessageBox.Show("Hello world!");
}

void ThisAddIn_Close()
{
    SaveCommandBarSettings();
}

This example requires:

  • Five user settings. Double-click the Settings.settings icon in the project’s Properties (in C#) or My Project (in Visual Basic) folder and add the following user-scoped properties:

    • CommandBarTop, type int, default value = 0

    • CommandBarLeft, type int, default value = 0

    • CommandBarVisible, type boolean, default value = true

    • CommandBarPosition, type int, default value = 4

    • CommandBarRowIndex, type int, default value = 1

Community Additions

ADD
Show: