我正在开发一个 Outlook 插件,它通过 Web 服务进行身份验证,以从数据库获取数据并将数据存储在数据库中。 当插件启动时,它会查询 Web 服务以确定安装的插件版本是否是当前版本,如果不是,则会通过以下方式从 Outlook 卸载插件:
Application.COMAddIns.Item("foo").Connect = false;
为了查询 Web 服务,必须对其进行身份验证。凭据是从 Windows 注册表中的加密字符串中检索的。这些凭据来自 Form 对象,该对象在加载项启动或对 Web 服务进行查询时运行,并且无法从注册表检索用户名和/或密码,通常是由于有人删除了所述值。
每当保存凭据时,这些凭据都会用于查询 Web 服务以检查插件版本是否正确。如果不是,则 COM 插件将与 Outlook 断开连接。
每当出于其他目的查询 Web 服务时,都会首先进行查询以检查插件版本是否正确。如果不是,则 COM 插件将与 Outlook 断开连接。
据我所知,断开插件连接只能从 Outlook.Application 对象完成,到目前为止我只能从我的插件对象访问该对象。
我需要弄清楚的是,当我不在我的 Addin 对象中时,如何断开 Outlook Addin 的连接或以其他方式禁用它?
最佳答案
我设法通过功能区的上下文访问 COM 对象,因此我通过创建一个公共(public)静态方法来解决这一切,该方法将 COMAddIn 对象作为参数,从那里我可以做任何我想做的事情:)
对所有插件的引用可以通过功能区的 Context 属性引用,如下所示:
Microsoft.Office.Core.COMAddIns comaddins = ((this.Context as Outlook.Explorer).Application.COMAddIns.Application as Outlook.Application).COMAddIns;
静态方法如下所示:
public static void ThisAddIn_CheckVersion(Microsoft.Office.Core.COMAddIn ThisAddIn)
{
var rk = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Office\\Outlook\\Addins\\My Outlook Add-in");
if (rk.GetValue("Username") == null || rk.GetValue("Password") == null)
{
new EditSettingsForm(ThisAddIn).Show();
return;
}
var sc = new MyWebService.WebServiceClient();
sc.ClientCredentials.UserName.UserName = (rk.GetValue("Username") == null ? null : rk.GetValue("Username").ToString());
sc.ClientCredentials.UserName.Password = (rk.GetValue("Password") == null ? null : Encryptor.Decrypt(rk.GetValue("Password").ToString()));
if (sc.GetMyOutlookAddinVersionNumber() != "TESTING")
{
System.Windows.Forms.MessageBox.Show("The version of My Outlook 2013 Add-in you're using is too old. Please update to the latest version at http://www.foo.bar/");
ThisAddIn.Connect = false;
}
sc = null;
}
关于c# - 以编程方式禁用 Outlook 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704389/