2016-08-12 86 views
0

我有以下問題。我有一個打開的窗口,允許我選擇一些文件。 然後,我可以右鍵單擊該窗口並選擇將所選文件的路徑附加到新的郵件對話框。Outlook VSTO - 從Outlook添加文件路徑/超鏈接到MailItem WordEditor VSTO

的工作流程是這樣的:

  1. 我打開窗戶,並選擇幾個文件,

  2. 單擊鼠標右鍵,選擇添加選定的文件路徑的MailItem

  3. 邏輯將檢查是否存在ActiveInspector

    3.1。如果有的話,我得到它的CurrentItem as MailItem(所以,新的郵件對話框存在並且不需要被創建)

    3.2。如果沒有,我叫CreateItem(Microsoft.Office.Interop.OLItemType.olMailItem)創建 新郵件對話框,然後我打電話MailItem.Display(false)顯示 郵件項目對話框

  4. 通過選擇文件的路徑列表接下來,我循環,並將其添加到新的郵件對話框。這很好。

問題如果我打開我的第二次選擇多個文件,其路徑添加到同一個郵件對話我剛纔打開的窗口中,它們不會添加。

下面是代碼:

public void AddFilePaths(List<string> paths) 
{ 
    if (paths.Count > 0) 
    { 
     var inspector = MyAddIn.Application.ActiveInspector(); 
     MailItem mi = null; 
     bool newMailItem = false; 

     if (inspector != null) 
     { 
      // If new mail dialog is already open, just get it. 
      // This is called on my 2nd attempt to add paths to new mail. 
      // This MailItem is the same one created on 1st call in below 
      // else block. I confirmed that by adding some dummy email 
      // Body in below else block, then checking for it here on 
      // 2nd call. I think this proves that correct 
      // Inspector/MailItem is returned here. 
      mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem; 
     } 
     else 
     { 
      // create new mail dialog and display it 
      // this is called on my 1st call to add paths to new mail 
      mi = MyAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 
      mi.Body = "Dummy email body"; 
      newMailItem = true; 
     } 

     if (newMailItem) 
     { 
      mi.Display(); 
      inspector = MyAddIn.Application.ActiveInspector(); 
     } 

     if (inspector != null) 
     { 
      foreach (var path in paths) 
      { 
       AddPathToActiveInspector(path); 
      } 
     } 
    } 
} 

上面的代碼調用此方法來添加路徑添加到當前ActiveInspectorWordEditor

public void AddPathToActiveInspector(string path) 
{ 
    var inspector = MyAddIn.Application.ActiveInspector(); 
    dynamic we = inspector.WordEditor; 
    dynamic word = we.Application; 
    const string nl = "\n"; 

    // I have noticed that if I am debugging, this line will throw error 
    // "COMException was unhandled by user code", "An exception of type 
    // System.Runtime.Interop.Services.COMException occurred in 
    // System.Dynamic.dll but was not handled by user code: 
    // Message: This command is not available 
    // InnerException: null 
    // I have also seen following error on 2nd attempt: "The TypeText  
    // method or property is not available because the document is 
    // locked for editing." 
    word.Selection.TypeText(nl); 

    string address = path; 
    string subAddress = ""; 
    string screenTip = ""; 
    string displayText = path; 
    word.ActiveDocument.Hyperlinks.Add(word.Selection.Range, ref address, ref subAddress, ref screenTip, ref displayText); 
    word.Selection.TypeText(" "); 
} 
+0

那麼,如果你通過你的代碼會發生什麼?它實際上運行?你確定只有一名督察?你如何處理內嵌回覆 –

+0

感謝德米特里,請看我更新的問題。我跟蹤了代碼,並且得到了正確的inspector/MailItem,通過在創建MailItem(第一次調用時)時添加虛擬MailItem.Body消息,然後在第二次調用時爲該文本檢查正文來確認。我在上面的代碼註釋中添加了這一點。我還發現COMException被拋出,但只有在調試時(???)。我也在我的代碼中添加了評論。在我的回覆和轉發部分(內嵌回覆)中,沒有任何內容會被選中,並且包含原始郵件文本(如果這就是您的內嵌回覆的意思。非常感謝 – pixel

回答

1

我會簡單地創建新的電子郵件消息中的每個要添加時間你的路徑,以避免添加路徑錯誤的電子郵件的可能性,如果你已經打開了多個電子郵件的可能性。

  1. 添加參考Microsoft.Office.Interop.Word到項目
  2. 增加您的類文件

這裏的頂部使用Microsoft.Office.Interop.Word是代碼:

public void AddFilePaths(List<string> paths) 
{ 
    if (paths.Count > 0) 
    { 
     MailItem mi = ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 
     mi.Display(); 
     if (mi!= null) 
     { 
      foreach (var path in paths) 
      { 
       AddPathsToNewEmailMessage(path); 
      } 
     } 
    } 
} 

上面的代碼調用此方法添加新電子郵件的路徑WordEditor

public void AddPathsToNewEmailMessage(string path) 
{ 
    object link = url; 
    object result = "url"; 
    object missing = Type.Missing; 
    string nl = "\n"; 

    var inspector = ThisAddIn.Application.ActiveInspector(); 
    MailItem currMessage = inspector.CurrentItem; 
    Word.Document doc = currMessage.GetInspector.WordEditor; 
    Word.Selection sel = doc.Windows[1].Selection; 
    doc.Hyperlinks.Add(sel.Range, ref result, ref missing, ref missing, ref link, ref missing); 
    sel.EndKey(Word.WdUnits.wdLine); 
    sel.InsertAfter(nl); 
    sel.MoveDownWord.WdUnits.wdLine); 
} 
+0

這很好,謝謝! – pixel