1

を使用してC#(3.5)の単語ドキュメントを読んでいますMicrosoft.Office.Interop.Word。行ごとに読み取り、行をarray []に分割し、行のすべての単語を処理し、いくつかの単語を置き換えるビジネスロジックに基づいて、単語を置き換えた後、行全体を変換された行に置き換えます。

今まですべてが正常に機能しています。

今、私はいくつかの単語文書を持っています、それらは段落と表を持っています。表のすべての列を1つずつ読み取り、特定の列の列の内容を置き換えたいと思います。

アップデート


Officeオートメーションの使用

1. Opening word file.
2. Moving cursor to top of the document
3. Selecting first line using (`wordApp.Selection.endKey`) and processing all words
4. After processing the words replacing the selected line with the processed line.
5. Using wordApp.Selection.MoveDown(ref lineCount, ref countPage, ref MISSING);    
   moving next line processed further.

問題:1。テーブルを読み取るときに、使用時に最初の列のみを読み取るwordApp.Selection.endKey

すべての列のデータを処理したい。コンテンツが段落か表かを識別する方法はありますか?

ここに画像の説明を入力してください

4

2 に答える 2

8

ドキュメントのスキャンに選択を使用すると、パフォーマンスが非常に高くなります。次のコードをお勧めします。

        List<Word.Range> TablesRanges = new List<Word.Range>();

        wordApp = new Microsoft.Office.Interop.Word.Application();
        doc = wordApp.Documents.OpenNoRepairDialog(FileName: @"c:\AAAAA.docx", ConfirmConversions: false, ReadOnly: true, AddToRecentFiles: false, NoEncodingDialog: true);


        for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++)
        {
            Word.Range TRange = doc.Tables[iCounter].Range;
            TablesRanges.Add(TRange);
        }

        Boolean bInTable;
        for (int par = 1; par <= doc.Paragraphs.Count; par++)
        {
            bInTable = false;
            Word.Range r = doc.Paragraphs[par].Range;
            foreach (Word.Range range in TablesRanges)
            {
                if (r.Start >= range.Start && r.Start <= range.End)
                {
                    Console.WriteLine("In Table - Paragraph number " + par.ToString() + ":" + r.Text);
                    bInTable = true;
                    break;
                }

            }

            if (!bInTable)
                Console.WriteLine("!!!!!! Not In Table - Paragraph number " + par.ToString() + ":" + r.Text);
        }
于 2013-09-30T19:18:41.513 に答える
2

私は同じ回避策を見つけました。アプローチを以下に示します。

1. を使用して Word 文書を開きますWordApp.Documents.Open()
2. を使用Selection.MoveDownして文書を行ごとにトラバースし
ます 3. 表のセルの内容をスキップします
4. 最後に文書の表のみを処理します

//Process all Paragraphs in the documents
while (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc") == false)
{
  doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove);
  doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);

  //Skiping table content
  if (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1")
  {
    while (doc.ActiveWindow.Selection.get_Information(WdInformation.wdEndOfRangeColumnNumber).ToString() != "-1")
    {
      if (doc.ActiveWindow.Selection.Bookmarks.Exists(@"\EndOfDoc"))
        break;

      doc.ActiveWindow.Selection.MoveDown(ref wdLine, ref wdCountOne, ref wdMove);
      doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);
    }
    doc.ActiveWindow.Selection.HomeKey(ref wdLine, ref wdMove);
  }

  doc.ActiveWindow.Selection.EndKey(ref wdLine, ref wdExtend);
  currLine = doc.ActiveWindow.Selection.Text;
}

//Processing all tables in the documents
for (int iCounter = 1; iCounter <= doc.Tables.Count; iCounter++)
{
  foreach (Row aRow in doc.Tables[iCounter].Rows)
  {
    foreach (Cell aCell in aRow.Cells)
    {
      currLine = aCell.Range.Text;
      //Process Line
    }
  }
}
于 2012-08-16T05:36:48.390 に答える