VB.NET Visual Studio Outlook 2013 Addin 项目 - 事件似乎未触发

标签 vb.net events outlook-addin

感谢您花时间查看我的第一篇文章。

我正在开发一个 Outlook 插件(使用 Visual Studio Community 2013),以允许用户轻松填充我们客户服务部门的电子邮件模板。 该插件运行良好,但是我需要添加一些功能来在电子邮件实际发送后将一些数据(以跟踪 KPI)记录到数据库。

经过一些研究,我为应用程序 itemSend 事件实现了类级事件处理程序。

这在开发计算机上完美运行,但是当我在客户端上发布并安装应用程序时,事件似乎没有触发,因为将数据记录到数据库的代码没有被调用。

由于我无法让它工作,我尝试了一种解决方法,随后将其更改为使用发送文件夹的 .ItemAdd 事件。 (代码是从 Pranav 那里借用的: https://easyvsto.wordpress.com/2010/07/27/how-to-save-mail-content-when-a-mail-is-sent-from-outlook/ )

这在开发计算机上也可以完美运行,但同样,一旦在客户端计算机上安装了插件,该事件似乎就无法触发。

由于我现在用两种不同的方法目睹了相同的行为,这让我认为有一些明显的我没有做的事情。我是否遗漏了什么或没有考虑到什么?

代码示例如下:

Imports Microsoft.Office.Tools.Ribbon
Imports System.Runtime.InteropServices
Imports System.IO
Imports MySql.Data.MySqlClient
Imports System.Diagnostics

Public Class ThisAddIn
Public WithEvents oSentFolder As Outlook.Folder
Public WithEvents oSentItems As Outlook.Items

Private Sub ThisAddIn_Startup() Handles Me.Startup
    oSentFolder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
    oSentItems = oSentFolder.Items
End Sub

Private Sub InsertSurveyTimestamp() Handles oSentItems.ItemAdd
    System.Windows.Forms.MessageBox.Show("oSentItems.ItemAdd Event")
End Sub
Private Sub Application_ItemSend(ByVal oItem As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
    System.Windows.Forms.MessageBox.Show("Application.ItemSend Event")
    log.WriteToErrorLog("Application ItemSend Reached", "", "Event")
    If TypeOf oItem Is Outlook.MailItem Then Call SentMailTimestamp(oItem)
    System.Windows.Forms.MessageBox.Show("end of Itemsend Event")
End Sub

Private Sub SentMailTimestamp(oitem As Outlook.MailItem)
    log.WriteToErrorLog("SentMailTimestamp Sub Reached", "", "Subroutine Flag")
    Try
        'Dim oitem As Outlook.MailItem = oSentItems.Item(oSentItems.Count) 'for use with oSentItems.ItemAdd event
        'Check the CSOSurvey property exists to make sure its a CSO Survey email, exit if not a CSOSurvey Email
        Dim o = oitem.ItemProperties("CSOSurvey")
        If (o IsNot Nothing) AndAlso (o.Value IsNot Nothing) Then
            System.Diagnostics.Debug.WriteLine("CSOsurvey email: " & o.Value.ToString)
            log.WriteToErrorLog("Email was CSO Survey", "Value: " & o.Value.ToString, "Email Property")
        Else
            System.Diagnostics.Debug.WriteLine("CSOsurvey email: Null")
            log.WriteToErrorLog("Email was NOT CSO Survey", "", "Email Property")
            oitem = Nothing
            o = Nothing
            Exit Sub
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    'Save the timedatestamp of the email template being sent
    Using sqlCommand As New MySqlCommand
        Dim querystr As String = "INSERT INTO cs_survey_emaillog (SRID,exec,datetimestamp) values(@srid,@exec,@datetimestamp)"
        Dim mysqlconn As MySqlConnection
        Try
            mysqlconn = New MySqlConnection(GlobalVariables.connstr)
            mysqlConn.Open()
        Catch ex As MySqlException
            System.Windows.Forms.MessageBox.Show("Unable to write to Log. Please contact bst.uk@chep.com" & vbCrLf & ex.Message)
            log.WriteToErrorLog(ex.Message, ex.StackTrace, "MySQL Error")
            Exit Sub
        End Try
        If mysqlconn.State = Data.ConnectionState.Open Then
            Try
                With sqlCommand
                    .Connection = mysqlconn
                    .CommandText = querystr
                    .CommandType = Data.CommandType.Text
                    .Parameters.AddWithValue("@srid", ThisAddIn.templateSRID)
                    .Parameters.AddWithValue("@exec", ThisAddIn.GetUserName())
                    .Parameters.AddWithValue("@datetimestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                    Dim numrec As Integer = .ExecuteNonQuery()
                    If ThisAddIn.GetUserName = "mclachd" Then System.Windows.Forms.MessageBox.Show("Query executed. " + numrec.ToString + " Records sent")
                End With
            Catch ex As MySqlException
                log.WriteToErrorLog(ex.Message, ex.StackTrace, "MySQL Error")
            Finally
                mysqlconn.Close()
                mysqlconn.Dispose()
            End Try
        Else
            log.WriteToErrorLog("Could not open db connection", "", "MySQL Error")
            mysqlconn.Dispose()
        End If
    End Using
End Sub

在这种情况下,SentMailTimeStamp 例程从未达到。

如果我删除调用,则这两个事件都会显示两个消息框。

如果您需要更多详细信息,请告诉我。

亲切的问候,

大卫

更新 已根据 Cadogi 注释修改了代码。谢谢。

更新2 已将对 SentMailtimeStamp 路由的调用包装在 Try Catch 中,我确实在客户端中遇到了错误。

Private Sub Application_ItemSend(ByVal oItem As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
    System.Windows.Forms.MessageBox.Show("Application.ItemSend Event")
    log.WriteToErrorLog("Application ItemSend Reached", "", "Event")
    Try
        If TypeOf oItem Is Outlook.MailItem Then Call SentMailTimestamp(oItem)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try
    System.Windows.Forms.MessageBox.Show("end of Itemsend Event")
End Sub

错误信息是:

无法加载文件或程序集“MySQL.Data,version=6.8.5.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d”或其依赖项之一。系统无法精细指定的文件

太棒了,如果感觉我正在进步的话。我假设已发布的项目将包含所需的所有内容。

更新3 我已经解决了这个问题,感谢汉斯,他向我指出了一些非常简单的调试经验教训 - 这是一个艰难的教训,因为这花了我几周的时间来解决。

汉斯绝对准确。

There is something obvious you are missing, Office programs are not that accommodating to add-ins that misbehave. By default any exception they throw in an "awkward" place is swallowed without a diagnostic. The only way to tell that this happened is that code you think should have a side-effect is just not doing its job.

插件在开发计算机上运行而不是在客户端上运行的原因是 MySQL.Data 引用需要将 Copy Local 属性设置为 True。

对于那些最终处于与我类似位置的人,解决方法是转到您的项目属性->引用。突出显示 MySQL.Data 并将 Copy Local 属性更改为 True

感谢大家的贡献,它对我的​​学习有所帮助,并帮助缩小了问题的范围。

最佳答案

您明显遗漏了一些东西,Office 程序无法容纳行为不当的加载项。默认情况下,他们在“尴尬”的地方抛出的任何异常都会在没有诊断的情况下被吞掉。判断发生这种情况的唯一方法是,您认为应该产生副作用的代码只是没有完成其工作。

你对此所做的事情已经很好地介绍了,谷歌“VSTO异常处理”。其中一个点击是 this MSDN article ,我将复制粘贴相关部分:

Visual Studio Tools for Office can write all errors that occur during startup to a log file or display each error in a message box. By default, these options are turned off for application-level projects. You can turn the options on by adding and setting environment variables. To display each error in a message box, set the VSTO_SUPPRESSDISPLAYALERTS variable to 0 (zero). You can suppress the messages by setting the variable to 1 (one). To write the errors to a log file, set the VSTO_LOGALERTS variable to 1 (one). Visual Studio Tools for Office creates the log file in the folder that contains the application manifest. The default name is .manifest.log. To stop logging errors, set the variable to 0 (zero)

关于VB.NET Visual Studio Outlook 2013 Addin 项目 - 事件似乎未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31994760/

相关文章:

java - Java 和 vb.net 中的 SHA-256 不同值

c# - 多次附加事件处理程序

c# - 遗留代码 visual basic to c#

asp.net - 你能告诉我为什么我收到 `The GridView ' GridView 1' fired event RowUpdating which wasn' t Handling.` 错误消息

C++ 等待所有线程完成

macos - 让 NSWindow 拒绝键盘事件

outlook-addin - CustomProperties 不会保存在使用 Office.js 的 Outlook 加载项中的定期 session 中

c# - Outlook OpenSharedItem 丢失用户属性

outlook-addin - 更改 Outlook xml 功能区选项卡中的显示顺序

c# - 在另一个窗体中显示 Winform 'Form',就好像它是用户控件一样