我有一个 word 2007 .doc
文件,其中包含多个按部分分隔的子文档。
有没有办法从文档中删除所有分节符?
我尝试查找并替换它们,但收到错误消息。
private void RemoveAllSectionBreaks(Word.Document doc)
{
Word.Find find = doc.Range(ref oMissing, ref oMissing).Find;
find.ClearFormatting();
//find.Text = "^b"; // This line throws an error
find.Text =((char)12).ToString(); // Same error when attempting it this way
find.Replacement.ClearFormatting();
find.Replacement.Text = "";
find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, Word.WdReplace.wdReplaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
find.Text
行会产生一个错误-
SEHException was unhandled by user code
External component has thrown an exception.
我没有得到关于错误可能是什么的任何进一步细节。该代码在 Word 2003 中运行良好,但我需要它在 Word 2007 中运行。
我是否遵循了 word 2007 的正确方法?
最佳答案
我最终采用了不同的方法。由于单词查找功能导致错误,我决定编写搜索/删除代码。以下代码删除它遇到的所有分节符。
private void RemoveAllSectionBreaks(Word.Document doc)
{
Word.Sections sections = doc.Sections;
foreach (Word.Section section in sections)
{
section.Range.Select();
Word.Selection selection = doc.Application.Selection;
object unit = Word.WdUnits.wdCharacter;
object count = 1;
object extend = Word.WdMovementType.wdExtend;
selection.MoveRight(ref unit, ref count, ref oMissing);
selection.MoveLeft(ref unit, ref count, ref extend);
selection.Delete(ref unit, ref count);
}
}
关于c# - Word 2007 删除分节符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955478/