COM ADD-in,Outlook,ItemSend

时间:2011-01-24 08:08:49

标签: vb.net

我正在尝试编写一个Office 2007 COM加载项,它将在邮件发送之前检查一些规则。下面的测试代码有效,但我在VS2010中有几个警告,也许有人可以帮我解决这个问题。

以下代码:

Imports Extensibility
Imports System.Runtime.InteropServices
Imports Outlook = Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Core
Imports System.Reflection
Imports Microsoft.Win32



<GuidAttribute("B19F59E7-4F71-475C-9531-FB46842E5E5E"), ProgIdAttribute("DODATKI.Connect")> _
Public Class Connect

 Implements Extensibility.IDTExtensibility2
    Dim WithEvents OutlookApplication As Microsoft.Office.Interop.Outlook.Application
 Private applicationObject As Object
    Private addInInstance As Object

    Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
        'MsgBox("Add-in is OnBeginShutdown")
    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
        MsgBox("Add-in is OnStartupComplete")
        OutlookApplication = New Outlook.Application
    End Sub

 Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
 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("Add-in is 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 Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles OutlookApplication.ItemSend
        System.Windows.Forms.MessageBox.Show("Hi, You are sending message")
        Dim Msg As Outlook.MailItem
        Msg = Item

        If Trim(Item.Subject) = "" Then
            System.Windows.Forms.MessageBox.Show("Cannot send message with an empty subject!!!")
            Cancel = True
        End If
    End Sub
End Class

警告:

1) On Msg = Item
Warning 1 Implicit conversion from 'Object' to Microsoft.Office.Interop.Outlook.MailItem'
2) On Item.Subject
Warning 2 Implicit conversion from 'Object' to 'String'
3) On Item.Subject
Warning 3 Late bound resolution; runtime errors could occur

1 个答案:

答案 0 :(得分:0)

关于1:

Msg = CType(Item, Outlook.MailItem)

关于2和3:

If Trim(Msg.Subject)

另一个建议是打开Option Explicit和Option Strict,即在每个文件的顶部添加类似

的内容
Option Explicit On
Option Strict On