3

.docセクションで区切られた複数のサブドキュメントを含むWord2007ファイルがあります。

ドキュメントからすべてのセクション区切りを削除する方法はありますか?

それらを見つけて置き換えようとしましたが、エラーが発生します。

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はユーザーコードによって処理されませんでした

外部コンポーネントが例外をスローしました。

エラーが何であったかについての詳細はわかりません。コードはWord2003で正常に機能していますが、Word2007で機能する必要があります。

私はWord2007の正しいアプローチに従っていますか?

4

2 に答える 2

7

私は別のアプローチをすることになりました。単語検索機能がエラーを引き起こしていたので、検索/削除をコーディングすることにしました。次のコードは、発生したすべてのセクションブレークを削除します。

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);
    }
}
于 2012-12-19T16:45:58.640 に答える
1

カムバックが遅れましたが、私が使用した他の解決策は、vstoを使用して、セクションの区切りを段落に置き換え、セクションに「^ m」、段落に「^ p」を使用するか、空の文字列に置き換えることもできます。

using Word = Microsoft.Office.Interop.Word;

object missing = Missing.Value;
Word.Document tmpDoc = wordApp.Documents.Open(fileToOpen);

object findText = "^m";
object replaceText = "^p^p";
tmpDoc.Range().Find.Execute(ref findText,
    true, true, true, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref replaceText, Word.WdReplace.wdReplaceAll, 
    ref missing, ref missing, ref missing, ref missing);
于 2013-02-20T20:30:32.273 に答える