2017-05-03 108 views
0

我正在创建一个Outlook Add In,它具有子窗体。表单上有一个按钮,如果用户点击它,我想通过它来生成一个mailitem。我想自动填充电子邮件中的一些信息,然后留给用户在闲暇时发送。在WinForms中使用MailItem.Display(true | false)异常

我的代码如下所示:

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功能异常,不管我用真或假(做了一点研究表示,这决定了当mailitem打开时用户是否可以访问主Outlook窗口)。例外情况是COM异常“对话框已打开,请关闭并重试”。我试图在创建邮件项目的函数调用之前隐藏WinForm,然后在函数退出后再次显示它,但它不起作用。我已经尝试了一个代码版本,我使用System.Diagnostics.Process.Start()在将文件保存到磁盘后尝试打开该文件,并且它没有从添加中触发异常,但Outlook会提示用户与ComException中的相同消息的消息框。我甚至尝试创建一个字段,以查看是否应该起草文档通知电子邮件,并且在form.close()调用后认为代码处理该操作,并认为close调用至少会处理出现的对话框锁定Outlook,我仍然有同样的例外。

有没有办法实现我想要的?有没有人有什么建议?我现在有点困难,并希望任何人在这个问题上提供任何帮助/指示/建议。如果这是一个重复的问题,我诚挚的道歉 - 我找不到问题的好答案。预先感谢您的时间。

回答

0

首先,为什么不能无模式地显示yoru自己的窗体?

其次(这是非常重要的),不喜欢以下

this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo; 

你运行像一条线每次使用代码,您检索HTMLBody,添加一些东西给它(使HTML格式不正确),然后再次设置HTMLBody并强制Outlook来理解您的(格式错误的)HTML。这(假设Outlook可以解析并修复您的HTML)将导致HTML返回与您设置的不同。

使用常规字符串构建一次HTML主体,并仅设置一次HTMLBody属性。

+0

原谅我的无知 - 你能给我一个无模式显示表单的例子吗?我对这个词不熟悉。感谢关于HTML的提示,不知道是否:HTMLBody。我会构建,然后按照您的建议申请一次。 –

+0

显示表单的代码是什么? –

+0

提示提示符=新提示(mail,OutlookItemType.Sent); prompt.ShowDialog(); –