简体   繁体   English

C#VSTO Outlook外发邮件超链接

[英]C# VSTO Outlook Outgoing Message Hyperlink

I am creating a C# VSTO add-in for Outlook 2010. I am attempting to generate a hyperlink at the insertion point of the active outgoing message that is being worked on (the hyperlink is inserted via a button on the message window ribbon). 我正在为Outlook 2010创建C#VSTO加载项。我正在尝试在正在处理的活动传出消息的插入点生成超链接(通过消息窗口功能区上的按钮插入超链接)。 All other functions of the add-in (ribbon button, accessing the ActiveInspector().CurrentItem , etc.) work fine. 加载项的所有其他功能(功能区按钮,访问ActiveInspector().CurrentItem等)都可以正常工作。 I am working with this code: 我正在使用此代码:

object linktext = txtDisplayText.Text;
object result = "MY URL";
object missObj = Type.Missing;

Outlook.MailItem currentMessage = 
     Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
Word.Document doc = currentMessage.GetInspector.WordEditor;
object oRange = doc.Windows[1].Selection;
doc.Application.Selection.Hyperlinks.Add
    (oRange, ref result, ref missObj, ref missObj, ref linktext, ref missObj);

When I run this code I get the message "Command failed." 当我运行此代码时,我收到消息“命令失败”。 I suspect I am missing something either in regards to how Outlook uses Microsoft Word's editor for Outlook messages or in the way I have specified the selection object in oRange . 我怀疑我在Outlook如何使用Microsoft Word的Outlook消息编辑器或在oRange指定选择对象的方式方面缺少某些东西。 Any help is very much appreciated. 很感谢任何形式的帮助。

This issue was indeed caused by the way the selection was defined for the Hyperlinks.Add command. 实际上,此问题是由为Hyperlinks.Add命令定义选择的方式引起的。 Instead of an object type, the selection needed to be typed as a Microsoft Word Selection (due to the fact that Outlook uses Word as its editor): 代替对象类型,该选择需要键入为Microsoft Word选择(由于Outlook使用Word作为其编辑器):

Word.Selection objSel = doc.Windows[1].Selection;

So to insert a hyperlink at the insertion point of an Outlook message during composition, the code has using statements for both Word and Outlook: 因此,要在撰写过程中在Outlook消息的插入点插入超链接,该代码对Word和Outlook都具有using语句:

using Outlook = Microsoft.Office.Interop.Outlook;
using Word = Microsoft.Office.Interop.Word;

And then this code: 然后这段代码:

object linktext = txtDisplayText.Text;
object result = "MY URL";
object missObj = Type.Missing;

Outlook.MailItem currentMessage = 
     Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
Word.Document doc = currentMessage.GetInspector.WordEditor;
Word.Selection objSel = doc.Windows[1].Selection;
doc.Hyperlinks.Add
     (objSel.Range, ref result, ref missObj, ref missObj, ref linktext, ref missObj);

Two other adjustments are worth noting. 另外两个调整值得注意。 Because a Word.Selection type was used for the anchor of the hyperlink, the Hyperlinks.Add command needed to be changed from doc.Application.Selection.Hyperlinks.Add to doc.Hyperlinks.Add . 由于Word.Selection类型用于超链接的锚点,因此需要将Hyperlinks.Add命令从doc.Application.Selection.Hyperlinks.Add更改为doc.Hyperlinks.Add And because Outlook uses Microsoft's Word editor, the anchor for doc.Hyperlinks.Add used a range: objSel.Range . 而且由于Outlook使用Microsoft的Word编辑器,因此doc.Hyperlinks.Add的锚点使用了一个范围: objSel.Range

Use the HTMLBody property of the MailItem class for modifying the message body (inserting a hyperlink) in the ItemSend event handler instead. 使用MailItem类的HTMLBody属性来在ItemSend事件处理程序中修改消息正文(插入超链接)。 You need to find the place where to paste a hyperlink <a href=.../> , modify the HTML well formed string and assign it back. 您需要找到粘贴超链接<a href=.../> ,修改HTML格式正确的字符串并将其分配回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM