Word 2007删除分节符

Kam*_*ami 3 c# replace ms-word ms-office

我有一个单词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);
}
Run Code Online (Sandbox Code Playgroud)

find.Text行将生成错误 -

SEHException未被用户代码处理

外部组件引发的异常.

我没有得到关于错误可能是什么的任何进一步细节.代码在word 2003中运行正常,但我需要在Word 2007中使用它.

我是否遵循2007年的正确方法?

Kam*_*ami 5

我最后做了一个不同的方法.由于单词查找功能导致错误,我决定编码搜索/删除.以下代码将删除它遇到的所有分节符.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)