2011-09-21 11 views
5

Tôi nhìn hereherehereLàm thế nào để thêm một "Trang x của y" chân để Word2007 doc như tôi đang tạo ra nó bằng cách sử C#

Tôi cố gắng này:

private void AddFooters() 
    { 
     foreach (Word.Section wordSection in this.WordDoc.Sections) 
     { 
      object fieldEmpty = Word.WdFieldType.wdFieldEmpty; 
      object autoText = "AUTOTEXT \"Page X of Y\" "; 
      object preserveFormatting = true; 

      wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
       wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range, 
       ref fieldEmpty, ref autoText, ref preserveFormatting); 
     } 
    } 

Và đây:

private void AddFooters() 
    { 
     foreach (Word.Section section in this.WordDoc.Sections) 
     { 
      Word.Range footerRange = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
      this.WordDoc.ActiveWindow.Selection.TypeText("Page "); 
      footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldPage); 
      this.WordDoc.ActiveWindow.Selection.TypeText(" of "); 
      footerRange = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
      footerRange.Fields.Add(footerRange, Word.WdFieldType.wdFieldNumPages); 
      footerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; 
     } 
    } 

Tôi đã ghi macro VBA này, nhưng dường như không hữu ích.

Sub Macro1() 
' 
' Macro1 Macro 
' 
' 
    WordBasic.ViewFooterOnly 
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 3"). _ 
     Insert Where:=Selection.Range, RichText:=True 
End Sub 

Không có gì mà tôi đã thử hoàn toàn làm việc cho tôi hoàn toàn (tôi đã phần nào đóng). Hãy cho tôi biết nếu có điều gì đó về câu hỏi không rõ ràng.

+0

Bạn đã bao giờ làm việc đó chưa? Tôi đang tìm cách làm điều tương tự. –

+0

@Christopher Mahan, xin lỗi, tôi đã ngừng thử một lúc nào đó và quyết định phát ra mã LaTeX và biên dịch thay vào đó. Đó là đủ tốt cho tôi. Nếu bạn giải quyết vấn đề này, tôi sẽ sẵn sàng xem câu trả lời. Tôi có thể đề nghị đốt cháy một số danh tiếng của bạn trên tiền thưởng. Đặt nó cao nhưng giải thích những gì bạn cần. –

Trả lời

0

Đây là trong VB nhưng tôi đã thử điều này và nó làm việc cho tôi, mặc dù ở đây bạn sẽ phải cung cấp số hiện tại và tổng số trang. Tôi chắc rằng có là một giải pháp tốt hơn:/

WordBasic.viewfooteronly 
Selection.EndKey Unit:=wdStory 
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight 
Selection.TypeText Text:="Page " & current & " of " & total 
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 
+0

Ngoài ra, bạn có thể thử thêm dấu trang mà bạn muốn chân trang được đặt sau đó đặt thuộc tính dấu trang.text – DontFretBrett

1

Ya, nhận nó làm việc.

// objects I use in the code below 
// instanciate wordapp as the Word application object 
Microsoft.Office.Interop.Word.Application wordapp = new Microsoft.Office.Interop.Word.Application(); 


// create missing object for compatibility with C# .NET 3.5 
Object oMissing = System.Reflection.Missing.Value; 

// define worddoc as the word document object 
Microsoft.Office.Interop.Word.Document worddoc = wordapp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

// define s as the selection object 
Microsoft.Office.Interop.Word.Selection s = wordapp.Selection; 

// code for the page numbers 
// move selection to page footer (Use wdSeekCurrentPageHeader for header) 
worddoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter; 

// Align right 
s.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; 

// start typing 
worddoc.ActiveWindow.Selection.TypeText("Page "); 

// create the field for current page number 
object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage; 

// insert that field into the selection 
worddoc.ActiveWindow.Selection.Fields.Add(s.Range, ref CurrentPage, ref oMissing, ref oMissing); 

// write the "of" 
worddoc.ActiveWindow.Selection.TypeText(" of "); 

// create the field for total page number. 
object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages; 

// insert total pages field in the selection. 
worddoc.ActiveWindow.Selection.Fields.Add(s.Range, ref TotalPages, ref oMissing, ref oMissing); 

// return to the document main body. 
worddoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; 

Dòng cuối cùng đó trở về tài liệu chính.

Nó không phải là tốt nhất và hầu hết "ưa thích" C#, nhưng nó hoạt động cho tôi. C# .Net 3.5, Office 2007.

Các vấn đề liên quan