K
KenPalmer
My Outlook add-in code is breaking when attempting to save the command bar
position. Complete code for the add-in appears below. This is built in
Visual Studio 2008 for Outlook 2007.
Also, the Settings.settings properties are as follows:
CommandBarTop, int, 0
CommandBarLeft, int, 0
CommandBarVisible, bool, True
CommandBarPosition, 4
CommandBarRowIndex, 1
Everything works so far, except when the ThisAddIn_Shutdown() event calls
SaveCommandBarSettings(). Debugger shows that code fails when it hits
commandBar.Top.
Below are some error details. What's a little disturbing is that it appears
that a method which is internal to Microsoft Office is the point of failure.
Microsoft.Office.Core.CommandBar.get_Top()
Perhaps I need to repair Office (yet again). I repaired Office 2007
Ultimate once before when troubleshooting a completely different issue that
was caused by Visual Studio 2008 Power Tools.
=====================================
Exception from HRESULT: 0x800A01A8
System.Runtime.InteropServices.COMException was unhandled
Message="Exception from HRESULT: 0x800A01A8"
Source="office"
ErrorCode=-2146827864
StackTrace:
at Microsoft.Office.Core.CommandBar.get_Top()
InnerException:
=====================================
Any assistance would be appreciated. Again, the complete code is pasted
below so you can try to replicate the problem. Thanks for your help.
Ken
========================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace OutlookHW
{
/// <summary>
/// Adapted from the following:
/// http://msdn.microsoft.com/en-us/library/cc136646.aspx
/// http://msdn.microsoft.com/en-us/library/ms268891.aspx
/// </summary>
/// <remarks>
/// 6/10/2008 - Ken Palmer - Created
/// </remarks>
public partial class ThisAddIn
{
#region Hello World
// Create the command bar variables at the class level.
Office.CommandBar commandBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddToolbar();
LoadCommandBarSettings();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
SaveCommandBarSettings();
}
private void AddToolbar()
{
Outlook.Explorer myExplorer;
myExplorer = this.Application.ActiveExplorer();
try
{
commandBar = myExplorer.CommandBars["Test"];
}
catch (ArgumentException e)
{
// Toolbar named Test does not exist so we should create it.
}
if (commandBar == null)
{
// Add a commandbar named Test.
commandBar = myExplorer.CommandBars.Add("Test", 1, missing,
true);
}
try
{
// Add a button to the command bar and an event handler.
firstButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, missing);
firstButton.Style = Office.MsoButtonStyle.msoButtonCaption;
firstButton.Caption = "Hello";
firstButton.Tag = "btnHello";
firstButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(firstButton_Click);
// Add a second button to the command bar and an event
handler.
secondButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, missing);
secondButton.Style = Office.MsoButtonStyle.msoButtonCaption;
secondButton.Caption = "World";
secondButton.Tag = "btnWorld";
secondButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(secondButton_Click);
commandBar.Visible = true;
}
catch (ArgumentException e)
{
MessageBox.Show(e.Message);
}
}
void secondButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
MessageBox.Show("2nd button clicked: World");
}
void firstButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
MessageBox.Show("1st button clicked: Hello");
}
#region Load Command Bar Settings
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 :
Screen.PrimaryScreen.Bounds.Height / 2;
int left =
Convert.ToInt32(Properties.Settings.Default["CommandBarLeft"]);
commandBar.Left = left != 0 ? left :
Screen.PrimaryScreen.Bounds.Width / 2;
bool visible =
Convert.ToBoolean(Properties.Settings.Default["CommandBarVisible"]);
commandBar.Visible = visible;
}
#endregion
private void SaveCommandBarSettings()
{
try
{
if (commandBar != null)
{
MessageBox.Show(commandBar.Top.ToString());
}
if (commandBar != null)
{
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();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
position. Complete code for the add-in appears below. This is built in
Visual Studio 2008 for Outlook 2007.
Also, the Settings.settings properties are as follows:
CommandBarTop, int, 0
CommandBarLeft, int, 0
CommandBarVisible, bool, True
CommandBarPosition, 4
CommandBarRowIndex, 1
Everything works so far, except when the ThisAddIn_Shutdown() event calls
SaveCommandBarSettings(). Debugger shows that code fails when it hits
commandBar.Top.
Below are some error details. What's a little disturbing is that it appears
that a method which is internal to Microsoft Office is the point of failure.
Microsoft.Office.Core.CommandBar.get_Top()
Perhaps I need to repair Office (yet again). I repaired Office 2007
Ultimate once before when troubleshooting a completely different issue that
was caused by Visual Studio 2008 Power Tools.
=====================================
Exception from HRESULT: 0x800A01A8
System.Runtime.InteropServices.COMException was unhandled
Message="Exception from HRESULT: 0x800A01A8"
Source="office"
ErrorCode=-2146827864
StackTrace:
at Microsoft.Office.Core.CommandBar.get_Top()
InnerException:
=====================================
Any assistance would be appreciated. Again, the complete code is pasted
below so you can try to replicate the problem. Thanks for your help.
Ken
========================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace OutlookHW
{
/// <summary>
/// Adapted from the following:
/// http://msdn.microsoft.com/en-us/library/cc136646.aspx
/// http://msdn.microsoft.com/en-us/library/ms268891.aspx
/// </summary>
/// <remarks>
/// 6/10/2008 - Ken Palmer - Created
/// </remarks>
public partial class ThisAddIn
{
#region Hello World
// Create the command bar variables at the class level.
Office.CommandBar commandBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddToolbar();
LoadCommandBarSettings();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
SaveCommandBarSettings();
}
private void AddToolbar()
{
Outlook.Explorer myExplorer;
myExplorer = this.Application.ActiveExplorer();
try
{
commandBar = myExplorer.CommandBars["Test"];
}
catch (ArgumentException e)
{
// Toolbar named Test does not exist so we should create it.
}
if (commandBar == null)
{
// Add a commandbar named Test.
commandBar = myExplorer.CommandBars.Add("Test", 1, missing,
true);
}
try
{
// Add a button to the command bar and an event handler.
firstButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, missing);
firstButton.Style = Office.MsoButtonStyle.msoButtonCaption;
firstButton.Caption = "Hello";
firstButton.Tag = "btnHello";
firstButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(firstButton_Click);
// Add a second button to the command bar and an event
handler.
secondButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, missing);
secondButton.Style = Office.MsoButtonStyle.msoButtonCaption;
secondButton.Caption = "World";
secondButton.Tag = "btnWorld";
secondButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(secondButton_Click);
commandBar.Visible = true;
}
catch (ArgumentException e)
{
MessageBox.Show(e.Message);
}
}
void secondButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
MessageBox.Show("2nd button clicked: World");
}
void firstButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
MessageBox.Show("1st button clicked: Hello");
}
#region Load Command Bar Settings
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 :
Screen.PrimaryScreen.Bounds.Height / 2;
int left =
Convert.ToInt32(Properties.Settings.Default["CommandBarLeft"]);
commandBar.Left = left != 0 ? left :
Screen.PrimaryScreen.Bounds.Width / 2;
bool visible =
Convert.ToBoolean(Properties.Settings.Default["CommandBarVisible"]);
commandBar.Visible = visible;
}
#endregion
private void SaveCommandBarSettings()
{
try
{
if (commandBar != null)
{
MessageBox.Show(commandBar.Top.ToString());
}
if (commandBar != null)
{
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();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}