创建带页脚的Word文档

Imports DocumentFormat.OpenXml.Packaging
Imports System.Windows.Forms
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml

Module Module1
    <STAThread()>
    Sub Main()
        Dim SFD As New SaveFileDialog
        Dim MDP As MainDocumentPart
        Dim Doc As Document
        Dim Body As Body
        Dim SP As SectionProperties
        Dim FP As FooterPart
        Dim Paragraph As Paragraph
        Dim Run As Run
        Dim Text As Text
        Dim FR As FooterReference
        Dim DSP As DocumentSettingsPart
        Dim Settings As Settings
        Dim CP As Compatibility
        Dim CPS As CompatibilitySetting
        Dim Path As String

        SFD.Filter = "Word Document|*.docx"
        SFD.ShowDialog()
        Path = SFD.FileName
        If Path.Length > 0 Then
            Using WPD As WordprocessingDocument = WordprocessingDocument _
                .Create(Path, WordprocessingDocumentType.Document)
                MDP = WPD.AddMainDocumentPart
                Doc = New Document
                Body = New Body
                SP = New SectionProperties
                FP = MDP.AddNewPart(Of FooterPart)()
                FP.Footer = New Footer
                Paragraph = New Paragraph
                Run = New Run
                Text = New Text
                Text.Text = "Just a test"
                Run.Append(Text)
                Paragraph.Append(Run)
                FP.Footer.Append(Paragraph)
                FR = New FooterReference
                With FR
                    .Type = HeaderFooterValues.Default
                    .Id = MDP.GetIdOfPart(FP)
                End With
                SP.Append(FR)
                Body.Append(SP)
                Doc.Body = Body
                MDP.Document = Doc
                DSP = MDP.AddNewPart(Of DocumentSettingsPart)()
                Settings = New Settings
                CP = New Compatibility
                CPS = New CompatibilitySetting
                With CPS
                    .Name = CompatSettingNameValues.CompatibilityMode
                    .Uri = "http://schemas.microsoft.com/office/word"
                    .Val = "14"
                End With
                CP.Append(CPS)
                Settings.Append(CP)
                DSP.Settings = Settings
            End Using
        Else
            MsgBox("Path is invalid")
        End If
    End Sub

End Module

你可能感兴趣的:(word)