locked
Page numbering in a word processing document RRS feed

  • Question

  • Hi,

    I need help how to include page numbering in a wordprocessing document.

    The document is generated on a web server (ASP.NET) (VB)

    I need help with creating the extendedfile properties part, as well as referencing to .pages later on.

    I´ve read a few answers, but they don´t work.

    Thanks.

    Friday, July 27, 2012 11:41 AM

Answers

  • Hi Black,

    Thanks for posting in the MSDN Forum.

    I hope following snippet will help you:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    namespace OpenXmlFooterPageNumber
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SaveFileDialog objSaveFileDialog = new SaveFileDialog();
                objSaveFileDialog.Filter = "Word Document (*.docx)|*.docx";
                objSaveFileDialog.ShowDialog();
                string strSavePath = objSaveFileDialog.FileName;
                if (strSavePath.Length > 0)
                {
                    using (WordprocessingDocument objWordprocessingDocument =
                        WordprocessingDocument.Create
                        (strSavePath, WordprocessingDocumentType.Document))
                    {
                        MainDocumentPart objMainDocumentPart =
                            objWordprocessingDocument.AddMainDocumentPart();
                        Document objDocument = new Document();
                        objMainDocumentPart.Document = objDocument;
                        Body objBody = new Body();
                        SectionProperties objSectionProperties = 
                            new SectionProperties();
                        FooterPart objFootPart = 
                            objMainDocumentPart.AddNewPart<FooterPart>();
                        Footer objFooter = new Footer();
                        objFootPart.Footer = objFooter;
                        SdtBlock objSdtBlock_1 = new SdtBlock();
                        SdtContentBlock objSdtContentBlock_1 = 
                            new SdtContentBlock();
                        SdtBlock objSdtBlock_2 = new SdtBlock();
                        SdtContentBlock objSdtContentBlock_2 = 
                            new SdtContentBlock();
                        Paragraph objParagraph_1 = new Paragraph();
                        ParagraphProperties objParagraphProperties = 
                            new ParagraphProperties();
                        ParagraphStyleId objParagraphStyleId =
                            new ParagraphStyleId() { Val = "Footer" };
                        objParagraphProperties.Append(objParagraphStyleId);
                        Justification objJustification = 
                            new Justification() { Val = JustificationValues.Right };
                        objParagraphProperties.Append(objJustification);
                        objParagraph_1.Append(objParagraphProperties);
                        Run objRun_1 = new Run();
                        Text objText_1 = new Text();
                        objText_1.Text = "Page ";
                        objRun_1.Append(objText_1);
                        objParagraph_1.Append(objRun_1);
                        Run objRun_2 = new Run();
                        FieldChar objFieldChar_1 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Begin };
                        objRun_2.Append(objFieldChar_1);
                        objParagraph_1.Append(objRun_2);
                        Run objRun_3 = new Run();
                        FieldCode objFieldCode_1 = 
                            new FieldCode() 
                            { Space = SpaceProcessingModeValues.Preserve };
                        objFieldCode_1.Text = "PAGE ";
                        objRun_3.Append(objFieldCode_1);
                        objParagraph_1.Append(objRun_3);
                        Run objRun_4 = new Run();
                        FieldChar objFieldChar_2 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Separate };
                        objRun_4.Append(objFieldChar_2);
                        objParagraph_1.Append(objRun_4);
                        Run objRun_5 = new Run();
                        Text objText_2 = new Text();
                        objText_2.Text = "2";
                        objRun_5.Append(objText_2);
                        objParagraph_1.Append(objRun_5);
                        Run objRun_6 = new Run();
                        FieldChar objFieldChar_3 = 
                            new FieldChar() {FieldCharType = FieldCharValues.End };
                        objRun_6.Append(objFieldChar_3);
                        objParagraph_1.Append(objRun_6);
                        Run objRun_7 = new Run();
                        Text objText_3 = new Text();
                        objText_3.Text = "of ";
                        objRun_7.Append(objText_3);
                        objParagraph_1.Append(objRun_7);
                        Run objRun_8 = new Run();
                        FieldChar objFieldChar_4 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Begin };
                        objRun_8.Append(objFieldChar_4);
                        objParagraph_1.Append(objRun_8);
                        Run objRun_9 = new Run();
                        FieldCode objFieldCode_2 = 
                            new FieldCode() 
                            { Space = SpaceProcessingModeValues.Preserve };
                        objFieldCode_2.Text = "NUMPAGES  ";
                        objRun_9.Append(objFieldCode_2);
                        objParagraph_1.Append(objRun_9);
                        Run objRun_10 = new Run();
                        FieldChar objFieldChar_5 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Separate };
                        objRun_10.Append(objFieldChar_5);
                        objParagraph_1.Append(objRun_10);
                        Run objRun_11 = new Run();
                        Text objText_4 = new Text();
                        objText_4.Text = "2";
                        objRun_11.Append(objText_4);
                        objParagraph_1.Append(objRun_11);
                        Run objRun_12 = new Run();
                        FieldChar objFieldChar_6 = 
                            new FieldChar() {FieldCharType = FieldCharValues.End };
                        objRun_12.Append(objFieldChar_6);
                        objParagraph_1.Append(objRun_12);
                        objSdtContentBlock_2.Append(objParagraph_1);
                        objSdtBlock_2.Append(objSdtContentBlock_2);
                        objSdtContentBlock_1.Append(objSdtBlock_2);
                        objSdtBlock_1.Append(objSdtContentBlock_1);
                        objFooter.Append(objSdtBlock_1);
                        string strFootrID =
                            objMainDocumentPart.GetIdOfPart(objFootPart);
                        FooterReference objFooterReference = new FooterReference() 
                        { 
                            Type = HeaderFooterValues.Default, 
                            Id = strFootrID 
                        };
                        objSectionProperties.Append(objFooterReference);
                        objBody.Append(objSectionProperties);
                        objMainDocumentPart.Document.Append(objBody);
                        DocumentSettingsPart objDocumentSettingPart =
                            objMainDocumentPart.AddNewPart<DocumentSettingsPart>();
                        objDocumentSettingPart.Settings = new Settings();
                        Compatibility objCompatibility = new Compatibility();
                        CompatibilitySetting objCompatibilitySetting =
                            new CompatibilitySetting()
                            {
                                Name = CompatSettingNameValues.CompatibilityMode,
                                Uri = "http://schemas.microsoft.com/office/word",
                                Val = "14"
                            };
                        objCompatibility.Append(objCompatibilitySetting);
                        objDocumentSettingPart.Settings.Append(objCompatibility);
                    }
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Black Santa Friday, August 3, 2012 10:02 PM
    Monday, July 30, 2012 8:28 AM
  • Thanks so much, you´ve saved the day! It works perfectly.

    This is the VB version:

        Private Sub pnum()
            'Dim objSaveFileDialog As New SaveFileDialog()
            'objSaveFileDialog.Filter = "Word Document (*.docx)|*.docx"
            'objSaveFileDialog.ShowDialog()
            Dim strSavePath As String = "pathtodoc"
            If strSavePath.Length > 0 Then
                Using objWordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Create(strSavePath, WordprocessingDocumentType.Document)
                    Dim objMainDocumentPart As MainDocumentPart = objWordprocessingDocument.AddMainDocumentPart()
                    Dim objDocument As New Document()
                    objMainDocumentPart.Document = objDocument
                    Dim objBody As New Body()
                    Dim objSectionProperties As New SectionProperties()
                    Dim objFootPart As FooterPart = objMainDocumentPart.AddNewPart(Of FooterPart)()
                    Dim objFooter As New Footer()
                    objFootPart.Footer = objFooter
                    Dim objSdtBlock_1 As New SdtBlock()
                    Dim objSdtContentBlock_1 As New SdtContentBlock()
                    Dim objSdtBlock_2 As New SdtBlock()
                    Dim objSdtContentBlock_2 As New SdtContentBlock()
                    Dim objParagraph_1 As New Paragraph()
                    Dim objParagraphProperties As New ParagraphProperties()
                    Dim objParagraphStyleId As New ParagraphStyleId() With { _
                      .Val = "Footer" _
                    }
                    objParagraphProperties.Append(objParagraphStyleId)
                    Dim objJustification As New Justification() With { _
                     .Val = JustificationValues.Right _
                    }
                    objParagraphProperties.Append(objJustification)
                    objParagraph_1.Append(objParagraphProperties)
                    Dim objRun_1 As New Run()
                    Dim objText_1 As New Text()
                    objText_1.Text = "Page "
                    objRun_1.Append(objText_1)
                    objParagraph_1.Append(objRun_1)
                    Dim objRun_2 As New Run()
                    Dim objFieldChar_1 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Begin _
                    }
                    objRun_2.Append(objFieldChar_1)
                    objParagraph_1.Append(objRun_2)
                    Dim objRun_3 As New Run()
                    Dim objFieldCode_1 As New FieldCode() With { _
                     .Space = SpaceProcessingModeValues.Preserve _
                    }
                    objFieldCode_1.Text = "PAGE "
                    objRun_3.Append(objFieldCode_1)
                    objParagraph_1.Append(objRun_3)
                    Dim objRun_4 As New Run()
                    Dim objFieldChar_2 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Separate _
                    }
                    objRun_4.Append(objFieldChar_2)
                    objParagraph_1.Append(objRun_4)
                    Dim objRun_5 As New Run()
                    Dim objText_2 As New Text()
                    objText_2.Text = "2"
                    objRun_5.Append(objText_2)
                    objParagraph_1.Append(objRun_5)
                    Dim objRun_6 As New Run()
                    Dim objFieldChar_3 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.[End] _
                    }
                    objRun_6.Append(objFieldChar_3)
                    objParagraph_1.Append(objRun_6)
                    Dim objRun_7 As New Run()
                    Dim objText_3 As New Text()
                    objText_3.Text = "of "
                    objRun_7.Append(objText_3)
                    objParagraph_1.Append(objRun_7)
                    Dim objRun_8 As New Run()
                    Dim objFieldChar_4 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Begin _
                    }
                    objRun_8.Append(objFieldChar_4)
                    objParagraph_1.Append(objRun_8)
                    Dim objRun_9 As New Run()
                    Dim objFieldCode_2 As New FieldCode() With { _
                     .Space = SpaceProcessingModeValues.Preserve _
                    }
                    objFieldCode_2.Text = "NUMPAGES  "
                    objRun_9.Append(objFieldCode_2)
                    objParagraph_1.Append(objRun_9)
                    Dim objRun_10 As New Run()
                    Dim objFieldChar_5 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Separate _
                    }
                    objRun_10.Append(objFieldChar_5)
                    objParagraph_1.Append(objRun_10)
                    Dim objRun_11 As New Run()
                    Dim objText_4 As New Text()
                    objText_4.Text = "2"
                    objRun_11.Append(objText_4)
                    objParagraph_1.Append(objRun_11)
                    Dim objRun_12 As New Run()
                    Dim objFieldChar_6 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.[End] _
                    }
                    objRun_12.Append(objFieldChar_6)
                    objParagraph_1.Append(objRun_12)
                    objSdtContentBlock_2.Append(objParagraph_1)
                    objSdtBlock_2.Append(objSdtContentBlock_2)
                    objSdtContentBlock_1.Append(objSdtBlock_2)
                    objSdtBlock_1.Append(objSdtContentBlock_1)
                    objFooter.Append(objSdtBlock_1)
                    Dim strFootrID As String = objMainDocumentPart.GetIdOfPart(objFootPart)
                    Dim objFooterReference As New FooterReference() With { _
                     .Type = HeaderFooterValues.[Default], _
                     .Id = strFootrID _
                    }
                    objSectionProperties.Append(objFooterReference)
                    objBody.Append(objSectionProperties)
                    objMainDocumentPart.Document.Append(objBody)
                    Dim objDocumentSettingPart As DocumentSettingsPart = objMainDocumentPart.AddNewPart(Of DocumentSettingsPart)()
                    objDocumentSettingPart.Settings = New Settings()
                    Dim objCompatibility As New Compatibility()
                    Dim objCompatibilitySetting As New CompatibilitySetting() With { _
                     .Name = CompatSettingNameValues.CompatibilityMode, _
                     .Uri = "http://schemas.microsoft.com/office/word", _
                     .Val = "14" _
                    }
                    objCompatibility.Append(objCompatibilitySetting)
                    objDocumentSettingPart.Settings.Append(objCompatibility)
                End Using
            End If
        End Sub

    • Marked as answer by Black Santa Friday, August 3, 2012 10:06 PM
    Friday, August 3, 2012 10:05 PM

All replies

  • Hi Black,

    Thanks for posting in the MSDN Forum.

    I hope following snippet will help you:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    namespace OpenXmlFooterPageNumber
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SaveFileDialog objSaveFileDialog = new SaveFileDialog();
                objSaveFileDialog.Filter = "Word Document (*.docx)|*.docx";
                objSaveFileDialog.ShowDialog();
                string strSavePath = objSaveFileDialog.FileName;
                if (strSavePath.Length > 0)
                {
                    using (WordprocessingDocument objWordprocessingDocument =
                        WordprocessingDocument.Create
                        (strSavePath, WordprocessingDocumentType.Document))
                    {
                        MainDocumentPart objMainDocumentPart =
                            objWordprocessingDocument.AddMainDocumentPart();
                        Document objDocument = new Document();
                        objMainDocumentPart.Document = objDocument;
                        Body objBody = new Body();
                        SectionProperties objSectionProperties = 
                            new SectionProperties();
                        FooterPart objFootPart = 
                            objMainDocumentPart.AddNewPart<FooterPart>();
                        Footer objFooter = new Footer();
                        objFootPart.Footer = objFooter;
                        SdtBlock objSdtBlock_1 = new SdtBlock();
                        SdtContentBlock objSdtContentBlock_1 = 
                            new SdtContentBlock();
                        SdtBlock objSdtBlock_2 = new SdtBlock();
                        SdtContentBlock objSdtContentBlock_2 = 
                            new SdtContentBlock();
                        Paragraph objParagraph_1 = new Paragraph();
                        ParagraphProperties objParagraphProperties = 
                            new ParagraphProperties();
                        ParagraphStyleId objParagraphStyleId =
                            new ParagraphStyleId() { Val = "Footer" };
                        objParagraphProperties.Append(objParagraphStyleId);
                        Justification objJustification = 
                            new Justification() { Val = JustificationValues.Right };
                        objParagraphProperties.Append(objJustification);
                        objParagraph_1.Append(objParagraphProperties);
                        Run objRun_1 = new Run();
                        Text objText_1 = new Text();
                        objText_1.Text = "Page ";
                        objRun_1.Append(objText_1);
                        objParagraph_1.Append(objRun_1);
                        Run objRun_2 = new Run();
                        FieldChar objFieldChar_1 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Begin };
                        objRun_2.Append(objFieldChar_1);
                        objParagraph_1.Append(objRun_2);
                        Run objRun_3 = new Run();
                        FieldCode objFieldCode_1 = 
                            new FieldCode() 
                            { Space = SpaceProcessingModeValues.Preserve };
                        objFieldCode_1.Text = "PAGE ";
                        objRun_3.Append(objFieldCode_1);
                        objParagraph_1.Append(objRun_3);
                        Run objRun_4 = new Run();
                        FieldChar objFieldChar_2 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Separate };
                        objRun_4.Append(objFieldChar_2);
                        objParagraph_1.Append(objRun_4);
                        Run objRun_5 = new Run();
                        Text objText_2 = new Text();
                        objText_2.Text = "2";
                        objRun_5.Append(objText_2);
                        objParagraph_1.Append(objRun_5);
                        Run objRun_6 = new Run();
                        FieldChar objFieldChar_3 = 
                            new FieldChar() {FieldCharType = FieldCharValues.End };
                        objRun_6.Append(objFieldChar_3);
                        objParagraph_1.Append(objRun_6);
                        Run objRun_7 = new Run();
                        Text objText_3 = new Text();
                        objText_3.Text = "of ";
                        objRun_7.Append(objText_3);
                        objParagraph_1.Append(objRun_7);
                        Run objRun_8 = new Run();
                        FieldChar objFieldChar_4 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Begin };
                        objRun_8.Append(objFieldChar_4);
                        objParagraph_1.Append(objRun_8);
                        Run objRun_9 = new Run();
                        FieldCode objFieldCode_2 = 
                            new FieldCode() 
                            { Space = SpaceProcessingModeValues.Preserve };
                        objFieldCode_2.Text = "NUMPAGES  ";
                        objRun_9.Append(objFieldCode_2);
                        objParagraph_1.Append(objRun_9);
                        Run objRun_10 = new Run();
                        FieldChar objFieldChar_5 = 
                            new FieldChar() 
                            { FieldCharType = FieldCharValues.Separate };
                        objRun_10.Append(objFieldChar_5);
                        objParagraph_1.Append(objRun_10);
                        Run objRun_11 = new Run();
                        Text objText_4 = new Text();
                        objText_4.Text = "2";
                        objRun_11.Append(objText_4);
                        objParagraph_1.Append(objRun_11);
                        Run objRun_12 = new Run();
                        FieldChar objFieldChar_6 = 
                            new FieldChar() {FieldCharType = FieldCharValues.End };
                        objRun_12.Append(objFieldChar_6);
                        objParagraph_1.Append(objRun_12);
                        objSdtContentBlock_2.Append(objParagraph_1);
                        objSdtBlock_2.Append(objSdtContentBlock_2);
                        objSdtContentBlock_1.Append(objSdtBlock_2);
                        objSdtBlock_1.Append(objSdtContentBlock_1);
                        objFooter.Append(objSdtBlock_1);
                        string strFootrID =
                            objMainDocumentPart.GetIdOfPart(objFootPart);
                        FooterReference objFooterReference = new FooterReference() 
                        { 
                            Type = HeaderFooterValues.Default, 
                            Id = strFootrID 
                        };
                        objSectionProperties.Append(objFooterReference);
                        objBody.Append(objSectionProperties);
                        objMainDocumentPart.Document.Append(objBody);
                        DocumentSettingsPart objDocumentSettingPart =
                            objMainDocumentPart.AddNewPart<DocumentSettingsPart>();
                        objDocumentSettingPart.Settings = new Settings();
                        Compatibility objCompatibility = new Compatibility();
                        CompatibilitySetting objCompatibilitySetting =
                            new CompatibilitySetting()
                            {
                                Name = CompatSettingNameValues.CompatibilityMode,
                                Uri = "http://schemas.microsoft.com/office/word",
                                Val = "14"
                            };
                        objCompatibility.Append(objCompatibilitySetting);
                        objDocumentSettingPart.Settings.Append(objCompatibility);
                    }
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Black Santa Friday, August 3, 2012 10:02 PM
    Monday, July 30, 2012 8:28 AM
  • Thanks so much, you´ve saved the day! It works perfectly.

    This is the VB version:

        Private Sub pnum()
            'Dim objSaveFileDialog As New SaveFileDialog()
            'objSaveFileDialog.Filter = "Word Document (*.docx)|*.docx"
            'objSaveFileDialog.ShowDialog()
            Dim strSavePath As String = "pathtodoc"
            If strSavePath.Length > 0 Then
                Using objWordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Create(strSavePath, WordprocessingDocumentType.Document)
                    Dim objMainDocumentPart As MainDocumentPart = objWordprocessingDocument.AddMainDocumentPart()
                    Dim objDocument As New Document()
                    objMainDocumentPart.Document = objDocument
                    Dim objBody As New Body()
                    Dim objSectionProperties As New SectionProperties()
                    Dim objFootPart As FooterPart = objMainDocumentPart.AddNewPart(Of FooterPart)()
                    Dim objFooter As New Footer()
                    objFootPart.Footer = objFooter
                    Dim objSdtBlock_1 As New SdtBlock()
                    Dim objSdtContentBlock_1 As New SdtContentBlock()
                    Dim objSdtBlock_2 As New SdtBlock()
                    Dim objSdtContentBlock_2 As New SdtContentBlock()
                    Dim objParagraph_1 As New Paragraph()
                    Dim objParagraphProperties As New ParagraphProperties()
                    Dim objParagraphStyleId As New ParagraphStyleId() With { _
                      .Val = "Footer" _
                    }
                    objParagraphProperties.Append(objParagraphStyleId)
                    Dim objJustification As New Justification() With { _
                     .Val = JustificationValues.Right _
                    }
                    objParagraphProperties.Append(objJustification)
                    objParagraph_1.Append(objParagraphProperties)
                    Dim objRun_1 As New Run()
                    Dim objText_1 As New Text()
                    objText_1.Text = "Page "
                    objRun_1.Append(objText_1)
                    objParagraph_1.Append(objRun_1)
                    Dim objRun_2 As New Run()
                    Dim objFieldChar_1 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Begin _
                    }
                    objRun_2.Append(objFieldChar_1)
                    objParagraph_1.Append(objRun_2)
                    Dim objRun_3 As New Run()
                    Dim objFieldCode_1 As New FieldCode() With { _
                     .Space = SpaceProcessingModeValues.Preserve _
                    }
                    objFieldCode_1.Text = "PAGE "
                    objRun_3.Append(objFieldCode_1)
                    objParagraph_1.Append(objRun_3)
                    Dim objRun_4 As New Run()
                    Dim objFieldChar_2 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Separate _
                    }
                    objRun_4.Append(objFieldChar_2)
                    objParagraph_1.Append(objRun_4)
                    Dim objRun_5 As New Run()
                    Dim objText_2 As New Text()
                    objText_2.Text = "2"
                    objRun_5.Append(objText_2)
                    objParagraph_1.Append(objRun_5)
                    Dim objRun_6 As New Run()
                    Dim objFieldChar_3 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.[End] _
                    }
                    objRun_6.Append(objFieldChar_3)
                    objParagraph_1.Append(objRun_6)
                    Dim objRun_7 As New Run()
                    Dim objText_3 As New Text()
                    objText_3.Text = "of "
                    objRun_7.Append(objText_3)
                    objParagraph_1.Append(objRun_7)
                    Dim objRun_8 As New Run()
                    Dim objFieldChar_4 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Begin _
                    }
                    objRun_8.Append(objFieldChar_4)
                    objParagraph_1.Append(objRun_8)
                    Dim objRun_9 As New Run()
                    Dim objFieldCode_2 As New FieldCode() With { _
                     .Space = SpaceProcessingModeValues.Preserve _
                    }
                    objFieldCode_2.Text = "NUMPAGES  "
                    objRun_9.Append(objFieldCode_2)
                    objParagraph_1.Append(objRun_9)
                    Dim objRun_10 As New Run()
                    Dim objFieldChar_5 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.Separate _
                    }
                    objRun_10.Append(objFieldChar_5)
                    objParagraph_1.Append(objRun_10)
                    Dim objRun_11 As New Run()
                    Dim objText_4 As New Text()
                    objText_4.Text = "2"
                    objRun_11.Append(objText_4)
                    objParagraph_1.Append(objRun_11)
                    Dim objRun_12 As New Run()
                    Dim objFieldChar_6 As New FieldChar() With { _
                     .FieldCharType = FieldCharValues.[End] _
                    }
                    objRun_12.Append(objFieldChar_6)
                    objParagraph_1.Append(objRun_12)
                    objSdtContentBlock_2.Append(objParagraph_1)
                    objSdtBlock_2.Append(objSdtContentBlock_2)
                    objSdtContentBlock_1.Append(objSdtBlock_2)
                    objSdtBlock_1.Append(objSdtContentBlock_1)
                    objFooter.Append(objSdtBlock_1)
                    Dim strFootrID As String = objMainDocumentPart.GetIdOfPart(objFootPart)
                    Dim objFooterReference As New FooterReference() With { _
                     .Type = HeaderFooterValues.[Default], _
                     .Id = strFootrID _
                    }
                    objSectionProperties.Append(objFooterReference)
                    objBody.Append(objSectionProperties)
                    objMainDocumentPart.Document.Append(objBody)
                    Dim objDocumentSettingPart As DocumentSettingsPart = objMainDocumentPart.AddNewPart(Of DocumentSettingsPart)()
                    objDocumentSettingPart.Settings = New Settings()
                    Dim objCompatibility As New Compatibility()
                    Dim objCompatibilitySetting As New CompatibilitySetting() With { _
                     .Name = CompatSettingNameValues.CompatibilityMode, _
                     .Uri = "http://schemas.microsoft.com/office/word", _
                     .Val = "14" _
                    }
                    objCompatibility.Append(objCompatibilitySetting)
                    objDocumentSettingPart.Settings.Append(objCompatibility)
                End Using
            End If
        End Sub

    • Marked as answer by Black Santa Friday, August 3, 2012 10:06 PM
    Friday, August 3, 2012 10:05 PM
  • Hi, Tom,

    I tried to use you code snippet (just change .Create to .Open) to add page number to an existing DOCX document and got the following error:

    Only one instance of the type is allowed for this parent.
    StackTrace:
       at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.InitPart[T](T newPart, String contentType, String id)
       at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.InitPart[T](T newPart, String contentType)
       at DocumentFormat.OpenXml.Packaging.WordprocessingDocument.AddMainDocumentPart()

    Any help would be appreciated.

    Jim Feng

    Friday, November 22, 2013 5:03 PM
  • Hi Jim

    You provide too little information. We'd need to know which line of code is triggering the error.

    Most likely, you're having the problem because you're opening an existing document, while Tom's code is creating a new document. Creating a document from scratch using Open XML SDK isn't like using the Word object model's Documents.Add method: you don't start out with a basic document. Everything has to be done from scratch, and since certain things are already present in an existing document and can be present only once, you're not allowed to add those things. I'm guessing that's what's happening, here...


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, November 23, 2013 3:18 PM