????? C# .NET ?? ????? ?? ???? Office COM ??-?? ????? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 302901 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

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

TheOnConnectionevent 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.

TheOnConnectionevent 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

TheOnDisconnectionevent 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.

TheOnDisconnectionevent 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

TheOnAddInsUpdateevent 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. TheOnStartupCompletemethod is called only if the add-in was connected during startup, and theOnBeginShutdownmethod 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 ???, ????? ????? ?? ???? ????:
  1. ?? ????? ????????? ?? ????? ??? ????? C# .NET, ??????????
  2. ???? ???? ?? ?? ?????? ????????? ?? ??? ??? ?????? ??????IDTExtensibility2. ???????? ?????? ??????? ?? ??? ?? ???? ?? ?? ??? ?? ??????? ?????? ??Extensibility.
  3. Microsoft Office ???????? ????????? ?? ??? ??? ?????? ??????? ???????? ?????? ??????? ?? ??? ?? ???? ?? ?? ??? ?? ??????? ?????? ??Office.
  4. ???? ???? ?? ?? ????? ????????? ??? ??? ????????? ???? ?????IDTExtensibility2.
  5. ???? ?? ??? ????????? ????? ??, COM interop ?? ??? ????????? ??????? ????? ??? ???? ?? ???, ????? ?? ????? ???? ?? ?? ????????? ?? ??? ??????? ?? ??? ?? ???? ??? ??? COM interop ?? ??? ??????? ????? COM interop ?? ??? ??? .NET ??? ?? ??????? ???? ?? ??? ?? Regasm.exe ?? ????? ?? ???? ????
  6. ????????? ???????????? ????? ???? 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 theAssemblyKeyfileattribute in Assemblyinfo.vb.

????? ????????? ????????? ?? ??? ???????? ????? ????? ????????? ?? COM ??-?? ???? ?????????? ?? ?????? ???? ?? ??? ????? ?? ???? ???? ??? ?????, ?? ?? ?? ????????? ????? ???? ????

??? ?? ??? ??????

  1. ????? ????????Microsoft Visual Studio .NET, ??? ???? ????? ????????? ????-????? ????, ?? ???? ????????????.
  2. ???????? ?????????????? ????? ???, ??????? ???????? ??????????? ???????????????? ????????? ????Extensibility ?????????, ?? ???? ??? ?? ??? ???????? ??? ?? ??-??????????:.
  3. ??????:MyCOMAddin??? ?? ??-??? ?? ???? ??? ?? ??? ???OK.
  4. ?? Extensibility ??????? ????? ???? ??, ?? ????? ????? ?? ???? ????:
    1. 1, ?? ??? ???? ????? ????? ??-?? ?? ????? ?? Visual C# ??????? ????-????? ????, ?? ???? ???next.
    2. ????? 2, ?? ????? ????? ????????? ?? ??? ????, ?? ???? ??? ????? ????next:
      • Microsoft Word
      • Microsoft PowerPoint
      • Microsoft Outlook
      • Microsoft Excel
      • Microsoft Access
    3. ????? 3, ?? ???? ??-?? ?? ??? ????? ?? ??? ?????? ????, ?? ???? ???next.

      ???:??? ?? ??-?? ?? ????? ??? ????? ???? ???COM ??-??????? ????? Office ????????? ??? ???

    4. ????? ?? 4, ??? ?????? ?????? ?? ??? ????, ?? ???? ??? ????? ????next.
    5. ????? ????,??????.
  5. ????? ????????????????? ??,?????? ??????. ????? ????,System.Windows.Forms.DLL??? ???? ??? ????? ??????? ?????? ????-????? ????, ?? ???? ???OK.
  6. ????? ??? ???????? ?? ???? ??? ????????????.????:
    using System.Reflection;
  7. ???? ?? ??? ????? ????? ???????????.????:
    private CommandBarButton MyButton; 
  8. ??? ?? ??????? ?? ??? ??????????? ????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"); }
    					
  9. ????? ?? COM ??-?? ?? ??????? ????? ??? ???? ?? ???, ????? ????? ?? ???? ????::
    1. ????? ????????????? ??,?????? ?????. ????? ??? ?? COM ??-?? ????? registers .NET ???? COM interop ?? ???
    2. ?? ?? ???? ?????? ????? ????? ????????? ?? ??? ??? ???? ??-?? (?????? ?? ???, Microsoft Word ?? Microsoft Excel) ?? ??? Office ????????? ??????? ?????
    3. ??-?? ??????? ?? ??? ??, ?? ???OnStartupComplete??-?? ?? ???? fired ??, ?? ???? ?? ????? ??????? ???? ??? ????? ????? ?? ?????? ????? Note that the add-in added a new custom button with the caption "My Custom Button" to the standard toolbar.
    4. ????? ????,My Custom Button. The????? ????,event for the button is handled by the add-in and you receive a message box. Dismiss the message box.
    5. Quit the Office application.
    6. 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:
190253INFO: VB6 Designers Do Not Work in VB5

???

???? ID: 302901 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Excel 2007
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Office Outlook 2007
  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Office PowerPoint 2007
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Office Word 2007
  • Microsoft Word 2002
??????: 
kbautomation kbhowtomaster kbmt KB302901 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:302901

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com