如何添加“第 x 页,共 y 页”页脚到 Word2007 文档,因为我使用 C# 生成它

发布于 2024-12-05 22:14:29 字数 2086 浏览 1 评论 0原文

我查看了此处此处此处

我尝试了这个:

    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

我尝试过的所有方法都没有完全对我有用(我已经有点接近了)。 如果问题的某些内容不清楚,请告诉我。

I looked here and here and here

I tried this:

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

And this:

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

I recorded this VBA macro, but it does not seem to be helpful.

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

Nothing that I tried quite worked for me entirely (I got somewhat close).
Let me know if something about the question is not clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别低头,皇冠会掉 2024-12-12 22:14:29

是的,成功了。

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

Ya, got it working.

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

That last line returns to the main document.

It't not the best and most "fancy" c#, but it works for me. C# .Net 3.5, Office 2007.

箹锭⒈辈孓 2024-12-12 22:14:29

这是用 VB 编写的,但我尝试过,它对我有用,尽管在这里您必须提供当前页码和总计页码。我确信有更好的解决方案:/

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

This is in VB but I tried this and it worked for me, although here you'd have to supply the current and total for page numbers. I'm sure there is a better solution :/

WordBasic.viewfooteronly
Selection.EndKey Unit:=wdStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.TypeText Text:="Page " & current & " of " & total
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
才能让你更想念 2024-12-12 22:14:29

此链接有助于解决此问题

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8/insert-current-page-number-quotpage-x-of-nquot-on -a-word-document?forum=vsto

这是我在 VB.NET 中解决它的方法:

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

This link helped in solving this problem

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8/insert-current-page-number-quotpage-x-of-nquot-on-a-word-document?forum=vsto

This is how I solved it in VB.NET:

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
睡美人的小仙女 2024-12-12 22:14:29

我刚刚解决了我的标题问题。标题由两行组成。第一行是纯文本“SLUŽBENI LIST BiH”,第二行应包含发行号(文本 Broj)、页码(Strana)、- 字幕 - 发行日期 Daywk var、day var、month var、year var 。副标题必须是斜体。
这是我的解决方案。

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)

我希望这能让别人的生活更轻松,如果有人有更优雅的解决方案,我想学习。
干杯!

I just solved my header problem. The header is composed of two lines. The first line is the plain text "SLUŽBENI LIST BiH", and the second line should contain the issue number (text Broj), page number (Strana), - subtitle - date of issue Daywk var, day var, month var, year var. The subtitle must be in italics.
Here is my solution.

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)

After adding the page number field, the following text would push the page number all the way to the right to the end. I think it's because after adding the field range positioner doesn't stay behind but in front of the field. The following line solved my problem.

    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)

I hope this can make someone's life easier, and if anyone has a more elegant solution, I'd like to learn.
Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文