2011-09-21 10 views
5

ho guardato here e here e hereCome aggiungere una "Pagina X di Y" piè di pagina per Word2007 doc come sto generando usando C#

ho provato questo:

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); 
     } 
    } 

E questo:

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; 
     } 
    } 

Ho registrato questa macro VBA, ma non sembra essere utile.

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

Niente di quello che ho provato ha funzionato abbastanza per me interamente (mi sono avvicinato un po '). Fatemi sapere se qualcosa sulla domanda non è chiara.

+0

Hai mai funzionato? Sto cercando di fare la stessa cosa. –

+0

@Christopher Mahan, mi dispiace, ho smesso di provare ad un certo punto e ho deciso di emettere il codice LaTeX e compilare quello invece. Era abbastanza buono per me. Se risolvi questo, allora darei volentieri un'occhiata alla risposta. Posso suggerire di bruciare parte della tua reputazione sulla taglia. Impostalo in alto ma spiega cosa ti serve. –

risposta

0

Questo è in VB, ma ho provato questo e ha funzionato per me, anche se qui si dovrebbe fornire il corrente e il totale per i numeri di pagina. Sono sicuro che c'è una soluzione migliore:/

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

Inoltre, puoi provare ad aggiungere un segnalibro nel punto in cui vuoi che sia il piè di pagina e quindi impostare la proprietà range.text dei segnalibri – DontFretBrett

1

Ya, ha funzionato.

// 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; 

L'ultima riga torna al documento principale.

Non è il migliore e più "elegante" C#, ma funziona per me. C# .Net 3.5, Office 2007.

Problemi correlati