原文:http://www.lob.cn/sl/other/2460.shtml

 

VB.Net为word设置页眉页脚
Public Class ThisDocument
    Private MyDocMenu As Office.CommandBarButton
    Private missing = System.Reflection.Missing.Value
    '在功能区中新增菜单组
    Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        Dim MyCommandBarPopup As Office.CommandBarPopup = Nothing
        Dim MyCommandBarMenu As Office.CommandBar = CType( _
            Application.CommandBars.ActiveMenuBar, Office.CommandBar)
        Dim MyControlsCount As Integer = MyCommandBarMenu.Controls.Count
        MyCommandBarPopup = CType(MyCommandBarMenu.Controls.Add( _
            Office.MsoControlType.msoControlPopup, missing, missing, _
            MyControlsCount, True), Office.CommandBarPopup)
        If (MyCommandBarPopup IsNot Nothing) Then
            MyCommandBarPopup.Caption = "文件管理"
            MyDocMenu = CType(MyCommandBarPopup.Controls.Add( _
                Office.MsoControlType.msoControlButton, missing, _
                missing, missing, True), Office.CommandBarButton)
            MyDocMenu.Caption = "设置页眉页脚"
            AddHandler MyDocMenu.Click, AddressOf MyDocMenuCommand_Click
        End If
    End Sub
    Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
    End Sub
    '设置页眉页脚 '更多.net源码和实例,来自乐博网 http://www.lob.cn/
    Private Sub MyDocMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
        Dim MyHeader As String = "乐博网lob.cn"
        Dim MyFooter As String = "vb2008实例更多在乐博网lob.cn"
        For Each MySection As Word.Section In Me.Sections
            MySection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
                Range.Font.ColorIndex = Word.WdColorIndex.wdDarkBlue
            MySection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
                Range.Font.Size = 16
            MySection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
                Range.Text = MyHeader
            MySection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
                Range.Font.ColorIndex = Word.WdColorIndex.wdBrightGreen
            MySection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
                Range.Font.Size = 12
            MySection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
                Range.Text = MyFooter
        Next
    End Sub
End Class