我有问题用word interop删除一些特定的Linebreaks。似乎doc.Content.Text的索引与单词的索引不匹配。
我需要删除所有没有点作为前一个字符的中断。
使用小文档可以工作,但只有大约3-5次中断后,所选范围不是分页符,任何人都知道如何删除没有range.select的这些中断(start:start,end:end)或者知道问题?
例如我有以下文字:
调查了34万人的美国调查人员发现星期一情绪并没有比其他工作日更糟糕,星期五吧。(休息)
人们在周末接近时更加快乐,支持((打破删除))
的概念“周五的感觉”。((休息))
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
word.Visible = true;
string document = @"C:\bin\Debug\123.doc";
if (document != null)
{
Document doc = word.Documents.Open(document, ReadOnly: false, Visible: true);
doc.Activate();
string text = doc.Content.Text;
int index = text.IndexOf("\r");
int deletetCount = 0;
while(index != -1)
{
if (index != 0 && text[index - 1] != '.')
{
int start = index + 1 - deletetCount;
int end = start + 1;
if (start >= 0 && end >= 0 && end > start)
{
Range range = doc.Range(Start: start, End: end);
range.Select();
range.Delete();
deletetCount++;
}
}
index = text.IndexOf("\r", index + 1);
}
}
}
}
答案 0 :(得分:0)
现在我使用Search&替换为通配符(Regex)
删除休息的规则:
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
word.Visible = true;
string document = @"C:\bin\Debug\1.doc";
if (document != null)
{
Document doc = word.Documents.Open(document, ReadOnly: false, Visible: true);
doc.Activate();
object missingObject = null;
doc.ConvertNumbersToText();
object item = WdGoToItem.wdGoToPage;
object whichItem = WdGoToDirection.wdGoToFirst;
object replaceAll = WdReplace.wdReplaceAll;
object forward = true;
object matchWholeWord = false;
object matchWildcards = true;
object matchSoundsLike = false;
object matchAllWordForms = false;
object wrap = WdFindWrap.wdFindAsk;
object format = true;
object matchCase = false;
object originalText = "([!.:])^13(?[!.])";
object replaceText = @"\1 \2";
doc.GoTo(ref item, ref whichItem, ref missingObject, ref missingObject);
foreach (Range rng in doc.StoryRanges)
{
rng.Find.Font.Bold = 0;
rng.Find.Execute(ref originalText, ref matchCase,
ref matchWholeWord, ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms, ref forward,
ref wrap, ref format, ref replaceText, ref replaceAll, ref missingObject,
ref missingObject, ref missingObject, ref missingObject);
}
}
}
}