2012-11-29 36 views
1

rtf文檔由數據庫應用程序生成,其中包含來自此數據庫的信息。我創建了一個軟件(C#,net framework 4.5),來獲取數據,然後將其記錄到Excel文件中。頁腳讀取後仍然有效(rtf公式文件)

我必須閱讀rtf文件的頁腳,我可以做的事情。

但是,當軟件訪問頁腳時,文檔視圖在頁腳/頁頭處於活動狀態時是相同的(當您在Word下雙擊頁眉/頁腳訪問頁面時,效果相同。在頭回車(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 

我試圖操縱Word中找到的東西找回到我的原始文檔(一頁),但沒有任何成功。

+0

不客氣。此外,由於您是StackOverflow的新用戶,因此我想告訴您,您可以通過查看答案旁邊的勾號來獲得最佳答案並接受最能幫助您的答案。在這個網站上upvote或接受的答案算作「謝謝」。 –

回答

0

清零對象不會清除其內容。如果要清除它,請更改範圍對象的文本

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

注意:這些對象具有引用類型。這意味着變量指向實際的對象,這是其他地方。如果將該變量設置爲null,則只是丟失了該對象的鏈接,但不會更改該對象。見我的答案SO質疑Setting a type reference type to null doesn't affect copied type?


UPDATE

我在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 
+0

謝謝。我已經在VBA下嘗試過了,它可以工作。現在,使用COM,除非將Word2003更改爲Word2010,否則仍然會遇到麻煩 – freeofbug

相關問題