Microsoft Office XP, Microsoft Office 2003, ?? Microsoft Office 2007 ????????? ??-???? ??????? ???? ?? ??? ?? Office ??????????? ?? ????????? ???? ?? ??? ????? ?? ??? ??? ?????????? ??????? ??????????? ?? ??????? ?? ??-???? Microsoft ??? ???????? ???? (COM) ??? ???? ??-????? ?? ??? ?? ??? ???? Office COM ??-???? ?? ???? ??? ????? ???? ?? ?? ????? ?? ?? ???? Office COM ??-?? Microsoft Visual C# .NET ?? ????? ?? ????? ?? ??? ???? ?????
IDTExensibility2 ???????
???? COM ??-?? ?? ?? ???-????????? COM ?????, ?? ActiveX ???????? ???? ????????? (DLL), ?? ???? ???? ??
IDTExensibility2??????? ?? ??? ??? ??-?? Microsoft ??????? ?????? ????????? (Msaddndr.dll) ??? ????? ???? ??? ??? All COM add-ins inherit from this interface and must
implement each of its five methods.
OnConnection
The
OnConnectionevent fires whenever the COM add-in is
connected. The add-in may be connected on startup, by the end user, or through
Automation. ???
OnConnectionevent returns successfully, the add-in is said to be
loaded. If an error message is returned, the host application immediately
releases its reference to the add-in, and the object is destroyed.
The
OnConnectionevent takes the following four parameters:
- ?????????: A reference to the host application object.
- ConnectMode: A constant that specifies how the add-in is connected. The
add-in can be connected in the following ways:
- ext_cm_AfterStartup: ??-?? ?? ??? ????? ?????? ??????? ???? ??COM ??-????????? ????? ?? ??????? ?? ??? ?? ..
- ext_cm_CommandLine: ??-?? ???????? ?? ???? ?????? ??? ????? ??? ?? ?? ?? ???? ???? COM ??-???? Office ??????????? ?? ??? ??? ??? ???
- ext_cm_External: ??-?? ???????? ?? ??????? ?? ?????? ?? ???? ????? ????????? ??????? ??? ???? ?? ?? ???? ???? ???? COM ??-???? ?? Office ?? ??? ????? ?? ??? ??????????
- ext_cm_Startup: ??-?? ?? ??????? ????????? ????????? ?? ????? ??????? ?? ??????? ?? ????????? ??? ??? ?????? ?????? ????????? ???
- AddInInst: ???? ?????? ?? ???COMAddIn???????? ?? ??-?? ??? ?? ??????? ??COMAddIns????? ????????? ?? ??? ?????? ?????
- ?????: ???? ????Varianttype values that can hold user-defined data.
OnDisconnection
The
OnDisconnectionevent fires when the COM add-in is
disconnected and just before it unloads from memory. The add-in should perform
any cleanup of resources in this event, and restore any changes made to the
host application.
The
OnDisconnectionevent takes the following two
parameters:
- RemoveMode: A constant that specifies how the add-in was disconnected. The
add-in can be disconnected in the following ways:
- ext_dm_HostShutdown: The add-in is disconnected when the host application
closes.
- ext_dm_UserClosed: The add-in is disconnected by the end user or an Automation
controller.
- ?????: An array ofVarianttype values that can hold user-defined data.
OnAddInsUpdate
The
OnAddInsUpdateevent fires when the set of registered COM
add-ins changes. In other words, whenever a COM add-in is installed or removed
from the host application, this event fires.
OnStartupComplete ?? OnBeginShutdown
?????
OnStartupComplete???? ??
OnBeginShutdownmethod are called
when the host application has left or is entering a state in which user
interaction should be avoided because the application is busy loading or
unloading itself from memory. The
OnStartupCompletemethod is called only if the add-in
was connected during startup, and the
OnBeginShutdownmethod is called only if the host
disconnects the add-in during shutdown.
?? ?? ??????? ???? ????? ????????? ?? ??? ?????????? ???????? ???? ??? ?????? ??, ??????? ?? ?????? ???? ???????? ?? ?? ?? ??? ???????? ???? ?? ??? ??????? ????? ?? ???? ??
OnConnection????? ??
OnDisconnection??????
COM ??-?? ???????
??????? COM ???????, ?? ????? ??? COM ??-?? ?? ????? ??? ?? ?? ???? ???????? Office ????????? ?? ??? ??????? ???? ?? ???? ??????? ???? ?? ??? ????? ?? ????? ????????? ?? ???, ??-?? ????? ????? ????? ?? ???, ????? ????? ?? ??????? ??? ?? ??? ??? ???? ProgID ?? ????? ???? ???, ???????:
HKEY_CURRENT_USER\Software\Microsoft\Office\ <officeapp>\Addins\ <progid></progid></officeapp>
??-?? ?????? ?? ???? ??? ???????? ???????? ??? ?? ????? ????? ???? ?? ??? ????? ?? ????? ?? ???? ???? ?????, ??-?? ????? ????????? ?????? ??? ??????? ???? LoadBehavior ??? ?? ?? DWORD ??? ?? ????? ????? ?? ??? ?? ???? ??-?? ????? ????????? ?????? ??? ??, ?? ????? ????? ?? ?? ?????? ?? ??? ????????? ???? ??:
- 0 ?????????? = - ??? ???? ???
- 1 = Connected - ??? ?? ?? ???
- 2 Bootload - ????????? ????????? ?? ??? ???? =?
- 8 = DemandLoad - ?????????? ?????? ???????? ???? ?? ???? ??? ?????
- 16 - ConnectFirstTime (???? ?????????) ?? ???? ?? ??? ??? =?
??????? ??? ????????? ?? 0x03 (Connected | Bootload)?
??-???? ?? ??????????? ???? ????
IDTExtensibility2??? DWORD ??? ?? ?? ????????? ???? ?????
CommandLineSafe
?? ????? ???? ??? ?? ???? ??-?? ????????? ???? ?????????? ??????? ?? ?????? ???? ???? ?? ??? ???????? ??? ???? ?? ???? ???? ??? ?? 0x00 false ????? ???? ??, ?? 0x01 ?? ??? ??? ??? ????? ???? ???
????? C# .NET ?? ????? ?? ???? COM ??-?? ????? ?? ??? ???? ????
???? ????? ???, ?? ??? ??? ??? Office COM ??-?? ?? ?? ???-????????? COM ????? COM ??-???? ??? ?? ?????? ?? ???? Office ????????? ?????? ?????? ??? ?????, ???? COM ??-?? ??? .NET ?? ?????? ?? ??-?? ??? .NET ??? ???? ???? ??? ?? COM ??????? ?? ????? ???? ?? ???? (?? ????, Office ?????????) COM ?????? ??? ?? ?????? ???
????? ?? ??? ??? COM ??-?? ????? C# .NET ???, ????? ????? ?? ???? ????:
- ?? ????? ????????? ?? ????? ??? ????? C# .NET, ??????????
- ???? ???? ?? ?? ?????? ????????? ?? ??? ??? ?????? ??????IDTExtensibility2. ???????? ?????? ??????? ?? ??? ?? ???? ?? ?? ??? ?? ??????? ?????? ??Extensibility.
- Microsoft Office ???????? ????????? ?? ??? ??? ?????? ??????? ???????? ?????? ??????? ?? ??? ?? ???? ?? ?? ??? ?? ??????? ?????? ??Office.
- ???? ???? ?? ?? ????? ????????? ??? ??? ????????? ???? ?????IDTExtensibility2.
- ???? ?? ??? ????????? ????? ??, COM interop ?? ??? ????????? ??????? ????? ??? ???? ?? ???, ????? ?? ????? ???? ?? ?? ????????? ?? ??? ??????? ?? ??? ?? ???? ??? ??? COM interop ?? ??? ??????? ????? COM interop ?? ??? ??? .NET ??? ?? ??????? ???? ?? ??? ?? Regasm.exe ?? ????? ?? ???? ????
- ????????? ???????????? ????? ???? Office ??????????? ?? ????? ?? ??-?? ??? ?? ???? ????
?? ??? ?? ????? ?? ???? ???? ?? ??? ??? ???? ???, ?? ?? ?????? ?? ??? .NET ????????? ??? ???? ???
???? Addin. ?? ??????? Extensibility ???????, ?? .NET ??? ??? COM ??-?? ????? ??? ??? ???? ???
Extensibility ???????? ????? ?? ?? ????? C# ?? ??? .NET ????? ????????? ????????? ????
??????.???? ?? ???? ???? ??
IDTExtensibility2???????? ??? Skeleton ??? ?? ?? ???? ??????? ?? ???? ???? ??
IDTExtensibility??? ????? ?? ??? ?? ????????? ??? Extensibility ?? Office assemblies ?? ?????? ???? ????????? ?? ????? ???????? ??
COM Interop ?? ??? ????????????. The assembly key (.snk) file is generated and is
referenced in the
AssemblyKeyfileattribute in Assemblyinfo.vb.
????? ????????? ????????? ?? ??? ???????? ????? ????? ????????? ?? COM ??-?? ???? ?????????? ?? ?????? ???? ?? ??? ????? ?? ???? ???? ??? ?????, ?? ?? ?? ????????? ????? ???? ????
??? ?? ??? ??????
- ????? ????????Microsoft Visual Studio .NET, ??? ???? ????? ????????? ????-????? ????, ?? ???? ????????????.
- ???????? ?????????????? ????? ???, ??????? ???????? ??????????? ???????????????? ????????? ????Extensibility ?????????, ?? ???? ??? ?? ??? ???????? ??? ?? ??-??????????:.
- ??????:MyCOMAddin??? ?? ??-??? ?? ???? ??? ?? ??? ???OK.
- ?? Extensibility ??????? ????? ???? ??, ?? ????? ????? ?? ???? ????:
- 1, ?? ??? ???? ????? ????? ??-?? ?? ????? ?? Visual C# ??????? ????-????? ????, ?? ???? ???next.
- ????? 2, ?? ????? ????? ????????? ?? ??? ????, ?? ???? ??? ????? ????next:
- Microsoft Word
- Microsoft PowerPoint
- Microsoft Outlook
- Microsoft Excel
- Microsoft Access
- ????? 3, ?? ???? ??-?? ?? ??? ????? ?? ??? ?????? ????, ?? ???? ???next.
???:??? ?? ??-?? ?? ????? ??? ????? ???? ???COM ??-??????? ????? Office ????????? ??? ???
- ????? ?? 4, ??? ?????? ?????? ?? ??? ????, ?? ???? ??? ????? ????next.
- ????? ????,??????.
- ????? ????????????????? ??,?????? ??????. ????? ????,System.Windows.Forms.DLL??? ???? ??? ????? ??????? ?????? ????-????? ????, ?? ???? ???OK.
- ????? ??? ???????? ?? ???? ??? ????????????.????:
- ???? ?? ??? ????? ????? ???????????.????:
private CommandBarButton MyButton;
- ??? ?? ??????? ?? ??? ??????????? ????IDTExtensibility2???????????.????, follows ?? ??? ???:
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);
}
}
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom) {
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}
public void OnAddInsUpdate(ref System.Array custom)
{
}
public void OnStartupComplete(ref System.Array custom)
{
CommandBars oCommandBars;
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);
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;
oCommandBars = null;
}
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);
MyButton = null;
}
private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel) {
System.Windows.Forms.MessageBox.Show("MyButton was Clicked","MyCOMAddin"); }
- ????? ?? COM ??-?? ?? ??????? ????? ??? ???? ?? ???, ????? ????? ?? ???? ????::
- ????? ????????????? ??,?????? ?????. ????? ??? ?? COM ??-?? ????? registers .NET ???? COM interop ?? ???
- ?? ?? ???? ?????? ????? ????? ????????? ?? ??? ??? ???? ??-?? (?????? ?? ???, Microsoft Word ?? Microsoft Excel) ?? ??? Office ????????? ??????? ?????
- ??-?? ??????? ?? ??? ??, ?? ???OnStartupComplete??-?? ?? ???? fired ??, ?? ???? ?? ????? ??????? ???? ??? ????? ????? ?? ?????? ????? Note that the add-in added a new custom button with the
caption "My Custom Button" to the standard toolbar.
- ????? ????,My Custom Button. The????? ????,event for the button is handled by the add-in and you receive a
message box. Dismiss the message box.
- Quit the Office application.
- When you quit the application, theOnBeginShutDownevent fires, and you receive a message. Dismiss the message box to end the
demonstration.
For additional information about writing COM add-ins, click the following article number to view the article in the Microsoft Knowledge Base:
190253
(http://support.microsoft.com/kb/190253/
)
INFO: VB6 Designers Do Not Work in
VB5