Microsoft Office 2000 ?? ??? ??? ????????? ??-???? ??????? ???? ?? ??? ?? Office ??????????? ?? ????????? ???? ?? ??? ????? ?? ??? ??? ???, ?????????? ??????? ??????????? ?? ?????? ??? ?? ??-???? COM ??-???? ??? ???? ??? ?? ??? ?? ??? ???? Office COM ??-???? ?? ???? ??? ????? ???? ?? ?? ????? ?? ?? ???? ?? Office COM ????? ?? ??? Microsoft Visual Basic .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. ???
OnConnectionreturns 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.
OnConnectiontakes 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- The add-in is started by the end user from theCOM ??-????????? ????? ?? ??????? ?? ??? ?? ..
- ext_cm_CommandLine- The add-in is connected from the command line. ????? ??? ?? ?? ?? ???? ???? COM ??-???? Office ??????????? ?? ??? ??? ??? ???
- ext_cm_External- The add-in is connected by an external application through
Automation. ??? ???? ?? ?? ???? ???? ???? COM ??-???? ?? Office ?? ??? ????? ?? ??? ??????????
- ext_cm_Startup- The add-in is started by the host at application startup. ?? ??????? ?? ????????? ??? ??? ?????? ?????? ????????? ???
- AddInInst- A reference to theCOMAddIn???????? ?? ??-?? ??? ?? ??????? ??COMAddIns????? ????????? ?? ??? ?????? ?????
- ?????- An array ofVarianttype 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 should restore any changes made to the
host application.
OnDisconnectiontakes 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
closed.
- 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. ????? ?????? ???, ?? ?? ??? COM ??-?? ?? ??????? ?? ????? ????????? ?? ????? ???? ??, ?? ????? fires.
OnStartupComplete ?? OnBeginShutdown
?????
OnStartupComplete??
OnBeginShutdown??????? ??? ???? ?? ?? ????? ????????? ??? ?? ?? ???? ?????? ??? ???-?? ?????????? ???????? ????? ?? avoided ??????? ????????? ?? ?? ?????? ?? ????? unloading ?????? ??? ???? ??? ???? ???
OnStartupComplete??? ??-??, ????????? ?? ????? ?????? ???? ??? ?? ?? ???? ?????? ?? ??
OnBeginShutdown???? ?????? ?? ??? ?????????? ????? ???? ??-?? ?? ?? ???? ???? ?? ????? ???
?? ?? ??????? ???? ????? ????????? ?? ??? ?????????? ???????? ???? ??? ?????? ??, ??????? ?? ?????? ???? ???????? ?? ?? ?? ??? ???????? ???? ?? ??? ??????? ????? ?? ???? ??
OnConnection, ??
OnDisconnection.
COM ??-?? ?? ???????
??????? COM ???????, ?? ????? ??? COM ??-?? ?? ????? ??? ?? ?? ???? ???????? Office ????????? ?? ??? ??????? ???? ?? ???? ??????? ???? ?? ??? ????? ?? ????? ????????? ?? ???, ??-?? ????? ????? ????? ?? ???, ????? ????? ?? ??????? ??? ?? ??? ??? ???? ProgID ?? ????? ???? ???, ???????:
HKEY_CURRENT_USER\Software\Microsoft\Office\OfficeApp\Addins\ProgID
??-?? ?????? ?? ???? ??? ???????? ???????? ??? ?? ????? ????? ???? ?? ??? ????? ?? ????? ?? ???? ???? ?????, ??-?? ????? ????????? ?????? ??? ??????? ???? ???? DWORD ??? ?? ????? ??
LoadBehavior
. ?? ??? ?? ???? ??-?? ????? ????????? ?????? ??? ??, ?? ????? ????? ?? ?? ?????? ?? ??? ????????? ???? ??:
- 0 ?????????? = - ??? ???? ???
- 1 = Connected - ??? ?? ?? ???
- 2 Bootload - ????????? ????????? ?? ??? ???? =?
- 8 = DemandLoad - ?????????? ?????? ???????? ???? ?? ???? ??? ?????
- 16 - ConnectFirstTime (???? ?????????) ?? ???? ?? ??? ??? =?
??????? ??? ????????? ?? 0x03 (Connected | Bootload)?
??-???? ?? ??????????? ???? ????
IDTExtensibility2??? DWORD ??? ?? ?? ????????? ???? ?????
CommandLineSafe
?? ????? ???? ??? ?? ???? ??-?? ????????? ???? ?????????? ??????? ?? ?????? ???? ???? ?? ??? ???????? ??? ???? ?? ???? ???? ??? ?? 0x00 false ????? ???? ??, ?? 0x01 ?? ??? ??? ??? ????? ???? ???
Visual Basic .NET ?? ????? ?? ???? COM ??-?? ????? ???? ????
???? ????? ???, ?? ??? ??? ??? Office COM ??-?? ?? ?? ???-????????? COM ????? COM ??-???? ??? ?? ?????? ?? ???? Office ????????? ?????? ?????? ??? ?????, ???? COM ??-?? ??? Visual Basic .NET ?? ?????? ?? ??-?? ??? .NET ??? ???? ???? ??? ?? COM ??????? ?? ????? ???? ?? ???? (?? ????, Office ?????????) COM ?????? ??? ?? ?????? ???
????? ?? ??? ??? COM ??-?? Visual Basic .NET ???, ????? ????? ?? ???? ????:
- Visual Basic .NET, ??? ?? ????? ????????? ????? ??????????
- ???? ???? ?? ?? ?????? ????????? ?? ??? ??? ?????? ??????IDTExtensibility2. ???????? ?????? ??????? ?? ??? ?? ???? ?? ?? ??? ?? ??????? ?????? ??Extensibility.
- Microsoft Office ???????? ????????? ?? ??? ??? ?????? ??????? ???????? ?????? ??????? ?? ??? ?? ???? ?? ?? ??? ?? ??????? ?????? ??Office.
- ???? ???? ?? ?? ????? ????????? ??? ??? ????????? ???? ?????IDTExtensibility2.
- ???? ?? ??? ????????? ????? ??, COM interop ?? ??? ????????? ??????? ????? ??? ???? ?? ???, ????? ?? ????? ???? ?? ?? ????????? ?? ??? ??????? ?? ??? ?? ???? ??? ??? COM interop ?? ??? ??????? ????? COM interop ?? ??? ??? .NET ??? ?? ??????? ???? ?? ??? ?? Regasm.exe ?? ????? ?? ???? ????
- ????????? ???????????? ????? ???? Office ??????????? ?? ????? ?? ??-?? ??? ?? ???? ????
?? ??? ?? ????? ?? ???? ???? ?? ??? ??? ???? ???, ?? ?? ?????? ?? ??? .NET ????????? ??? ???? ???
???? Addin. ?? ??????? Extensibility ???????, ?? .NET ??? ??? COM ??-?? ????? ??? ??? ???? ???
Extensibility ???????? ????? ???? Visual Basic .NET ????? ????????? ????????? ?? ??? ??
??????.???? ?? ???? ???? ??
IDTExtensibility2???????? ??? Skeleton ??? ?? ?? ???? ??????? ?? ???? ???? ??
IDTExtensibility??? ????? ?? ??? ?? ????????? ??? Extensibility ?? Office assemblies ?? ?????? ???? ????????? ?? ????? ???????? ??
COM Interop ?? ??? ????????????. ??????? ????? (.snk) ???? ????? ???? ??? ?? ?? ??? ???????? ??
AssemblyKeyfile??????? Assemblyinfo.vb ??? ???
????? ????????? ????????? ?? ??? ???????? ????? ????? ????????? ?? COM ??-?? ???? ?????????? ?? ?????? ???? ?? ??? ????? ?? ???? ???? ??? ?????, ?? ?? ?? ????????? ????? ???? ????
Step-by-Step ??????
- ????? ????????Microsoft Visual Studio .NET, ??? ???? ????? ????????? ????-????? ????, ?? ???? ????????????.
- ???????? ?????????????? ????? ???, ??????? ???????? ??????????? ???????????????? ????????? ????Extensibility ?????????, ?? ???? ??? ?? ??? ???????? ??? ?? ??-??????????:.
- ??????:MyCOMAddinas the name of the
add-in, and then clickOK.
- ?? Extensibility ??????? ????? ???? ??, ?? ????? ????? ?? ???? ????:
- 1, ?? ??? ???? ????? ??Create an Add-in using Visual Basic?? ????-????? ????, ?? ???? ???next.
- ????? 2, ?? ????? ????? ????????? ?? ??? ????, ?? ???? ??? ????? ????next:
- Microsoft Word
- Microsoft PowerPoint
- Microsoft Outlook
- Microsoft Excel
- Microsoft Access
- ????? 3, ?? ???? ??-?? ?? ??? ????? ?? ??? ?????? ????, ?? ???? ???next.
???:The name and description of the add-in appear in theCOM ??-??????? ????? Office ????????? ??? ???
- ????? ?? 4, ??? ?????? ?????? ?? ??? ????, ?? ???? ??? ????? ????next.
- ????? ????,??????.
- ???? ?? ??? ????? ????? ???????????.????:
Dim WithEvents MyButton As CommandBarButton
- Implement the code for all the members ofIDTExtensibility2???????????.????, follows ?? ??? ???:
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
On Error Resume Next
' Notify the user you are shutting down, and delete the button.
MsgBox("Our custom Add-in is unloading.")
MyButton.Delete()
MyButton = Nothing
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
'
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
Dim oCommandBars As CommandBars
Dim oStandardBar As CommandBar
On Error Resume Next
' Set up a custom button on the "Standard" command bar.
oCommandBars = applicationObject.CommandBars
If oCommandBars Is Nothing Then
' Outlook has the CommandBars collection on the Explorer object.
oCommandBars = applicationObject.ActiveExplorer.CommandBars
End If
oStandardBar = oCommandBars.Item("Standard")
If oStandardBar Is Nothing Then
' Access names its main toolbar Database.
oStandardBar = oCommandBars.Item("Database")
End If
' In case the button was not deleted, use the exiting one.
MyButton = oStandardBar.Controls.Item("My Custom Button")
If MyButton Is Nothing Then
MyButton = oStandardBar.Controls.Add(1)
With MyButton
.Caption = "My Custom Button"
.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.
.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 clicks the button,
' MSO loads the add-in automatically and then raises
' the Click event for the add-in to handle.
.OnAction = "!<MyCOMAddin.Connect>"
.Visible = True
End With
End If
' Display a simple message to show which application you started in.
MsgBox("Started in " & applicationObject.Name & ".")
oStandardBar = Nothing
oCommandBars = Nothing
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
On Error Resume Next
If RemoveMode <> Extensibility.ext_DisconnectMode.ext_dm_HostShutdown Then _
Call OnBeginShutdown(custom)
applicationObject = Nothing
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
MsgBox("On Connection In MyAddin")
applicationObject = application
addInInstance = addInInst
' If you aren't in startup, manually call OnStartupComplete.
If (connectMode <> Extensibility.ext_ConnectMode.ext_cm_Startup) Then _
Call OnStartupComplete(custom)
End Sub
Private Sub MyButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles MyButton.Click
MsgBox("Our CommandBar button was pressed!")
End Sub
- ????? ?? COM ??-?? ?? ??????? ????? ??? ???? ?? ???, ????? ????? ?? ???? ????::
- ????? ????????????? ??,Build MyCOMAddin. ????? ??? ?? COM ??-?? ????? registers .NET ???? COM interop ?? ???
- ?? ?? ???? ?????? ????? ????? ????????? ?? ??? ??? ???? ??-?? (?????? ?? ???, Microsoft Word ?? Microsoft Excel) ?? ??? Office ????????? ??????? ?????
- ??-?? ??????? ?? ??? ??, ?? ???OnConnectionevent is
fired, and you receive a message box. When you dismiss the message box, theOnStartupCompleteevent fires, and you receive a second message box. Dismiss the
message box.
- 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 box. Dismiss the message box to end the
demonstration.
For
additional information about writing COM add-ins, click the article number
below to view the article in the Microsoft Knowledge Base:
190253
(http://support.microsoft.com/kb/190253/EN-US/
)
INFO: VB6 Designers Do Not Work in VB5
Microsoft Internet Explorer ?? ??? ???-?????? ?????? ?? ???? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ??? ???? ?? ????:
(?) Microsoft Corporation 2001, ??? ?????? ?????????
Contributions by Ranjit R. Sawant, Microsoft Corporation.