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());
}
}
}
}