Interop.Word查找和超链接这个词

时间:2015-02-17 07:42:32

标签: c# hyperlink ms-word office-interop

我正在尝试创建一个程序,在Word文档中搜索某个单词并添加一个指向该单词的超链接。

我可以使用代码来搜索单词(使用FindandReplace),并且可以找出如何添加到单词doc的超链接,但我无法弄清楚如何超链接我搜索的单词。我认为是因为我需要将我搜索的单词定义为范围。

到目前为止我的代码是:

using System;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Color = Microsoft.Office.Interop.Word.WdColor;

...

private void button2_Click(object sender, EventArgs e)
    {
// open word doc  
object fileName = openFileDialog1.FileName;
object readOnly = false;
object isVisible = true;
var applicationWord = new Microsoft.Office.Interop.Word.Application();
applicationWord.Visible = true;
Microsoft.Office.Interop.Word.Document doc =     applicationWord.Documents.Open(openFileDialog1.FileName, ReadOnly: false, Visible: true);
//using FindAndReplace
        Word.Find fnd = applicationWord.ActiveWindow.Selection.Find;
        fnd.ClearFormatting();
        fnd.Replacement.ClearFormatting();
        fnd.Forward = true;
        fnd.Wrap = Word.WdFindWrap.wdFindContinue;
        fnd.MatchCase = true;
        fnd.MatchWholeWord = true;
        fnd.Text = "Qwe";
        fnd.Replacement.Text = "Qwe";
        fnd.Execute(Replace: Word.WdReplace.wdReplaceAll);

        Word.Hyperlinks hyp = doc.Hyperlinks;
        object myRange = applicationWord.Selection.Range;
        object linkAddr = textBox1.Text;
        object linkSubAddr = textBox2.Text;
        Microsoft.Office.Interop.Word.Hyperlink myLink = hyp.Add(myRange, ref linkAddr, ref linkSubAddr);
    }

我无法将超链接应用于我搜索的Word

0 个答案:

没有答案