2017-05-24 78 views
1

我需要打開多個Outlook窗口,以前填充了ticulo和電子郵件正文以供以後用戶通知發件人。我需要打開幾個窗口(我走一個網格來知道有多少個窗口)。 我試圖用線程做到這一點,但會出現一條錯誤消息:Outlook無法執行此操作,因爲對話框已打開。請關閉它,然後再試一次「 如何打開多個窗口的競爭?打開多個Outlook窗口使用C#發送電子郵件

測試呼叫

private void button2_Click(object sender, EventArgs e) 
{ 
    int qtdEventos = dgvDescEvento.RowCount; 
    Thread[] Threads = new Thread[qtdEventos]; 
    try 
    { 
     cEmail testeEmail = new cEmail(); 
     for (int i = 0; i < qtdEventos; i++) 
     { 
      Threads[i] = new Thread(new ThreadStart(new cEmail().Monta)); 
     } 
     foreach (Thread t in Threads) 
     { 
      t.Start(); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Outlook = Microsoft.Office.Interop.Outlook; 

namespace NavEventos.Class 
{ 
    class cEmail 
    { 
     private Outlook.Application outlookApp; 
     public cEmail() 
     { 
      outlookApp = new Outlook.Application(); 
     } 

     public void Monta() 
     { 
      string pTitulo = "Title"; 
      string pAssunto = "Body test"; 
      Outlook._MailItem oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem); 
      Outlook.Inspector oInspector = oMailItem.GetInspector; 

      Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients; 

      #region MONTA ASSUNTO 
      oMailItem.Subject = pTitulo; 
      #endregion 

      #region MONTA CORPO DO E-MAIL 
      oMailItem.Body = pAssunto; 
      #endregion 

      oMailItem.Display(true); 
     } 
    } 
} 
+0

我解決了這個問題,通過在方法中使用「Lock」並正常調用線程。這一次只能打開一個窗口。 –

回答

0

你可以不喜歡這個...但你不應該嘗試;(

正如你所看到的Outlook COM接口ace正在努力阻止你這樣做,這是展示郵件項目的前景自動化庫的侷限之一是以模態方式完成的。

這樣做有很好的理由,您的用戶在您的LOB應用程序中,然後您的代碼要他們閱讀Outlook中的電子郵件,您已經使用COM自動化庫進行Outlook閱讀。現在,工具欄中的Outlook圖標會閃爍,因爲新的電子郵件模式窗口已打開,但此對話框可能已在您當前的LOB應用程序後面打開。

現在用戶需要將上下文切換到Outlook以查看對話框並閱讀電子郵件。

如果你可以查看你需要打開這些電子郵件都在同一時間,那麼你和Outlook COM自動化會相處得很好:)

否則考慮編寫一個插件Outlook和移動電子郵件管理程序進入前景本身。在那裏,你可以非常有創意,聽起來像你真的只需要一個主界面的細節風格,比如主視圖瀏覽器,所以你有這些電子郵件列表,當用戶點擊它們時,它們顯示在預覽檢查器中。

也許解決方案是使用你的邏輯將這些郵件移動到Outlook中的 特定的文件夾,然後使用Outlook的自動化,使這個 文件夾在Outlook中的當前活動窗口,然後用戶可以決定 哪些電子郵件他們想要行動與否。

+0

感謝您的幫助@Chris。 –

相關問題