C# 将Word中的字符串链接到addin Word.interop 2
在代码中,我搜索字符串并将链接放在该字符串下面。但问题是,它只适用于段落中的第一个单词。我怎样才能正确地表达每一句话C# 将Word中的字符串链接到addin Word.interop 2,c#,ms-word,office-interop,C#,Ms Word,Office Interop,在代码中,我搜索字符串并将链接放在该字符串下面。但问题是,它只适用于段落中的第一个单词。我怎样才能正确地表达每一句话 using Word = Microsoft.Office.Interop.Word; private Word.Range FindAndReplace(Word.Range rngToSearch, object findText, object replaceWithText) { bool found = false;
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);
}
}
}
但现在它只需点击一下就可以完成一个单词。最后我用这种方法解决了这个问题。看看我最后的评论
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);
}
它用段落中的第一个单词来完成工作
我需要做什么才能在我的所有文档范围内运行它 代码返回“奇数”结果的原因是
Find.Wrap
参数的属性设置。这被设置为1
,这相当于枚举wdFindcontinue
。在Word的Find功能的代码中不应使用此选项,因为它会导致代码继续循环,直到搜索的内容被“找到”。如果找不到,代码将进入“无限循环”
通常,需要的是0
,wdFindStop
,这意味着搜索从指定搜索范围的开始处开始,然后继续到该范围的终点,然后停止
因此,改变这一行:
object wrap = 1;
致:
我强烈建议使用完整枚举而不是int
等效枚举,除非有明确的理由不这样做。(后期绑定/PInvoke就是这样一个原因。)如果没有其他原因,它会使代码更易于阅读和理解,正如这个问题所说明的
有了这个更正,下面的测试代码对我来说很有用。我发布这篇文章是为了展示使用System.diagnostics.Debug.Print解决问题的方法:我可以将循环中的每个段落与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.Document doc=Globals.ThisAddIn.Application.ActiveDocument;
Word.Range rng=单据内容;
字符串searchTerm=@;
字符串超链接=”//把你的超链接放在这里
foreach(文件段落中的文字段落)
{
rng=段落范围;
系统、诊断、调试、打印(rng.Text);
Word.Range rngFound=FindAndReplace(rng,searchTerm,“”;//搜索和换行。
if(rngFound!=null)
{
//Word.Hyperlink hp=(Word.Hyperlink)
系统、诊断、调试、打印(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
}
}
Word.Selection FindAndReplace(Word.Selection rngToSearch、object findText、object replaceWithText)//funcija poiska 4erez范围
{
bool-found=false;
//选择权
objectmatchcase=false;
对象匹配wholeword=true;
对象匹配通配符=true;
对象匹配Soundslike=false;
对象matchAllWordForms=false;
对象向前=真;
对象格式=false;
对象matchKashida=false;
对象匹配DicalCritics=false;
对象匹配alefhamza=false;
对象匹配控件=false;
对象只读=假;
对象可见=真实;
对象替换=false;
对象换行=Word.WdFindWrap.wdFindStop;;
//执行查找和替换
Find=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);
如果(!找到)
{
rngToSearch=null;
}
返回rngToSearch;
}
私有无效按钮3\u单击(对象发送者,RibbonControlEventArgs e)
{
int-wordscont=0;
int计数器=0;
Word.Application app=Globals.ThisAddIn.Application;
Word.Document doc=Globals.ThisAddIn.Application.ActiveDocument;
Word.Range rng=单据内容;
字符串searchTerm=@;
string hyperlink=“google.com”//
string s=rng.Text;//将文档文本推送到字符串中
正则表达式正则表达式=新正则表达式(@“\d*-\d*-\d*/\d*”;
WordScont=regex.Matches(s).Count;//使用regex获取可搜索单词的计数
while(wordscont>=counter)//知道字数,我们就知道需要进行多少次迭代。
foreach(文件章节中的Word.Section段落)
{
Word.Selection rngFound=findandplace(app.Selection,searchTerm,“”;//搜索和包装。
if(rngFound!=null)
{
添加(rngFound.Range,hyperlink+rngFound.Text);
}计数器+++;//计算迭代次数
}
}
它正在发挥作用。谢谢你的忠告。嘿,辛迪。我试图改变你建议的陈述。但结果是一样的。如果我在文档中有示例2-16-9035/88、2-16-8344/41、5-17-43/12、2-15-5027/137。第一个2-16-9035/88将被包装,sikcle将停止。我目前在移动设备上做不了太多,但我昨天使用第一个问题中给出的样本进行了彻底的测试。注意,这只捕获第一个实例/段落,因为这是隐含的要求,循环段落。如果你需要别的东西,你必须非常具体。
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
}
}