页脚在读取后仍然有效(rtf公式文件)

时间:2012-11-29 16:28:32

标签: c# ms-word footer rtf

rtf文档由数据库应用程序生成,包含来自此数据库的信息。我创建了一个软件(C#,net framework 4.5),用于获取数据,然后将其记录到Excel文件中。

我必须阅读rtf文件的页脚,我能做的事情。

但是,当软件访问页脚时,文件视图在页脚/页眉处于活动状态时是相同的(当您在Word下双击页眉/页脚以访问它时效果相同。此操作操作添加了一个支架返回标题(Word添加此项以输入内容),此\ r \ n会导致有其他页面。

这里是代码:

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

我试图操纵Word找到回复原始文档的内容(一页),但没有任何成功。

1 个答案:

答案 0 :(得分:0)

取消对象将无法清除其内容。如果要清除它,请更改范围对象的文本

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

注意:这些对象具有引用类型。这意味着变量指向实际对象,该对象位于其他位置。如果将变量设置为null,则只是丢失了对象的链接,但您没有更改对象。请参阅我对SO问题Setting a type reference type to null doesn't affect copied type?

的回答

<强>更新

我在Word中进行了一项实验,使用一个VBA宏来读取页脚的表格范围,如上所述。它不会更改单词的视图类型。

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
相关问题