imports Extensibility
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core
Read me for Add-in installation and setup information.
<GuidAttribute("E8CB3356-99C5-40A0-9169-C39D795E13D4"), ProgIdAttribute("MyAddin2.Connect")> _
Public Class Connect
    
    
Implements Extensibility.IDTExtensibility2
    
Dim WithEvents CBtnbar As CommandBarButton
    
Dim WithEvents CBtnCG As CommandBarButton

    
Dim applicationObject As Object
    
Dim addInInstance As Object
    
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.
        CBtnbar.Delete()
        CBtnbar 
= Nothing
        CBtnCG.Delete()
        CBtnCG 
= Nothing
        
'  MsgBox("delete")
    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
        
Dim cmbMenu As CommandBar
        
Dim ctlPopup As CommandBarPopup
        
Dim applicationType As System.Type
        
Const m_sMenuCaption As String = "Testing Addin"
        
Const m_sMenuTag As String = "OutlookAddinTesting"
        
Const m_sCmdCaption As String = "Configuration"
        
Const m_sCmdTag As String = "OutlookAddinConfigTesting"
        
Dim strtype As String = applicationObject.name.ToString()

        
On Error Resume Next
        
If strtype = "Outlook" Then
            cmbMenu 
= applicationObject.ActiveExplorer.CommandBars("Tools")
            ctlPopup 
= cmbMenu.FindControl(MsoControlType.msoControlPopup, System.Type.Missing, m_sMenuTag, TrueTrue)
            
If ctlPopup Is Nothing Then
                ctlPopup 
= cmbMenu.Controls.Add(Type:=MsoControlType.msoControlPopup, Temporary:=True, Before:=1)
                
With ctlPopup
                    .Tag 
= m_sMenuTag
                    .Caption 
= m_sMenuCaption
                    CBtnCG 
= .Controls.Add(Type:=MsoControlType.msoControlButton, Temporary:=True, Before:=1)
                    CBtnCG.Caption 
= m_sCmdCaption
                    CBtnCG.Tag 
= m_sCmdTag
                    CBtnCG.Enabled 
= True
                
End With
            
End If
        
End If




        
' 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
        
' oStandardBar.Reset()
        ' In case the button was not deleted, use the exiting one.
        CBtnbar = oStandardBar.Controls.Item("Testing CommandbarControl")
        
If CBtnbar Is Nothing Then

            CBtnbar 
= oStandardBar.Controls.Add(1)
            
With CBtnbar
                .Caption 
= "Testing CommandbarControl"
                .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 
= "Testing"

                
' 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
        cmbMenu = Nothing
        ctlPopup 
= Nothing
        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 ObjectByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As ObjectByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
        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 CBtnCG_Click(ByVal Ctrl As CommandBarButton, ByRef CancelDefault As BooleanHandles CBtnCG.Click
        
MsgBox("Config Button of menu")
    
End Sub

    
Private Sub CBtnbar_Click(ByVal Ctrl As CommandBarButton, ByRef CancelDefault As BooleanHandles CBtnbar.Click
        
MsgBox("Our CommandBar button was pressed!")
    
End Sub


End Class


posted on 2007-08-16 11:37    阅读(722)  评论(0)    收藏  举报