我正在开发一个Outlook Addin(使用Visual Studio社区2013),以允许用户轻松地为我们的客户服务部门填充电子邮件模板.
插件运行良好,但是我需要添加一些功能,以便在实际发送电子邮件后将一些数据(跟踪KPI)记录到数据库.
经过一些研究,我为Applications itemSend事件实现了一个Class级事件处理程序.
这在开发机器上完美地工作,但是当我在客户端上发布和安装应用程序时,事件似乎不会被触发,因为不会调用将数据记录到数据库的代码.
由于我无法使其正常工作,我尝试了一个解决方案,然后将其更改为使用已发送文件夹的.ItemAdd事件. (代码是从Pranav借来的:https://easyvsto.wordpress.com/2010/07/27/how-to-save-mail-content-when-a-mail-is-sent-from-outlook/)
这也适用于开发机器,但同样,一旦在客户端macine上安装了插件,事件似乎无法触发.
正如我现在看到两种不同方法的相同行为,它让我觉得有一些显而易见的事情.我错过了什么或没有考虑到某些事情?
代码示例如下:
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例程.
如果我删除了该呼叫,则两个消息框都将作为两个事件的结果显示.
如果您需要更多细节,请告诉我.
亲切的问候,
大卫
UPDATE
根据Cadogi评论修改了代码.谢谢.
UPDATE2
在Try Catch中将调用包装到SentMailtimeStamp路由中,确实在客户端中有错误.
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
我已经解决了这个问题,感谢Hans向我指出了一些非常简单的调试课程 – 这是一个很难吸取的教训,因为这需要花费几周的时间来解决.
汉斯绝对认真.
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.
Addin在开发机器而不是客户端上工作的原因是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)