C# 在WinForms中使用时显示(true | false)异常

C# 在WinForms中使用时显示(true | false)异常,c#,winforms,email,outlook,outlook-addin,C#,Winforms,Email,Outlook,Outlook Addin,我正在创建一个Outlook加载项,它有一个子表单。表单上有一个按钮,如果用户单击它,我想通过它生成一个邮件项。我想在电子邮件中自动填充一些信息,然后让用户在空闲时发送 我的代码如下所示: private void btnMailDocNotice_Click(object sender, EventArgs e) { string clientInfo = string.Empty; string matInfo = string.Empty;

我正在创建一个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);
    }
private void btnMailDocNotice\u单击(对象发送者,事件参数e)
{
string clientInfo=string.Empty;
string matInfo=string.Empty;
string author=string.Empty;
string dType=string.Empty;
string fLocation=string.Empty;
字符串关键字=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;
关键词=this.txtKeyWords.Text;
docName=this.txtDocName.Text;
this.sendDocNotice=true;
this.Hide();
CreateMailItem(clientInfo、matInfo、作者、数据类型、this.operatorCode.ToUpper()、fLocation、关键字、docName);
this.Show();
}
私有void CreateMailItem(字符串clientInfo、字符串matInfo、字符串作者、字符串数据类型、字符串profiledBy、字符串fLocation、字符串关键字、字符串docName)
{
this.DNoticeItem=(Outlook.MailItem)ThisAddIn.myApp.CreateItem(Outlook.OlItemType.olMailItem);
this.DNoticeItem.Subject=“文件:”+docName;
this.DNoticeItem.HTMLBody=“关键字:”+关键字+”;
this.DNoticeItem.HTMLBody+=“
客户端:”+clientInfo; this.DNoticeItem.HTMLBody+=“
物质:”+matInfo; this.DNoticeItem.HTMLBody+=“
作者:”+作者; this.DNoticeItem.HTMLBody+=“
单据类型:”+dtClient; this.DNoticeItem.HTMLBody+=”
由“+profiledBy; this.DNoticeItem.HTMLBody+=“
文件://”+fLocation; this.DNoticeItem.Importance=Outlook.OlImportance.olImportanceNormal; 此.DNoticeItem.Display(false); }
我遇到的问题是,无论我使用的是true还是false,它都会在mailitem.display函数上引发异常(进行一些研究,确定用户是否可以在mailitem打开时访问Outlook主窗口)。该异常是“对话框已打开。请关闭并重试”的COM异常。我尝试在创建邮件项的函数调用之前隐藏WinForm,然后在函数退出后再次显示它,但它不起作用。我尝试了一个版本的代码,其中我使用System.Diagnostics.Process.Start()在将文件保存到磁盘后尝试打开该文件,虽然它不会从加载项引发异常,但Outlook会向用户提示一个消息框,其中包含来自ComException的相同消息。我甚至尝试创建一个字段来查看是否应该起草doc notice电子邮件,并认为在一个form.close()调用后,代码会处理这个问题,认为close调用至少会处理锁定Outlook的对话框,但我仍然得到了相同的异常


有没有办法实现我想要的?有人有什么建议吗?我现在有点不知所措,如果有人能在这个问题上提供帮助/建议,我将不胜感激。如果这是一个重复的问题,我诚挚的道歉-我找不到一个好的答案。提前感谢您抽出时间

首先,为什么不以无模式显示您自己的表单

其次(这一点非常重要)不要使用如下代码

this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
this.DNoticeItem.HTMLBody+=”
客户端:“+clientInfo;
每次运行这样的行时,您都会检索HTMLBody,向其中添加一些内容(使HTML格式错误),然后再次设置HTMLBody并强制Outlook理解(格式错误的)HTML。这(假设Outlook可以解析和修复您的HTML)将导致返回的HTML与您设置的HTML不同


使用常规字符串构建HTML正文一次,只设置HTMLBody属性一次。

请原谅我的无知-您能给我一个无模式显示表单的示例吗?我不熟悉这个词。谢谢关于HTML的提示,我不知道re:HTMLBody。我将构造,然后根据您的建议应用一次。Prompt Prompt=new Prompt(mail,OutlookItemType.Sent);prompt.ShowDialog();将ShowDialog替换为Show()。我刚刚尝试过,但仍然得到异常。而且,奇怪的是,tab按钮不再从一个控件切换到另一个控件。在任何字段中按tab键保存多行文本框时,windows会发出嘟嘟声。切换回ShowDialog至少可以恢复选项卡功能…您的表单是否作为相应Outlook窗口的子窗口创建?