【发布时间】:2020-02-06 21:02:53
【问题描述】:
我有一个带有文本和数字的 word 文档。数字是这样写的:
2-16-9035/88、2-16-8344/41、5-17-43/12、2-15-5027/137
等等。他们之间可能有文字。喜欢:
“在 2-16-8344/4 中有一个 2-16-9035/88 案例”。
我需要在数字下方放置一个链接。 我有一个代码:
private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
{
//options
object matchCase = false;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false; //Function for searching and wrapping the text. It can also replace the wraped text.
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = false;
object wrap = 1;
//execute find and replace
doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}
private void Button3_Click(object sender, RibbonControlEventArgs e)
{
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Application app = Globals.ThisAddIn.Application;
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
FindAndReplace(app, "Google", ""); //searching and wrapping.
Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
Microsoft.Office.Interop.Word.Hyperlink hp = (Microsoft.Office.Interop.Word.Hyperlink)
currentRange.Hyperlinks.Add(currentRange, "www.google.com");
}
}
现在我可以搜索一个单词“Google”来包装它并放置一个链接。
也许你会知道我如何搜索 thouse 号码。如您所见,它们的长度可能不同。我需要的是找到一个数字,获取一个字符串(找到的数字)并添加超链接:https://www.****asjaNr=$"{NumberWhatIsFound}"
【问题讨论】:
标签: c# hyperlink ms-word vsto office-interop