C# 如何从word文档中获取与通配符匹配的所有字符串
我正在尝试编写一个应用程序来搜索word文档中所有出现的,其中一些文本是<和>之间的任何字符串。当我找到每个匹配项时,我想存储/显示/处理每个匹配项 以下是我目前掌握的情况:C# 如何从word文档中获取与通配符匹配的所有字符串,c#,.net,ms-word,find,office-interop,C#,.net,Ms Word,Find,Office Interop,我正在尝试编写一个应用程序来搜索word文档中所有出现的,其中一些文本是之间的任何字符串。当我找到每个匹配项时,我想存储/显示/处理每个匹配项 以下是我目前掌握的情况: Word._Application word = new Word.Application(); Word.Documents d = word.Documents; Word._Document doc; doc = d.Open(strFileName); doc.Activate(); foreach (Word.Ra
Word._Application word = new Word.Application();
Word.Documents d = word.Documents;
Word._Document doc;
doc = d.Open(strFileName);
doc.Activate();
foreach (Word.Range myStoryRange in doc.StoryRanges)
{
myStoryRange.Find.MatchWildcards = true;
myStoryRange.Find.Text = "[<]*[>]";
myStoryRange.Find.Execute();
// Somehow get the result string that matched the wildcard
}
Word.\u Application Word=new Word.Application();
Word.Documents d=Word.Documents;
Word.\u文件文档;
doc=d.Open(strFileName);
doc.Activate();
foreach(文档StoryRanges中的Word.Range myStoryRange)
{
myStoryRange.Find.MatchWildcards=true;
myStoryRange.Find.Text=“[]”;
myStoryRange.Find.Execute();
//以某种方式获得匹配通配符的结果字符串
}
结果是为每个找到的字符串重新定义了范围。您可以通过以下方式访问找到的每个文本:
rng.Text
您可以在更大范围内获得找到的文本字符位置:
rng.Start
rng.End
因此,我可以通过在Find循环中声明一个仅包含找到的字符串的本地范围来实现这一点。我将用DocProperty替换每个文本,但您可以使用它执行任何操作:
Word.Range rng = this.Content;
rng.Find.MatchWildcards = true;
rng.Find.Text = "[<]*[>]";
while (rng.Find.Execute())
{
// create a local Range containing only a single found string
object cstart = rng.Start;
object cend = rng.End;
Word.Range localrng = this.Range(ref cstart, ref cend);
// replace the text with a custom DocProperty
Word.Field newfld = localrng.Fields.Add(localrng, Word.WdFieldType.wdFieldDocProperty, "MyDocProp", false);
localrng.Fields.Update();
}
Word.Range rng=this.Content;
rng.Find.MatchWildcards=true;
rng.Find.Text=“[]”;
while(rng.Find.Execute())
{
//创建仅包含单个已找到字符串的本地范围
对象cstart=rng.Start;
对象cend=rng.End;
Word.Range localrng=此.Range(参考cstart,参考cend);
//用自定义DocProperty替换文本
Word.Field newfld=localrng.Fields.Add(localrng,Word.WdFieldType.wdFieldDocProperty,“MyDocProp”,false);
localrng.Fields.Update();
}
结果是为每个找到的字符串重新定义了范围。您可以通过以下方式访问找到的每个文本:
rng.Text
您可以在更大范围内获得找到的文本字符位置:
rng.Start
rng.End
因此,我可以通过在Find循环中声明一个仅包含找到的字符串的本地范围来实现这一点。我将用DocProperty替换每个文本,但您可以使用它执行任何操作:
Word.Range rng = this.Content;
rng.Find.MatchWildcards = true;
rng.Find.Text = "[<]*[>]";
while (rng.Find.Execute())
{
// create a local Range containing only a single found string
object cstart = rng.Start;
object cend = rng.End;
Word.Range localrng = this.Range(ref cstart, ref cend);
// replace the text with a custom DocProperty
Word.Field newfld = localrng.Fields.Add(localrng, Word.WdFieldType.wdFieldDocProperty, "MyDocProp", false);
localrng.Fields.Update();
}
Word.Range rng=this.Content;
rng.Find.MatchWildcards=true;
rng.Find.Text=“[]”;
while(rng.Find.Execute())
{
//创建仅包含单个已找到字符串的本地范围
对象cstart=rng.Start;
对象cend=rng.End;
Word.Range localrng=此.Range(参考cstart,参考cend);
//用自定义DocProperty替换文本
Word.Field newfld=localrng.Fields.Add(localrng,Word.WdFieldType.wdFieldDocProperty,“MyDocProp”,false);
localrng.Fields.Update();
}
结果是为每个找到的字符串重新定义了范围。您可以通过以下方式访问找到的每个文本:
rng.Text
您可以在更大范围内获得找到的文本字符位置:
rng.Start
rng.End
因此,我可以通过在Find循环中声明一个仅包含找到的字符串的本地范围来实现这一点。我将用DocProperty替换每个文本,但您可以使用它执行任何操作:
Word.Range rng = this.Content;
rng.Find.MatchWildcards = true;
rng.Find.Text = "[<]*[>]";
while (rng.Find.Execute())
{
// create a local Range containing only a single found string
object cstart = rng.Start;
object cend = rng.End;
Word.Range localrng = this.Range(ref cstart, ref cend);
// replace the text with a custom DocProperty
Word.Field newfld = localrng.Fields.Add(localrng, Word.WdFieldType.wdFieldDocProperty, "MyDocProp", false);
localrng.Fields.Update();
}
Word.Range rng=this.Content;
rng.Find.MatchWildcards=true;
rng.Find.Text=“[]”;
while(rng.Find.Execute())
{
//创建仅包含单个已找到字符串的本地范围
对象cstart=rng.Start;
对象cend=rng.End;
Word.Range localrng=此.Range(参考cstart,参考cend);
//用自定义DocProperty替换文本
Word.Field newfld=localrng.Fields.Add(localrng,Word.WdFieldType.wdFieldDocProperty,“MyDocProp”,false);
localrng.Fields.Update();
}
结果是为每个找到的字符串重新定义了范围。您可以通过以下方式访问找到的每个文本:
rng.Text
您可以在更大范围内获得找到的文本字符位置:
rng.Start
rng.End
因此,我可以通过在Find循环中声明一个仅包含找到的字符串的本地范围来实现这一点。我将用DocProperty替换每个文本,但您可以使用它执行任何操作:
Word.Range rng = this.Content;
rng.Find.MatchWildcards = true;
rng.Find.Text = "[<]*[>]";
while (rng.Find.Execute())
{
// create a local Range containing only a single found string
object cstart = rng.Start;
object cend = rng.End;
Word.Range localrng = this.Range(ref cstart, ref cend);
// replace the text with a custom DocProperty
Word.Field newfld = localrng.Fields.Add(localrng, Word.WdFieldType.wdFieldDocProperty, "MyDocProp", false);
localrng.Fields.Update();
}
Word.Range rng=this.Content;
rng.Find.MatchWildcards=true;
rng.Find.Text=“[]”;
while(rng.Find.Execute())
{
//创建仅包含单个已找到字符串的本地范围
对象cstart=rng.Start;
对象cend=rng.End;
Word.Range localrng=此.Range(参考cstart,参考cend);
//用自定义DocProperty替换文本
Word.Field newfld=localrng.Fields.Add(localrng,Word.WdFieldType.wdFieldDocProperty,“MyDocProp”,false);
localrng.Fields.Update();
}
您不能使用正则表达式吗?难道你不能访问Word文档的纯文本吗?你的回复中有没有隐藏的答案?你基本上是在问我刚才的问题。我不知道如何访问Word文档的纯文本。不,没有答案。伪装:>我只是认为它更有效,更容易使用正则表达式。但是如果你不能访问纯文本,那就不同了。你不能使用正则表达式吗?难道你不能访问Word文档的纯文本吗?你的回复中有没有隐藏的答案?你基本上是在问我刚才的问题。我不知道如何访问Word文档的纯文本。不,没有答案。伪装:>我只是认为它更有效,更容易使用正则表达式。但是如果你不能访问纯文本,那就不同了。你不能使用正则表达式吗?难道你不能访问Word文档的纯文本吗?你的回复中有没有隐藏的答案?你基本上是在问我刚才的问题。我不知道如何访问Word文档的纯文本。不,没有答案。伪装:>我只是认为它更有效,更容易使用正则表达式。但是如果你不能访问纯文本,那就不同了。你不能使用正则表达式吗?难道你不能访问Word文档的纯文本吗?你的回复中有没有隐藏的答案