在 Word VSTO 插件中链接字符串 Word.interop 2
Linking a string in Word VSTO addin Word.interop 2
在代码中,我正在搜索字符串并将 link 放在该字符串下方。但问题是,它只对段落中的第一个词有效。如何正确执行 foreach 语句?
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);
}
}
}
1 )累了: foreach (Word.Range docRange in doc.Words) {Word.Range rngFound = FindAndReplace(docRange, searchTerm, "")
一个字一个字的。而且速度很慢。
2)还尝试使用选择:
private Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText)
{
-----------------------------------------------------------------------------
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping.
rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + rngFound.Text);
但现在是一键一字。最后我用那个方法解决了它。看看我最后的评论。
3) 我也试过:
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
Word.Range rngFound = FindAndReplace(paragraph.Range, searchTerm, ""); //searching and wrapping.
if (rngFound != null)
{
Word.Hyperlink hp = (Word.Hyperlink)
rngFound.Hyperlinks.Add(rngFound, hyperlink + rngFound.Text);
}
它会处理段落中的第一个单词。
我需要做什么才能 运行 它在我所有的文档范围内。
代码返回 "odd" 结果的原因是 Find.Wrap
参数的 属性 设置。这被设置为 1
,相当于枚举 wdFindcontinue
。这应该 永远不会 在 Word 的 Find
功能的代码中使用,因为它会导致代码继续循环,直到要搜索的内容是 "found"。如果找不到,代码将进入 "infinite loop".
通常,需要的是 0
、wdFindStop
,这意味着搜索从指定搜索的开头 Range
开始,一直持续到该范围的终点,然后停止。
因此更改此行:
object wrap = 1;
至:
object wrap = Word.WdFindWrap.wdFindStop; //or 0
我强烈建议使用完整的枚举而不是 int
等价物,除非有明确的理由不这样做。 (延迟绑定/PInvoke 就是这样一个原因。)如果没有别的,它使代码更容易阅读和理解,正如这个问题所说明的那样。
通过该更正,以下测试代码对我有效。我 post 这是为了显示使用 System.Diagnotics.Debug.Print
进行的故障排除帮助我找到了问题所在:我可以将循环中的每个 paragraph
与 rngFound.Text
返回的内容进行比较。
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)
{
rng = paragraph.Range;
System.Diagnostics.Debug.Print(rng.Text);
Word.Range rngFound = FindAndReplace(rng, searchTerm, ""); //searching and wrapping.
if (rngFound != null)
{
//Word.Hyperlink hp = (Word.Hyperlink)
System.Diagnostics.Debug.Print(rngFound.Text);
}
}
谢谢。最后我有>
Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText) //funcija poiska 4erez range
{
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 = Word.WdFindWrap.wdFindStop;;
//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)
{
int WordsCount = 0;
int counter = 0;
Word.Application app = Globals.ThisAddIn.Application;
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 = "google.com"; //
string s = rng.Text; // Pushing doc text to the string
Regex regex = new Regex(@"\d*-\d*-\d*/\d*");
WordsCount = regex.Matches(s).Count; // Using regex getting count of searchable words
while (WordsCount >= counter ) // knowing count of words we know how much iterations we need to do.
foreach (Word.Section paragraph in doc.Sections)
{
Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping.
if (rngFound != null)
{
rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + rngFound.Text);
}counter++; // counting iterations
}
}
它正在运行。谢谢 Cindy 的好建议。
在代码中,我正在搜索字符串并将 link 放在该字符串下方。但问题是,它只对段落中的第一个词有效。如何正确执行 foreach 语句?
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);
}
}
}
1 )累了: foreach (Word.Range docRange in doc.Words) {Word.Range rngFound = FindAndReplace(docRange, searchTerm, "")
一个字一个字的。而且速度很慢。
2)还尝试使用选择:
private Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText)
{
-----------------------------------------------------------------------------
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping.
rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + rngFound.Text);
但现在是一键一字。最后我用那个方法解决了它。看看我最后的评论。
3) 我也试过:
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
Word.Range rngFound = FindAndReplace(paragraph.Range, searchTerm, ""); //searching and wrapping.
if (rngFound != null)
{
Word.Hyperlink hp = (Word.Hyperlink)
rngFound.Hyperlinks.Add(rngFound, hyperlink + rngFound.Text);
}
它会处理段落中的第一个单词。
我需要做什么才能 运行 它在我所有的文档范围内。
代码返回 "odd" 结果的原因是 Find.Wrap
参数的 属性 设置。这被设置为 1
,相当于枚举 wdFindcontinue
。这应该 永远不会 在 Word 的 Find
功能的代码中使用,因为它会导致代码继续循环,直到要搜索的内容是 "found"。如果找不到,代码将进入 "infinite loop".
通常,需要的是 0
、wdFindStop
,这意味着搜索从指定搜索的开头 Range
开始,一直持续到该范围的终点,然后停止。
因此更改此行:
object wrap = 1;
至:
object wrap = Word.WdFindWrap.wdFindStop; //or 0
我强烈建议使用完整的枚举而不是 int
等价物,除非有明确的理由不这样做。 (延迟绑定/PInvoke 就是这样一个原因。)如果没有别的,它使代码更容易阅读和理解,正如这个问题所说明的那样。
通过该更正,以下测试代码对我有效。我 post 这是为了显示使用 System.Diagnotics.Debug.Print
进行的故障排除帮助我找到了问题所在:我可以将循环中的每个 paragraph
与 rngFound.Text
返回的内容进行比较。
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)
{
rng = paragraph.Range;
System.Diagnostics.Debug.Print(rng.Text);
Word.Range rngFound = FindAndReplace(rng, searchTerm, ""); //searching and wrapping.
if (rngFound != null)
{
//Word.Hyperlink hp = (Word.Hyperlink)
System.Diagnostics.Debug.Print(rngFound.Text);
}
}
谢谢。最后我有>
Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText) //funcija poiska 4erez range
{
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 = Word.WdFindWrap.wdFindStop;;
//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)
{
int WordsCount = 0;
int counter = 0;
Word.Application app = Globals.ThisAddIn.Application;
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 = "google.com"; //
string s = rng.Text; // Pushing doc text to the string
Regex regex = new Regex(@"\d*-\d*-\d*/\d*");
WordsCount = regex.Matches(s).Count; // Using regex getting count of searchable words
while (WordsCount >= counter ) // knowing count of words we know how much iterations we need to do.
foreach (Word.Section paragraph in doc.Sections)
{
Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping.
if (rngFound != null)
{
rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + rngFound.Text);
}counter++; // counting iterations
}
}
它正在运行。谢谢 Cindy 的好建议。