MailItem.Display(true|false) 在 WinForms 中使用时出现异常
MailItem.Display(true|false) exception when used in WinForms
我正在创建一个 Outlook 加载项,它有一个子窗体。表单上有一个按钮,如果用户单击它,我想通过它生成一个邮件项目。我想在电子邮件中自动填充一些信息,然后让用户在闲暇时发送。
我的代码如下所示:
private void btnMailDocNotice_Click(object sender, EventArgs e)
{
string clientInfo = string.Empty;
string matInfo = string.Empty;
string author = string.Empty;
string dType = string.Empty;
string fLocation = string.Empty;
string keyWords = string.Empty;
string docName = string.Empty;
clientInfo = this.mCboClient.Text + " " + lblClient;
matInfo = this.mCboMatter.Text + " " + lblMatter;
author = this.txtAuthor.Text;
dType = this.mCboDocType.Text.ToUpper();
fLocation = this.txtSavePath.Text;
keyWords = this.txtKeyWords.Text;
docName = this.txtDocName.Text;
this.sendDocNotice = true;
this.Hide();
CreateMailItem(clientInfo, matInfo, author, dType, this.operatorCode.ToUpper(), fLocation, keyWords, docName);
this.Show();
}
private void CreateMailItem(string clientInfo, string matInfo, string author, string dType, string profiledBy, string fLocation, string keyWords, string docName)
{
this.DNoticeItem = (Outlook.MailItem)ThisAddIn.myApp.CreateItem(Outlook.OlItemType.olMailItem);
this.DNoticeItem.Subject = "Document: " + docName;
this.DNoticeItem.HTMLBody = "<span style=\"font-family:Calibri; font-size: 11pt;\">KeyWords: " + keyWords + "</span>";
this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
this.DNoticeItem.HTMLBody += "<br />Matter: " + matInfo;
this.DNoticeItem.HTMLBody += "<br />Author: " + author;
this.DNoticeItem.HTMLBody += "<br />Doc Type: " + dtClient;
this.DNoticeItem.HTMLBody += "<br />Profiled by: " + profiledBy;
this.DNoticeItem.HTMLBody += "<br />File://" + fLocation;
this.DNoticeItem.Importance = Outlook.OlImportance.olImportanceNormal;
this.DNoticeItem.Display(false);
}
我 运行 遇到的问题是,它是否会在 mailitem.display 函数上触发异常,无论我使用 true 还是 false(做一些研究表明确定用户是否邮件项目打开时可以访问主 Outlook window 或不)。该异常是 "A dialog box is open. Close it and try again" 的 COM 异常。我试过在创建邮件项目的函数调用之前隐藏 WinForm,然后在函数退出后再次显示它,但它没有用。我已经尝试了一个代码版本,我使用 System.Diagnostics.Process.Start() 尝试在将文件保存到磁盘后打开文件,虽然它不会从加载项中触发异常,但 Outlook 会提示用户来自 ComException 的相同消息的消息框。我什至尝试创建一个字段来查看是否应该起草文档通知电子邮件,并认为在 form.close() 调用后让代码处理这个问题,认为关闭调用至少会处理对话框那是锁定 Outlook,我仍然遇到同样的异常。
有没有办法实现我想要的?有没有人有什么建议?我现在有点卡住了,非常感谢 help/pointers/suggestions 任何人在这个问题上提供的帮助。如果这是一个重复的问题,我深表歉意 - 我找不到问题的好答案。预先感谢您的宝贵时间。
首先,为什么不无模式地显示你自己的表格?
其次(这很重要)不要使用如下代码
this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
每次你 运行 这样的一行,你检索 HTMLBody,向它添加一些东西(使 HTML 格式错误),然后设置 HTMLBody 并强制 Outlook 理解您的(格式错误的)HTML。这(假设 Outlook 可以解析并修复您的 HTML)将导致返回的 HTML 与您设置的不同。
使用常规字符串构建 HTML body 一次,并仅设置一次 HTMLBody 属性。
我正在创建一个 Outlook 加载项,它有一个子窗体。表单上有一个按钮,如果用户单击它,我想通过它生成一个邮件项目。我想在电子邮件中自动填充一些信息,然后让用户在闲暇时发送。
我的代码如下所示:
private void btnMailDocNotice_Click(object sender, EventArgs e)
{
string clientInfo = string.Empty;
string matInfo = string.Empty;
string author = string.Empty;
string dType = string.Empty;
string fLocation = string.Empty;
string keyWords = string.Empty;
string docName = string.Empty;
clientInfo = this.mCboClient.Text + " " + lblClient;
matInfo = this.mCboMatter.Text + " " + lblMatter;
author = this.txtAuthor.Text;
dType = this.mCboDocType.Text.ToUpper();
fLocation = this.txtSavePath.Text;
keyWords = this.txtKeyWords.Text;
docName = this.txtDocName.Text;
this.sendDocNotice = true;
this.Hide();
CreateMailItem(clientInfo, matInfo, author, dType, this.operatorCode.ToUpper(), fLocation, keyWords, docName);
this.Show();
}
private void CreateMailItem(string clientInfo, string matInfo, string author, string dType, string profiledBy, string fLocation, string keyWords, string docName)
{
this.DNoticeItem = (Outlook.MailItem)ThisAddIn.myApp.CreateItem(Outlook.OlItemType.olMailItem);
this.DNoticeItem.Subject = "Document: " + docName;
this.DNoticeItem.HTMLBody = "<span style=\"font-family:Calibri; font-size: 11pt;\">KeyWords: " + keyWords + "</span>";
this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
this.DNoticeItem.HTMLBody += "<br />Matter: " + matInfo;
this.DNoticeItem.HTMLBody += "<br />Author: " + author;
this.DNoticeItem.HTMLBody += "<br />Doc Type: " + dtClient;
this.DNoticeItem.HTMLBody += "<br />Profiled by: " + profiledBy;
this.DNoticeItem.HTMLBody += "<br />File://" + fLocation;
this.DNoticeItem.Importance = Outlook.OlImportance.olImportanceNormal;
this.DNoticeItem.Display(false);
}
我 运行 遇到的问题是,它是否会在 mailitem.display 函数上触发异常,无论我使用 true 还是 false(做一些研究表明确定用户是否邮件项目打开时可以访问主 Outlook window 或不)。该异常是 "A dialog box is open. Close it and try again" 的 COM 异常。我试过在创建邮件项目的函数调用之前隐藏 WinForm,然后在函数退出后再次显示它,但它没有用。我已经尝试了一个代码版本,我使用 System.Diagnostics.Process.Start() 尝试在将文件保存到磁盘后打开文件,虽然它不会从加载项中触发异常,但 Outlook 会提示用户来自 ComException 的相同消息的消息框。我什至尝试创建一个字段来查看是否应该起草文档通知电子邮件,并认为在 form.close() 调用后让代码处理这个问题,认为关闭调用至少会处理对话框那是锁定 Outlook,我仍然遇到同样的异常。
有没有办法实现我想要的?有没有人有什么建议?我现在有点卡住了,非常感谢 help/pointers/suggestions 任何人在这个问题上提供的帮助。如果这是一个重复的问题,我深表歉意 - 我找不到问题的好答案。预先感谢您的宝贵时间。
首先,为什么不无模式地显示你自己的表格?
其次(这很重要)不要使用如下代码
this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
每次你 运行 这样的一行,你检索 HTMLBody,向它添加一些东西(使 HTML 格式错误),然后设置 HTMLBody 并强制 Outlook 理解您的(格式错误的)HTML。这(假设 Outlook 可以解析并修复您的 HTML)将导致返回的 HTML 与您设置的不同。
使用常规字符串构建 HTML body 一次,并仅设置一次 HTMLBody 属性。