次の問題があります。ウィンドウが開き、いくつかのファイルを選択できます。次に、そのウィンドウを右クリックして、選択したファイルのパスを新しいメール ダイアログに添付することを選択できます。
ワークフローは次のようになります。
ウィンドウを開き、いくつかのファイルを選択します
右クリックして、選択したファイルのパスを MailItem に追加することを選択します
ロジックは、
ActiveInspector
3.1. 存在する場合は、それを取得します
CurrentItem as MailItem
(したがって、新しいメール ダイアログが存在し、作成する必要はありません)3.2. 何もない場合は、
CreateItem(Microsoft.Office.Interop.OLItemType.olMailItem)
新しいメール ダイアログを作成するために呼び出しMailItem.Display(false)
てから、メール アイテム ダイアログを表示するために呼び出します次に、選択したファイル パスのリストをループして、それらを新しいメール ダイアログに追加します。これはうまくいきます。
問題ウィンドウを 2 回目に開いてさらにファイルを選択し、それらのパスを以前に開いた同じメール ダイアログに追加すると、ファイルが追加されません。
コードは次のとおりです。
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);
}
}
}
}
上記のコードは、このメソッドを呼び出して、現在の にパスを追加しますActiveInspector
WordEditor
。
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(" ");
}