2013-03-04 31 views
0

即時通訊需要實現一種方式來一鍵添加頁腳到由一行組成的word文檔。 第一部分需要是文檔的絕對路徑,它必須是左邊界的。除此之外,實際頁碼必須與右側對齊。VSTO格式的頁腳左邊界

這不是Excel上的問題;那裏我可以使用LeftFooter,CenterFooter,RightFooter。 在Word上,但是沒有這樣的屬性可以訪問。

編輯:我發現一個半工作的解決方案,它有一些錯誤,並沒有正確的設計,因爲我找不到合適的方式。

Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument; 
     foreach (Word.Section wordSection in doc.Sections) 
     { 
      Word.Range PageNumberRange = wordSection.Range; 
      PageNumberRange.Fields.Add(PageNumberRange, Word.WdFieldType.wdFieldEmpty ,"PAGE Arabic ", true); 


      Word.Range footer = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
      footer.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; 
      footer.Tables.Add(footer, 1, 3); 

      Word.Table tbl = footer.Tables[1]; 

      tbl.Cell(1, 1).Range.Text = doc.FullName; 
      tbl.Cell(1, 3).Range.Text = PageNumberRange.Text; 
      /**/ 

      footer.Font.ColorIndex = Word.WdColorIndex.wdBlack; 
      footer.Font.Size = 6; 

      PageNumberRange.Text = ""; 

這個問題是:它從不會覆蓋現有的頁腳。如果它寫入「document1 ... 1」,並且您再次單擊它,因爲您保存了文檔,它會更改頁腳。此外:如果您有多個頁面,除頁面1以外的每個頁面都會被刪除。

我從來沒有想過要實現這麼簡單的任務可能太困難了。

回答

0

使用樣式

Document doc = this.application.ActiveDocument; 
Section wordSection = doc.Sections[1];  
Range footer = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;        
footer.Fields.Add(footer, WdFieldType.wdFieldEmpty, @"PAGE \* ARABIC", true);        
footer.Collapse(WdCollapseDirection.wdCollapseStart); 
footer.InsertBefore("\t \t"); 
footer.InsertBefore(doc.FullName);               
footer.Font.Name = "Arial"; 
+0

的主要問題仍然是一個替代方法:我如何添加這個領域,在同一線路上,那裏的路徑已經站? 只需添加此行會導致我的路徑與頁碼過度纏繞。 – Marco 2013-03-04 11:15:14

+0

你的意思是你想在寫新的之前清理現有的頁腳? – 2013-03-05 01:43:08

+0

我編輯了我原來的帖子,以顯示我面臨的問題。 – Marco 2013-03-05 07:42:32