问题描述
我有一个MS Word加载项,仅需要根据其格式从一系列文本中提取文本:在我的情况下,特别是如果文本带有下划线或删除线,则下划线或删除线需要找到,以便我可以跟踪它们.
I have a MS Word add-in that needs to extract text from a range of text based solely on its formatting: in my case in particular, if the text is underlined or struck through, the range of characters/words that are underlined or struck through need to be found so that I can keep track of them.
我的第一个想法是使用Range.Find
,此处概述,但是当我不知道我要查找的字符串是什么时,它将无法正常工作
My first idea was to use Range.Find
, as is outlined here, but that won't work when I have no idea what the string is that I'm looking for:
var rng = doc.Range(someStartRange, someEndRange);
rng.Find.Forward = true;
rng.Find.Format = true;
// I removed this line in favor of putting it inside Execute()
//rng.Find.Text = "";
rng.Find.Font.Underline = WdUnderline.wdUnderlineSingle;
// this works
rng.Find.Execute("");
int foundNumber = 0;
while (rng.Find.Found)
{
foundNumber++;
// this needed to be added as well, as per the link above
rng.Find.Execute("");
}
MessageBox.Show("Underlined strings found: " + foundNumber.ToString());
我本人会很高兴地解析文本,但是在仍然了解格式的同时不确定如何进行此操作.预先感谢您的任何想法.
I would happily parse the text myself, but am not sure how to do this while still knowing the formatting. Thanks in advance for any ideas.
我更改了代码以解决find下划线问题,并且进行了更改,而while循环永远不会终止.更具体地说,rng.Find.Found
找到带下划线的文本,但一遍又一遍地找到相同的文本,并且永不终止.
I changed my code to fix the find underline issue, and with that change the while loop never terminates. More specifically, rng.Find.Found
finds the underlined text, but it finds the same text over and over, and never terminates.
一旦我在while循环中添加了额外的Execute()
调用,查找就会根据需要发挥作用.
EDIT 2:Once I added the additional Execute()
call inside the while loop, the find functioned as needed.
推荐答案
您需要
rng.Find.Font.Underline = wdUnderline.wdUnderlineSingle;
(目前,您正在为指定的rng设置格式,而不是为Find设置格式)
(At the moment you are setting the formatting for the specified rng, rather than the formatting for the Find)
这篇关于使用Word互操作查找具有特定格式的文本范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!