Powerpoint 2007 Beta 2 : context menu not created by plugin

O

Olivier B.

Hi,
We are adding a plugin in Office (powerpoint, word, Excel and Outlook) and
everything works fine for Office 2003 and lower versions.
With Office 2007, we have an issue with powerpoint where the context menu
associated with our functions is not created. For Word, Excel and Outlook, it
works fine. My problem is that we are using the same code to load the context
menu items for each application (our base plugin class is doing it).

All we are doing is adding buttons to our popup command bar.

Does anybody have an idea or has encountered the same problem ?

Olivier
 
O

Olivier B.

I'm not sure I have posted in the right group. I will copy my post in
mac.office.powerpoint which seems to be more developpement oriented.

What means mac in mac.office.powerpoint ?
 
O

Olivier B.

Yes,
You understood correctly. It's about a right-clic context menu, but it
doesn't work.
Everything seems fine in the code and it does even work for word and excel
but not with powerpoint.

I have tried to install Office 2003 on the same Vista machine, and used the
same plugin. It works fine with powerpoint 2003 ...

I have traced my plugin, and no error is raised.

Olivier
 
P

Patrick Schmid

Have you tried this on Windows XP? I'd blame Vista for this issue.

Patrick Schmid
 
O

Olivier B.

I have tried it on XP but not with Office 2007.

I will try it and I will let you know the results.
 
O

Olivier B.

I will have to wait before having a test machine with Windows XP and Office
2007.

Meanwhile, I created a sample project with an "hello world" Add-in.

I installed my plugin on Vista / Office 2007, as well as my real plugin.

In Word, everything works fine : I've got my add-in and the other tool bar in
the Add-In ribbon, and both context menus are present.

In Excel, both add-ins are present in the add_ins ribbon and work fine. The
context menus text is displayed 5 times in the menu commands panel in the add-
ins ribbon (it's a bug we already have discovered with Excel 2007), but work
normaly on a right clic.

In Outlook, both add-ins are present in the main windows and work fine.

In powerpoint, both add-ins are present in the add_ins ribbon and work fine.
No
context menu appears.

So there seems to be a bug in powerpoint 2007 ...

Here is the code of my Connect.cs class :

***************************************************************
namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections;


#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("D9305294-26DD-44B8-AD2A-2A822B98DCDA"),
ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

private object applicationObject;
private object addInInstance;
private CommandBarButton MyButton;
private CommandBars oCommandBars;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
CommandBar oStandardBar;

try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "My Custom Button";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
MyButton.OnAction = "!<MyCOMAddin.Connect>";

MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

CreateOrAttachContextMenuItems();

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");

oStandardBar = null;
}

/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
object omissing = System.Reflection.Missing.Value ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
DeleteContextMenuItems();
MyButton = null;
oCommandBars = null;
}

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }

#region Manage context menus
virtual protected void CreateOrAttachContextMenuItems()
{

// TODO: handle correctly partial attachment scenarios
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControls controls =
oCommandBars.FindControls(MsoControlType.msoControlButton,
Type.Missing,
tag,Type.Missing);
if(controls == null)
{
System.Diagnostics.Debug.WriteLine("Create context menu for " + tag);
CreateContextMenuItems();
}
}
}


protected void CreateContextMenuItems()
{
foreach(CommandBar commandBar in oCommandBars)
{
if(commandBar.Type == MsoBarType.msoBarTypePopup &&
commandBar.Name!="System")
{
System.Diagnostics.Debug.WriteLine("Create context menu in command bar
: " + commandBar.Name);
try
{
CreateContextMenuItems(commandBar);
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}

}

protected void CreateContextMenuItems(CommandBar commandBar)
{
AddContextMenuButton(commandBar, "Button 1 " +
commandBar.Name.ToString(), "MyAddin1.Context.Translate").BeginGroup = true;
AddContextMenuButton(commandBar, "Button 2",
"MyAddin1.Context.TranslateAndReplace");
AddContextMenuButton(commandBar, "Button 3", "MyAddin1.Context.Lookup");
}

protected Hashtable ContextMenuItems
{
get
{
Hashtable fctval = new Hashtable();
fctval["MyAddin1.Context.Translate"] = "First context menu item";
fctval["MyAddin1.Context.TranslateAndReplace"] ="Second context menu
item";
fctval["MyAddin1.Context.Lookup"] = "Third context menu item";
return fctval;
}
}

protected void DeleteContextMenuItems()
{
for(int j = 1; j <= oCommandBars.Count; j++)
{
CommandBar commandBar = oCommandBars[j];
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControl controls =
commandBar.FindControl(MsoControlType.msoControlButton,
Type.Missing,
tag,true,false);
if(controls != null)
{
controls.Delete(false);
System.Diagnostics.Debug.WriteLine("bar = " + commandBar.Name + " tag
removed = " + tag);
}
}
}
}

/// <summary>
/// Adds a button to a context menu
/// </summary>
/// <param name="caption">String to display as the caption and tool
tip</param>
/// <param name="image">String indicating the image resource to display as
the icon (uses .bmp .mask.bmp resources</param>
/// <returns>Created button control</returns>
protected CommandBarButton AddContextMenuButton(CommandBar commandBar,
string caption, string tag)
{
return AddButton(commandBar, caption, MsoButtonStyle.msoButtonCaption,
tag);
}

protected CommandBarButton AddButton(CommandBar commandBar, string
caption, MsoButtonStyle style, string tag)
{

CommandBarButton button =(CommandBarButton)
commandBar.Controls.Add(MsoControlType.msoControlButton, 1, null,
commandBar.Controls.Count + 1, false);

button.Tag = tag;
if(caption != null)
{
button.Caption = caption;
button.TooltipText = caption;
}
button.Style = style;

return button;
}

#endregion
}
}
****************************************************************
 
P

Patrick Schmid

Hi Olivier,

Can you send me the VS project for your test add-in via email? I'd like
to submit this to Microsoft so that they can take a look at it. You can
get my email from my website.

Thanks,

Patrick Schmid
--------------
http://pschmid.net

I will have to wait before having a test machine with Windows XP and Office
2007.

Meanwhile, I created a sample project with an "hello world" Add-in.

I installed my plugin on Vista / Office 2007, as well as my real plugin.

In Word, everything works fine : I've got my add-in and the other tool bar in
the Add-In ribbon, and both context menus are present.

In Excel, both add-ins are present in the add_ins ribbon and work fine. The
context menus text is displayed 5 times in the menu commands panel in the add-
ins ribbon (it's a bug we already have discovered with Excel 2007), but work
normaly on a right clic.

In Outlook, both add-ins are present in the main windows and work fine.

In powerpoint, both add-ins are present in the add_ins ribbon and work fine.
No
context menu appears.

So there seems to be a bug in powerpoint 2007 ...

Here is the code of my Connect.cs class :

***************************************************************
namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections;


#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("D9305294-26DD-44B8-AD2A-2A822B98DCDA"),
ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

private object applicationObject;
private object addInInstance;
private CommandBarButton MyButton;
private CommandBars oCommandBars;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
CommandBar oStandardBar;

try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "My Custom Button";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
MyButton.OnAction = "!<MyCOMAddin.Connect>";

MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

CreateOrAttachContextMenuItems();

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");

oStandardBar = null;
}

/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
object omissing = System.Reflection.Missing.Value ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
DeleteContextMenuItems();
MyButton = null;
oCommandBars = null;
}

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }

#region Manage context menus
virtual protected void CreateOrAttachContextMenuItems()
{

// TODO: handle correctly partial attachment scenarios
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControls controls =
oCommandBars.FindControls(MsoControlType.msoControlButton,
Type.Missing,
tag,Type.Missing);
if(controls == null)
{
System.Diagnostics.Debug.WriteLine("Create context menu for " + tag);
CreateContextMenuItems();
}
}
}


protected void CreateContextMenuItems()
{
foreach(CommandBar commandBar in oCommandBars)
{
if(commandBar.Type == MsoBarType.msoBarTypePopup &&
commandBar.Name!="System")
{
System.Diagnostics.Debug.WriteLine("Create context menu in command bar
: " + commandBar.Name);
try
{
CreateContextMenuItems(commandBar);
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}

}

protected void CreateContextMenuItems(CommandBar commandBar)
{
AddContextMenuButton(commandBar, "Button 1 " +
commandBar.Name.ToString(), "MyAddin1.Context.Translate").BeginGroup = true;
AddContextMenuButton(commandBar, "Button 2",
"MyAddin1.Context.TranslateAndReplace");
AddContextMenuButton(commandBar, "Button 3", "MyAddin1.Context.Lookup");
}

protected Hashtable ContextMenuItems
{
get
{
Hashtable fctval = new Hashtable();
fctval["MyAddin1.Context.Translate"] = "First context menu item";
fctval["MyAddin1.Context.TranslateAndReplace"] ="Second context menu
item";
fctval["MyAddin1.Context.Lookup"] = "Third context menu item";
return fctval;
}
}

protected void DeleteContextMenuItems()
{
for(int j = 1; j <= oCommandBars.Count; j++)
{
CommandBar commandBar = oCommandBars[j];
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControl controls =
commandBar.FindControl(MsoControlType.msoControlButton,
Type.Missing,
tag,true,false);
if(controls != null)
{
controls.Delete(false);
System.Diagnostics.Debug.WriteLine("bar = " + commandBar.Name + " tag
removed = " + tag);
}
}
}
}

/// <summary>
/// Adds a button to a context menu
/// </summary>
/// <param name="caption">String to display as the caption and tool
tip</param>
/// <param name="image">String indicating the image resource to display as
the icon (uses .bmp .mask.bmp resources</param>
/// <returns>Created button control</returns>
protected CommandBarButton AddContextMenuButton(CommandBar commandBar,
string caption, string tag)
{
return AddButton(commandBar, caption, MsoButtonStyle.msoButtonCaption,
tag);
}

protected CommandBarButton AddButton(CommandBar commandBar, string
caption, MsoButtonStyle style, string tag)
{

CommandBarButton button =(CommandBarButton)
commandBar.Controls.Add(MsoControlType.msoControlButton, 1, null,
commandBar.Controls.Count + 1, false);

button.Tag = tag;
if(caption != null)
{
button.Caption = caption;
button.TooltipText = caption;
}
button.Style = style;

return button;
}

#endregion
}
}
****************************************************************
 
O

Olivier B.

Hi Patrick,

I just sent you an e-mail with the project attached.

Thanks for your help.

Olivier

Patrick Schmid said:
Hi Olivier,

Can you send me the VS project for your test add-in via email? I'd like
to submit this to Microsoft so that they can take a look at it. You can
get my email from my website.

Thanks,

Patrick Schmid
--------------
http://pschmid.net

I will have to wait before having a test machine with Windows XP and Office
2007.

Meanwhile, I created a sample project with an "hello world" Add-in.

I installed my plugin on Vista / Office 2007, as well as my real plugin.

In Word, everything works fine : I've got my add-in and the other tool bar in
the Add-In ribbon, and both context menus are present.

In Excel, both add-ins are present in the add_ins ribbon and work fine. The
context menus text is displayed 5 times in the menu commands panel in the add-
ins ribbon (it's a bug we already have discovered with Excel 2007), but work
normaly on a right clic.

In Outlook, both add-ins are present in the main windows and work fine.

In powerpoint, both add-ins are present in the add_ins ribbon and work fine.
No
context menu appears.

So there seems to be a bug in powerpoint 2007 ...

Here is the code of my Connect.cs class :

***************************************************************
namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections;


#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("D9305294-26DD-44B8-AD2A-2A822B98DCDA"),
ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

private object applicationObject;
private object addInInstance;
private CommandBarButton MyButton;
private CommandBars oCommandBars;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
CommandBar oStandardBar;

try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "My Custom Button";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
MyButton.OnAction = "!<MyCOMAddin.Connect>";

MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

CreateOrAttachContextMenuItems();

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");

oStandardBar = null;
}

/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
object omissing = System.Reflection.Missing.Value ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
DeleteContextMenuItems();
MyButton = null;
oCommandBars = null;
}

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }

#region Manage context menus
virtual protected void CreateOrAttachContextMenuItems()
{

// TODO: handle correctly partial attachment scenarios
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControls controls =
oCommandBars.FindControls(MsoControlType.msoControlButton,
Type.Missing,
tag,Type.Missing);
if(controls == null)
{
System.Diagnostics.Debug.WriteLine("Create context menu for " + tag);
CreateContextMenuItems();
}
}
}


protected void CreateContextMenuItems()
{
foreach(CommandBar commandBar in oCommandBars)
{
if(commandBar.Type == MsoBarType.msoBarTypePopup &&
commandBar.Name!="System")
{
System.Diagnostics.Debug.WriteLine("Create context menu in command bar
: " + commandBar.Name);
try
{
CreateContextMenuItems(commandBar);
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}

}
 
O

Olivier B.

I have just made the test, with my test Add-in on Windows XP / Office 2007
and I have the same problem.

So this is not a Vista related issue, but a powerpoint one.

Olivier
 
O

Olivier B.

I just remarked that I never said that I'm using .NET 1.1 ...

Olivier

Patrick Schmid said:
Hi Olivier,

Can you send me the VS project for your test add-in via email? I'd like
to submit this to Microsoft so that they can take a look at it. You can
get my email from my website.

Thanks,

Patrick Schmid
--------------
http://pschmid.net

I will have to wait before having a test machine with Windows XP and Office
2007.

Meanwhile, I created a sample project with an "hello world" Add-in.

I installed my plugin on Vista / Office 2007, as well as my real plugin.

In Word, everything works fine : I've got my add-in and the other tool bar in
the Add-In ribbon, and both context menus are present.

In Excel, both add-ins are present in the add_ins ribbon and work fine. The
context menus text is displayed 5 times in the menu commands panel in the add-
ins ribbon (it's a bug we already have discovered with Excel 2007), but work
normaly on a right clic.

In Outlook, both add-ins are present in the main windows and work fine.

In powerpoint, both add-ins are present in the add_ins ribbon and work fine.
No
context menu appears.

So there seems to be a bug in powerpoint 2007 ...

Here is the code of my Connect.cs class :

***************************************************************
namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections;


#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("D9305294-26DD-44B8-AD2A-2A822B98DCDA"),
ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

private object applicationObject;
private object addInInstance;
private CommandBarButton MyButton;
private CommandBars oCommandBars;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
CommandBar oStandardBar;

try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "My Custom Button";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
MyButton.OnAction = "!<MyCOMAddin.Connect>";

MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

CreateOrAttachContextMenuItems();

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");

oStandardBar = null;
}

/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
object omissing = System.Reflection.Missing.Value ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
DeleteContextMenuItems();
MyButton = null;
oCommandBars = null;
}

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }

#region Manage context menus
virtual protected void CreateOrAttachContextMenuItems()
{

// TODO: handle correctly partial attachment scenarios
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControls controls =
oCommandBars.FindControls(MsoControlType.msoControlButton,
Type.Missing,
tag,Type.Missing);
if(controls == null)
{
System.Diagnostics.Debug.WriteLine("Create context menu for " + tag);
CreateContextMenuItems();
}
}
}


protected void CreateContextMenuItems()
{
foreach(CommandBar commandBar in oCommandBars)
{
if(commandBar.Type == MsoBarType.msoBarTypePopup &&
commandBar.Name!="System")
{
System.Diagnostics.Debug.WriteLine("Create context menu in command bar
: " + commandBar.Name);
try
{
CreateContextMenuItems(commandBar);
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}

}
 
P

Patrick Schmid

Luckily for me you do! I only have VS 2003 myself. Due to that, I could
actually run your project here without a problem.

Patrick Schmid
--------------
http://pschmid.net

I just remarked that I never said that I'm using .NET 1.1 ...

Olivier

Patrick Schmid said:
Hi Olivier,

Can you send me the VS project for your test add-in via email? I'd like
to submit this to Microsoft so that they can take a look at it. You can
get my email from my website.

Thanks,

Patrick Schmid
--------------
http://pschmid.net

I will have to wait before having a test machine with Windows XP and Office
2007.

Meanwhile, I created a sample project with an "hello world" Add-in.

I installed my plugin on Vista / Office 2007, as well as my real plugin.

In Word, everything works fine : I've got my add-in and the other tool bar in
the Add-In ribbon, and both context menus are present.

In Excel, both add-ins are present in the add_ins ribbon and work fine. The
context menus text is displayed 5 times in the menu commands panel in the add-
ins ribbon (it's a bug we already have discovered with Excel 2007), but work
normaly on a right clic.

In Outlook, both add-ins are present in the main windows and work fine.

In powerpoint, both add-ins are present in the add_ins ribbon and work fine.
No
context menu appears.

So there seems to be a bug in powerpoint 2007 ...

Here is the code of my Connect.cs class :

***************************************************************
namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections;


#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("D9305294-26DD-44B8-AD2A-2A822B98DCDA"),
ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

private object applicationObject;
private object addInInstance;
private CommandBarButton MyButton;
private CommandBars oCommandBars;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
CommandBar oStandardBar;

try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "My Custom Button";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
MyButton.OnAction = "!<MyCOMAddin.Connect>";

MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

CreateOrAttachContextMenuItems();

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");

oStandardBar = null;
}

/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
object omissing = System.Reflection.Missing.Value ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
DeleteContextMenuItems();
MyButton = null;
oCommandBars = null;
}

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }

#region Manage context menus
virtual protected void CreateOrAttachContextMenuItems()
{

// TODO: handle correctly partial attachment scenarios
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControls controls =
oCommandBars.FindControls(MsoControlType.msoControlButton,
Type.Missing,
tag,Type.Missing);
if(controls == null)
{
System.Diagnostics.Debug.WriteLine("Create context menu for " + tag);
CreateContextMenuItems();
}
}
}


protected void CreateContextMenuItems()
{
foreach(CommandBar commandBar in oCommandBars)
{
if(commandBar.Type == MsoBarType.msoBarTypePopup &&
commandBar.Name!="System")
{
System.Diagnostics.Debug.WriteLine("Create context menu in command bar
: " + commandBar.Name);
try
{
CreateContextMenuItems(commandBar);
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}

}
 
P

Patrick Schmid

It seems that there is a bug in PPT 2007. Items are only added to the
right-click menus of frames (white space), but to none of the
right-click menus of any object. I've submitted this as bug to
Microsoft.

Patrick Schmid
--------------
http://pschmid.net

Luckily for me you do! I only have VS 2003 myself. Due to that, I could
actually run your project here without a problem.

Patrick Schmid
--------------
http://pschmid.net

I just remarked that I never said that I'm using .NET 1.1 ...

Olivier

Patrick Schmid said:
Hi Olivier,

Can you send me the VS project for your test add-in via email? I'd like
to submit this to Microsoft so that they can take a look at it. You can
get my email from my website.

Thanks,

Patrick Schmid
--------------
http://pschmid.net


I will have to wait before having a test machine with Windows XP and Office
2007.

Meanwhile, I created a sample project with an "hello world" Add-in.

I installed my plugin on Vista / Office 2007, as well as my real plugin.

In Word, everything works fine : I've got my add-in and the other tool bar in
the Add-In ribbon, and both context menus are present.

In Excel, both add-ins are present in the add_ins ribbon and work fine. The
context menus text is displayed 5 times in the menu commands panel in the add-
ins ribbon (it's a bug we already have discovered with Excel 2007), but work
normaly on a right clic.

In Outlook, both add-ins are present in the main windows and work fine.

In powerpoint, both add-ins are present in the add_ins ribbon and work fine.
No
context menu appears.

So there seems to be a bug in powerpoint 2007 ...

Here is the code of my Connect.cs class :

***************************************************************
namespace MyAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections;


#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("D9305294-26DD-44B8-AD2A-2A822B98DCDA"),
ProgId("MyAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

private object applicationObject;
private object addInInstance;
private CommandBarButton MyButton;
private CommandBars oCommandBars;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
CommandBar oStandardBar;

try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "My Custom Button";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.
MyButton.OnAction = "!<MyCOMAddin.Connect>";

MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

CreateOrAttachContextMenuItems();

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");

oStandardBar = null;
}

/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
object omissing = System.Reflection.Missing.Value ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
DeleteContextMenuItems();
MyButton = null;
oCommandBars = null;
}

private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }

#region Manage context menus
virtual protected void CreateOrAttachContextMenuItems()
{

// TODO: handle correctly partial attachment scenarios
foreach(string tag in ContextMenuItems.Keys)
{
CommandBarControls controls =
oCommandBars.FindControls(MsoControlType.msoControlButton,
Type.Missing,
tag,Type.Missing);
if(controls == null)
{
System.Diagnostics.Debug.WriteLine("Create context menu for " + tag);
CreateContextMenuItems();
}
}
}


protected void CreateContextMenuItems()
{
foreach(CommandBar commandBar in oCommandBars)
{
if(commandBar.Type == MsoBarType.msoBarTypePopup &&
commandBar.Name!="System")
{
System.Diagnostics.Debug.WriteLine("Create context menu in command bar
: " + commandBar.Name);
try
{
CreateContextMenuItems(commandBar);
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top