如何添加"页x "页脚到Word2007文档,因为我正在使用c#生成它
本文关键字:quot 因为 添加 何添加 Word2007 文档 | 更新日期: 2023-09-27 18:09:20
我看这里,这里和这里
我试过了:
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);
}
}
:
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;
}
}
我记录了这个VBA宏,但它似乎没有帮助。
Sub Macro1()
'
' Macro1 Macro
'
'
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 3"). _
Insert Where:=Selection.Range, RichText:=True
End Sub
我试过的方法都没有完全奏效(我接近了)。如果有什么地方不清楚,请告诉我。
嗯,搞定了。
// 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;
最后一行返回主文档。
它不是最好和最"花哨"的c#,但它适合我。c# . net 3.5, Office 2007
这是在VB,但我试过了,它为我工作,虽然在这里你必须提供当前和总页码。我相信有更好的解决办法:/
WordBasic.viewfooteronly
Selection.EndKey Unit:=wdStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.TypeText Text:="Page " & current & " of " & total
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
这个链接帮助解决了这个问题
https://social.msdn.microsoft.com/forums/vstudio/en - us/a044ff2d b4a7 - 4 - f19 - 84 - f4 f3d5c55396a8/insert当前页-数量- quotpage - x -的- nquot——一个字——document?forum=vsto
这就是我在VB中解决它的方法。净:
Dim aDoc As Word.Document
aDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter
aDoc.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight
aDoc.ActiveWindow.Selection.TypeText("Page ")
Dim CurrentPage = Word.WdFieldType.wdFieldPage
aDoc.ActiveWindow.Selection.Fields.Add(aDoc.ActiveWindow.Selection.Range, CurrentPage, , )
aDoc.ActiveWindow.Selection.TypeText(" of ")
Dim TotalPageCount = Word.WdFieldType.wdFieldNumPages
aDoc.ActiveWindow.Selection.Fields.Add(aDoc.ActiveWindow.Selection.Range, TotalPageCount, , )
aDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument
我刚刚解决了我的标题问题。标题由两行组成。第一行为纯文本"SLUŽBENI LIST BiH"第二行包含发行号(文本Broj)、页码(Strana)、- subtitle -发行日期(日、月、年),副标题必须为斜体。这是我的解决方案。
Imports Microsoft.Office.Interop.Word
Imports Word = Microsoft.Office.Interop.Word
Dim headerRange As Word.Range = Section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range
headerRange.Font.Size = 9
headerRange.Text = "SLUŽBENI GLASNIK BiH" & vbCrLf
headerRange.Font.Italic = False
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
headerRange.Text = "Broj " & Br_Gl & " - Strana "
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
headerRange.Fields.Add(headerRange, CurrentPage)
添加页码字段后,下面的文本将把页码一直推到末尾。我认为这是因为在添加了场范围定位器后,它没有留在后面,而是在场的前面。下面这行解决了我的问题。
headerRange.MoveEnd(Word.WdUnits.wdCharacter, 1)
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
headerRange.Text = vbTab & " - O g l a s n i k j a v n e n a b a v k e - "
headerRange.Font.Italic = True
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
headerRange.Text = vbTab & DanObjL & ", " & DanObj & ". " & Mjesec & ". " & Godina & "."
headerRange.Font.Italic = False
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
我希望这能让一些人的生活更轻松,如果有人有更优雅的解决方案,我很乐意学习。干杯!