我编写了一个简单的 VSTO 插件,当用户单击功能区栏按钮时,它会在电子邮件中插入超链接。这是一个代码示例:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
var context = e.Control.Context as Inspector;
if (context != null)
{
if (context.IsWordMail())
{
var doc = context.WordEditor as Document;
if (doc != null)
{
var sel = doc.Windows[1].Selection;
doc.Hyperlinks.Add(sel.Range, "http://www.google.com", "", "", "Google", "");
}
}
}
else if (e.Control.Context is Explorer)
{
Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();
if (explorer.Selection.Count == 1)
{
Microsoft.Office.Interop.Outlook.Selection itemSelection = explorer.Selection;
var item = itemSelection[1] as MailItem;
// get the instance of WordEditor in …Run Code Online (Sandbox Code Playgroud)