【问题标题】:Linking a string in Word VSTO addin Word.interop在 Word VSTO 插件 Word.interop 中链接字符串
【发布时间】: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


    【解决方案1】:

    要找到这样的模式,有必要使用 Word 的通配符搜索功能(有点像 RegEx)。以下找到问题中发布的示例

    <[0-9]-[0-9]{1;}-[0-9]{1;}/[0-9]{1;}>
    

    单词的开头 | 0 - 9 范围内任意字符的一个实例 | - 字符 | 0 - 9 范围内的任意数量的字符 | - 字符 | 0 - 9 范围内的任意数量的字符 |斜线/ | 0 - 9 范围内的任意数量的字符 |词尾

    您可能需要进行进一步测试,如果您知道任何一组数字的出现次数有限,请设置一个上限(在; 之后和右大括号} 之前。

    另请注意,根据 Windows 中的区域设置,您可能需要 , 代替大括号中的 ;

    我已修改问题中的代码以使用通配符。由于调用过程需要处理结果范围和文本,我更改了签名以返回Word.Range。它还接受Word.Range 参数并执行Range.Find 而不是Selection.Find,因为它更可靠、更准确。

    如果Find 成功,Range 将更改为“找到”术语。 Execute 返回一个布尔值,表示成功或失败。对此进行检查并在失败时返回null

    我没有尝试创建您指定的超链接,因为这不是问题的关键(每个问题只有一个问题!),但我已经说明了如何使用返回的 Range 来生成它。

    请注意,我确实测试了通配符搜索字符串,但由于我没有时间为它设置项目,因此在没有测试的情况下调整了代码。所以你可能会在这里或那里遇到语法错误...

    using Word = Microsoft.Office.Interop.Word;
    
    private Word.Range FindAndReplace(Word.Range rngToSearch, object findText, object replaceWithText)
            {
                bool found = false;
                //options
                object matchCase = false;
                object matchWholeWord = true;
                object matchWildCards = true;
                object matchSoundsLike = false;
                object matchAllWordForms = false;
                object forward = true;
                object format = false;
                object matchKashida = false;                     
                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
                found = rngToSearch.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);
              if (!found)
              {
                rngToSearch = null;
              }
    
              return rngToSearch;
            }
    
            private void Button3_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                Word.Range rng = doc.Content;
                string searchTerm = @"<[0-9]-[0-9]{1;}-[0-9]{1;}/[0-9]{1;}>";
                string hyperlink = "";  //put your hyperlink stuff here
    
                foreach (Word.Paragraph paragraph in doc.Paragraphs)
                {
                    Word.Range rngFound = FindAndReplace(rng, searchTerm, ""); //searching and wrapping.
    
                    if (rngFound != null)
                    {
                    Word.Hyperlink hp = (Word.Hyperlink)
                        rngFound.Hyperlinks.Add(rngFound, hyperlink + rngFound.Text);
                    }
                }
     }
    

    【讨论】:

    • 谢谢。这是工作。但它只适用于第一个字符串。 foreach(doc.Paragraphs 中的 Word.Paragraph 段落)不起作用。我不知道为什么。
    • 尝试将FindAndReplace(...中的rng替换为paragraph.Range
    • 谢谢,我试过了,但没用。但我做到了:foreach (Word.Range docRange in doc.Words) {Word.Range rngFound = FindAndReplace(docRange, searchTerm, "") 它需要一个个单词。我想这不是很好,而且会很慢。
    • 我也尝试过改变这样的功能:private Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText) 并使用选择:Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping. Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range; currentRange.Hyperlinks.Add(currentRange, hyperlink + rngFound.Text); 但它一键完成一个单词。
    • Vlady,你能把这个作为一个单独的问题发布吗?如果它可以通过我的建议简单地修复,好的。但如果需要对其进行研究和测试,那么提出一个新问题会更好。
    猜你喜欢
    • 1970-01-01
    • 2015-04-13
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多