????? ????? ????? ?????? COM Office ???????? Visual C# .NET

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

?? ??? ??????

??????

????? Microsoft Office XP ? Microsoft Office 2003 Microsoft Office 2007 ????? ????? ????? ????? ??????? ??????? ???????? ?????? ? ?????? ?? ??????? Office. ???? ??? ??????? ???????? ????? ???? ?????? Microsoft "(COM) ???????? ????????. ?????? ??? ??????? ??????? ???????? ?? COM Office ??? ??????? ???? ????? ??? ???? ????? ????? ????? ?????? COM Office ???????? Microsoft Visual C# .NET.

????? IDTExensibility2

????? COM ???????? ????? ?? ???? COM ??? ??????? ?? ????? ???????? ?????? ActiveX (DLL) ???? ???? ?????? ??????? IDTExensibility2 ??? ?? ???? ?? ????? ????? ???? Microsoft ??????? ???????? (Msaddndr.dll). ???? ????? COM ???????? ??? ?? ??? ??????? ? ??? ?? ???? ?????? ?? ?? ?????? ??? ????? ??.

OnConnection

??? ????? OnConnection ???? ??? ?????? ??????? ???????? COM. ??????? ???????? ???? ?????? ?? ??? ??? ??????? ?????? ???????? ??????? ?? ?? ???? ???????. ??? ????? ????? OnConnection ????? ??????? ???????? ?? ???? ???? ??????. ??? ?? ????? ????? ??? ??????? ?????? ?????? ????????? ?????? ?? ???? ??????? ???????? ?? ????? ??????.

???? ??????? OnConnection ???????? ?????? ???????:
  • ???????: ???? ??? ???? ??????? ??????.
  • ConnectMode: ?? ???? ???? ????? ??????? ???????? ?????. ???? ?? ???? ????? ??????? ???????? ?????? ???????:
    • ext_cm_AfterStartup: ??????? ???????? ??? ?????? ?????? ???????? ??????? ?? ???? ?????? ????? COM ????????.
    • ext_cm_CommandLine: ??????? ???????? ???? ?? ??? ???????. ???? ??? ?? ??? ????? ??? ??? ????? ??????? ???????? ?? COM ???????? Office.
    • ext_cm_External: ??????? ???????? ????? ?????? ????? ????? ???? ???????. ???? ??? ?? ??? ????? ??? ??? ????? ????? COM ???????? ?? Office ?????????.
    • ext_cm_Startup: ??????? ???????? ??? ?????? ?????? ?????? ??? ??? ????? ???????. ????? ??? ?????? ??? ????? ?? ???????.
  • AddInInst: ???? ??? ???? COMAddIn ???? ???? ??? ??? ??????? ???????? ??? ?????? ????? COM ???????? ??????? ??????.
  • ??????: ???? ??? ??? ????? ???? ???????? ??????? ?? ??? ????????.

OnDisconnection

??? ????? OnDisconnection ????? ??? ??? ??????? ???????? COM ? ??? ????? ????? ?? ???????. ??????? ???????? ??? ????? ????? ????? ?? ??????? ?? ??? ????? ?? ??????? ??? ??????? ??? ??????? ??? ??????? ??????.

???? ??????? OnDisconnection ??????? ???????:
  • RemoveMode: ?? ???? ???? ????? ??????? ???????? ?? ??? ?????. ??????? ???????? ???? ??? ??????? ?????? ???????:
    • ext_dm_HostShutdown: ??????? ???????? ?? ??? ??????? ??? ????? ??????? ??????.
    • ext_dm_UserClosed: ??????? ???????? ?? ??? ??????? ?? ??? ???????? ??????? ?? ??????? ??? ???? ????.
  • ??????: ???? ??? ??? ????? ???? ???????? ??????? ?? ??? ????????.

OnAddInsUpdate

??? ????? OnAddInsUpdate ??? ???????? ?? ????? COM ????????? ??????? ????????. ????? ???? ???? ????? ???????? COM ??? ????? ?? ????? ?? ??????? ?????? ??? ??? ?????.

OnStartupComplete ? OnBeginShutdown

?? ????? OnStartupComplete ?????? OnBeginShutdown ???? ??? ??? ??????? ?????? ?? ??? ????? ???? ?? ?????? ??? ???? ????? ??? ??????? ????? ????? ?? ????? ????? ???? ?? ???????. ??? ??????? ????? OnStartupComplete ??? ??? ???? ??????? ???????? ?? ????? ????? ??? ??????? ?? ??????? ??????? OnBeginShutdown ??? ?? ???? ??? ????? ?????? ??????? ???????? ????? ????? ???????.

??? ????? ???????? ??????? ?????? ??? ???? ???? ??? ????? ??? ???????, ??? ???? ??????? ??????? ?????? ??????? ????? ???? ????? ?????? ?? ????? OnConnection ?????? OnDisconnection.

COM ???????? ???????

???????? ??? ????? COM ?????? ????? ???????? COM ????? ????? ???? ?? ?? ????? Office ???? ??? ?????. ????? ???? ?? ????? ???? ??????? ???????? ???? ????? ??????? ???? ???????? ProgID ????? ?? ??? ??? ????? ??? ?????? ??????:
HKEY_CURRENT_USER\Software\Microsoft\Office\<OfficeApp>\Addins\<ProgID>
??????? ???????? ???? ????? ????? ?? ??? ?????? ??????? ?? ??? ??? ????? ???? ????. ???????? ??? ???? ??????? ???????? ??? ????? ???? ??????? ??????? ???????? ???? DWORD ???? LoadBehavior. ???? ??? ?????? ??????? ???????? ??? ?????? ?? ??? ??????? ?????? ? ?????? ????? ?? ?????? ?? ????? ???????:
  • 0 = ??? ??????? - ??? ??????.
  • 1 = ???? - ?? ??????.
  • 2 = bootload - ??????? ??? ??? ???????.
  • 8 = demandLoad - ????? ??? ??? ???? ?? ??? ????????.
  • 16 = ConnectFirstTime - ??????? ??? ????? ??? (??? ??? ??????? ??????).
?????? ????????? ??????? ?? 0x03 (???? | Bootload).

??? ????? ????? ??????? ???????? ???? ???? ?????? IDTExtensibility2 ???? DWORD ????
CommandLineSafe
?????? ?? ??? ???? ??????? ???????? ????? ???????? ???? ?? ????? ????? ??????. ???? ???? 0x00 ??? False ????? ???? 0x01 ??? True.

????? ????? ????? ???????? COM ???????? Visual C# .NET

??? ???? ?????? COM Office ????? ????? ???????? COM ?? ??????? ?????? ???? ?? ?????? ?????? ????? Office ???? ???? ??? ??????? COM. ????? ????? ????? ???????? ?? COM ?? .NET ????? ?? ???? ???? ??????? ???????? ?? .NET ??? ?? ????? ?? ????? COM (??? ??????? Office) ??? ???? COM ??????? ??????? ????????.

?????? ????? ???????? COM ?? Visual C# .NET ???? ??????? ???????:
  1. ?? Visual C# .NET ? ????? "????? ??????" ???????.
  2. ????? ???? ??? ????? ????? ???? ???? IDTExtensibility2. ????? ??????? ??????? ???????? ??????? ??? ????? ?????? ??? ??? ???????? ???????.
  3. ????? ???? ??? ????? ?????? Microsoft Office. ????? ??????? ??????? ???????? ??????? ??? ????? ?????? ??? ??? Office.
  4. ????? ??? ???? ?? ????? ????? ????? IDTExtensibility2.
  5. ?? ????? ????? ??? ????? ? ????? ????? COM interop. ?????? ????? ????? ???? ???? ??????? ???? ??????? ??? ?? ?? ???????? ?? interop COM. ????? ??????? Regasm.exe ????? ???? .NET interop COM.
  6. ????? ??????? ??????? ???? ???? ?????? ??? ??????? Office ?? ????? ??????? ????????.
????? ?????? ?????? ???? ??????? ??????? ?? ????? ????? ????? .NET ?? ????? Addin ????????. ???? ??? ??? ????? "????? ???????? ???????" ???? ?????? ?? ????? ????? ???????? COM ?? .NET.

???? "????? ???????? ???????" ?? Visual C# .NET ??? ????? ??????? ?? ??? ??????? ???? ???? ??????? IDTExtensibility2. ?? ????? ??? skeleton ????? ????? ????? IDTExtensibility ?????. ????? ??? ??????? ????? ????????? ???????? ??????? ?Office. ??????? ??????? ??????? ?? ???? ???? ????? Interop COM ??????. ??? ????? ??? ????? (.snk) ????? ? ?????? ????? ?? ??? AssemblyKeyfile ?? Assemblyinfo.vb.

?? ??????? ????? ??? ??????? ?????? ????? ????? ???? ????? ????????? ???? ?? ??????? ???????? COM ??? ????? ??????? ????. ????? ????? ??? ??????? ??? ???? ?? ???.

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

  1. ?? ??????? ??? ?? Microsoft Visual Studio .NET ???? ??? ???? ?? ???? ??? ???????.
  2. ?? ???? ?????? ????? ???? ????? ?????? ???? ??? Project ????? ??? ???????? ???????? ??????? ?? ??? ???? ??????? ???????? ????????.
  3. ???? MyCOMAddin ??? ?? ??? ??????? ???????? ?? ???? ??? ?????.
  4. ??? ???? "????? ???????? ???????" ???? ??????? ???????:
    1. ??? ?????? 1 ??? ????? ??????? ???????? ???????? Visual C# ?? ???? ??? ??????.
    2. ?? ?????? 2 ? ???? ????????? ?????? ??????? ?? ???? ??? ??????:
      • Microsoft Word
      • Microsoft PowerPoint
      • Microsoft Outlook
      • Microsoft Excel
      • Microsoft Access
    3. ??? ?????? 3 ????? ??? ???? ?????? ??? ??????? ???????? ?? ???? ??? ??????.

      ?????? ???? ????? ???? ??????? ???????? ?? ???? ?????? ????? COM ???????? ?? ????? Office.

    4. ?? ?????? 4 ? ??? ???? ???????? ???????? ?? ???? ??? ??????.
    5. ???? ??? ?????.
  5. ?? ??????? ??????? ???? ??? ????? ????. ???? ??? System.Windows.Forms.DLL ?? ??????? ?????? ???? ??? ????? ?? ???? ??? ?????.
  6. ??? ?????? ??? ????? ?????? ??????? ?? ??? ???????:
    using System.Reflection;
  7. ????? ??? ??????? ??? ??? ???????:
    private CommandBarButton MyButton; 
  8. ????? ????????? ???????? ????? 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);
       }
    
    }
    
    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 ???? ??? .NET ?????? interop COM.
    2. ??? ????? ??? ??????? Office ???? ?????? ???????? ???? ??????? ???????? (??? ???? ??????? Microsoft Word ?? Microsoft Excel).
    3. ??? ??????? ???????? ??? ? OnStartupComplete ????? ????? ??????? ???????? ????? ?????. ??????? ???? ???????. ???? ?? ??????? ???????? ????? ?? ???? ???? ?? ??????? ????????? "?? ???? ????" ??? ???? ??????? ???????.
    4. ???? ??? ?? ???? ????. ??? ?????? ??? ????? ??? ?? ??? ??????? ???????? ????? ???? ?????. ??????? ???? ???????.
    5. ?? ?????? ????? Office.
    6. ????? ??????? OnBeginShutDown ??????? ????? ??? ???? ?????. ??????? ???? ??????? ?????? ????? ????????.

?????

?????? ??? ??????? ?????? ??? ????? ????? COM ???????? ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
190253INFO: ????? VB6 ?? ???? ?? VB5

???????

???? ???????: 302901 - ????? ??? ??????: 24/???? ??????/1428 - ??????: 10.2
????? ???
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Excel 2007
  • Microsoft Office Excel 2003
  • 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 Office Word 2003
  • Microsoft Word 2002 Standard Edition
????? ??????: 
kbmt kbautomation kbhowtomaster KB302901 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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