在TypeText
上调用Selection
抛出“此命令不可用。”例外
以下是我的代码
public void AddFilePaths(List<string> urls)
{
if (urls.Count > 0)
{
MailItem mi = null;
bool newMailItem = false;
mi = MyAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mi.Body = "New email body";
newMailItem = true;
mi.Display();
inspector = MyAddIn.Application.ActiveInspector();
if (mi != null)
{
foreach (var url in urls)
{
AddPathToActiveInspector(urls);
}
}
}
}
public void AddLinkToCurrentInspector(string url)
{
var inspector = MyAddIn.Application.ActiveInspector();
var currMessage = inspector.CurrentItem;
Microsoft.Office.Interop.Word.Document word = currMessage.GetInspector.WordEditor;
dynamic wordapp = word.Application;
const string text = "\n"; // thisfor some reason will not add new line
Microsoft.Office.Interop.Word.Selection sel = word.Windows[1].Selection;
sel.TypeText(text); // this often errors
string address = url;
string subAddress = "";
string screenTip = "";
string textToDisplay = url;
wordapp.ActiveDocument.Hyperlinks.Add(sel.Range, ref address, ref subAddress, ref screenTip, ref textToDisplay);
if (word.ProtectionType != Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection) word.Unprotect();
sel.TypeText(" ");
}
答案 0 :(得分:1)
您无需致电TypeText
- 只需设置Text属性:
Application.ActiveInspector.WordEditor.Application.Selection.Text = "test"
答案 1 :(得分:0)
根据这个问题的答案,我也解决了这个问题。下面的代码比上面的代码更好,更容易理解。非常感谢@joeshwa:
public void AddLinkToCurrentInspector(string url)
{
object link = url;
object result = "url";
object missing = Type.Missing;
var inspector = MyAddIn.Application.ActiveInspector();
MailItem currMessage = inspector.CurrentItem;
Microsoft.Office.Interop.Word.Document doc = currMessage.GetInspector.WordEditor;
Microsoft.Office.Interop.Word.Selection sel = doc.Windows[1].Selection;
doc.Hyperlinks.Add(sel.Range, ref result, ref missing, ref missing, ref link, ref missing);
sel.EndKey(Microsoft.Office.Interop.Word.WdUnits.wdLine);
sel.InsertAfter("\n");
sel.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdLine);
}