1

rtf ドキュメントは、このデータベースからの情報を使用して、データベース アプリケーションによって生成されます。データを取得して Excel ファイルに記録するソフトウェア (C#、ネット フレームワーク 4.5) を作成しました。

rtf ファイルのフッターを読まなければなりません。

ただし、ソフトウェアがフッターにアクセスする場合、フッター/ヘッダーがアクティブな場合のドキュメント ビューは同じです (Word の下にいるときに、ヘッダー/フッターをダブルクリックしてアクセスした場合と同じ効果です)。このアクション アクションは、ヘッダーにキャリッジ リターンを追加します。 (単語はこれを追加して何かを入力します)、この \r によりページが追加されます。

ここにコード:

Sections oSection = cGlobalVar.varWordApp.ActiveDocument.Sections;
HeaderFooter oFooter = oSection[1].Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];

Range oRange = oFooter.Range.Tables[1].Range;//<= at this point, footer is accessible, the empty header of original document has a\r character, causing 2nd page to document that I don't want

strBuffer = oRange.Text;//<= information I need

oRange = oSection[1].Range.Tables[1].Range;//<= try to affect something else to oRange
oFooter = null;//<= try to null the object
oSection = null;//<= same as above

//cGlobalVar.varWordDoc.ActiveWindow.View.Type = WdViewType.wdPrintView;//<= try to use this to return to a normal state

元の文書 (1 ページ) に戻すために Word を操作しようとしましたが、成功しませんでした。

4

1 に答える 1

0

オブジェクトを null にしても、その内容はクリアされません。クリアしたい場合は範囲​​オブジェクトのテキストを変更

oFooter.Range.Text = "";
oSection.Range.Text = "";

注: これらのオブジェクトには参照型があります。これは、変数が別の場所にある実際のオブジェクトを指していることを意味します。変数を null に設定すると、オブジェクトへのリンクが失われるだけで、オブジェクトは変更されません。SO の質問に対する私の回答を参照してください。型参照型を null に設定しても、コピーされた型には影響しませんか?


アップデート

上記のように、フッターのテーブル範囲を読み取る VBA マクロを使用して、Word で実験を行いました。単語の表示タイプは変更されません。

Sub Macro1()
    Dim oSection As Sections
    Dim oFooter As HeaderFooter
    Dim oRange As Range
    Dim strBuffer As String

    Set oSection = Application.ActiveDocument.Sections
    Set oFooter = oSection(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary)
    Set oRange = oFooter.Range.Tables(1).Range

    strBuffer = oRange.Text
    Debug.Print strBuffer
End Sub
于 2012-11-29T16:39:46.990 に答える