none
Force updating numPages field using c# RRS feed

  • Question

  • Hello,

    I'm using word 2010.

    I put the numPages field in the fotter and it is not updated each time the num of pages in the doc are changed.

    I wonder if there is any way to force this field to recalc using c#.

    Thanks,

    Orit Tevy

    Wednesday, June 27, 2012 11:39 AM

Answers

  • Hi,

    I inserted this field NumPages to header and implemented "Page xx of {NUMPAGES}", code like below, for your information. If you could not figure out what's the problem, please share code snippet how you insert field code.

     // Creates an Header instance and adds its children.
            public Header GenerateHeader()
            {
                Header header1 = new Header(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 wp14" }  };
                header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
                header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
                header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
                header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
                header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
                header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
                header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
                header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
                header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
                header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
                header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
                header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
                header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
                header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
                Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00733BC8", RsidRunAdditionDefault = "00733BC8" };
                ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Header" };
                paragraphProperties1.Append(paragraphStyleId1);
                ProofError proofError1 = new ProofError(){ Type = ProofingErrorValues.GrammarStart };
                Run run1 = new Run();
                Text text1 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
                text1.Text = "Page  ";
                run1.Append(text1);
                ProofError proofError2 = new ProofError(){ Type = ProofingErrorValues.GrammarEnd };
                Run run2 = new Run();
                FieldChar fieldChar1 = new FieldChar(){ FieldCharType = FieldCharValues.Begin };
                run2.Append(fieldChar1);
                Run run3 = new Run();
                FieldCode fieldCode1 = new FieldCode(){ Space = SpaceProcessingModeValues.Preserve };
                fieldCode1.Text = " PAGE  \\* MERGEFORMAT ";
                run3.Append(fieldCode1);
                Run run4 = new Run();
                FieldChar fieldChar2 = new FieldChar(){ FieldCharType = FieldCharValues.Separate };
                run4.Append(fieldChar2);
                Run run5 = new Run(){ RsidRunAddition = "000C154F" };
                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();
                runProperties1.Append(noProof1);
                Text text2 = new Text();
                text2.Text = "1";
                run5.Append(runProperties1);
                run5.Append(text2);
                Run run6 = new Run();
                FieldChar fieldChar3 = new FieldChar(){ FieldCharType = FieldCharValues.End };
                run6.Append(fieldChar3);
                Run run7 = new Run();
                Text text3 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
                text3.Text = " of  ";
                run7.Append(text3);
                SimpleField simpleField1 = new SimpleField(){ Instruction = " NUMPAGES   \\* MERGEFORMAT " };
                Run run8 = new Run(){ RsidRunAddition = "000C154F" };
                RunProperties runProperties2 = new RunProperties();
                NoProof noProof2 = new NoProof();
                runProperties2.Append(noProof2);
                Text text4 = new Text();
                text4.Text = "2";
                run8.Append(runProperties2);
                run8.Append(text4);
                simpleField1.Append(run8);
                paragraph1.Append(paragraphProperties1);
                paragraph1.Append(proofError1);
                paragraph1.Append(run1);
                paragraph1.Append(proofError2);
                paragraph1.Append(run2);
                paragraph1.Append(run3);
                paragraph1.Append(run4);
                paragraph1.Append(run5);
                paragraph1.Append(run6);
                paragraph1.Append(run7);
                paragraph1.Append(simpleField1);
                header1.Append(paragraph1);
                return header1;
            }

    thanks,

    Forrest Guo | MSDN Community Support | Feedback to manager

    Tuesday, July 10, 2012 3:48 PM
    Moderator

All replies

  • Hi,

    Thanks  for posting in the MSDN Forum.

    Based on your issue, I would suggest a method of updating the NumPages field in Word. When the number of pages in the document is changed, call the method. The code of the method is

    public partial class ThisDocument
        {
            private void UpdateNumPagesField()
            {
                foreach (Word.Field field in this.Fields)
                {
                    if (field.Type == Word.WdFieldType.wdFieldNumPages)
                    {
                        field.Select();
                        field.Update();
                    }
                }
                this.Save();
    
            }
    }

    Hope this helps.

    If anything is unclear, please feel free to let me know.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Friday, June 29, 2012 5:41 AM
    Moderator
  • Hi Leo,

    Sorry for the late reply, 

    I forgot to mention that I'm using open xml in my code.

    Is there any way to do that in WordprocessingDocument.

    Best Regards,

    Orit

    Wednesday, July 4, 2012 6:19 AM
  • Hi Orit,

    As far as I know, you can refer to this UpdateFieldsOnOpen Class from DocumentFormat.OpenXml.WordprocessingDocument to resolve your issue.

    Thanks,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 4, 2012 8:40 AM
    Moderator
  • Hi,

    I tried it and it didn't help.

    Any other idea?

    Thanks,

    Orit

    Wednesday, July 4, 2012 10:59 AM
  • Hi Orit,

    I'll involve some experts who are familiar with your issue, this may take some time. Much appreciate for your patient and understanding.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us


    Friday, July 6, 2012 7:52 AM
    Moderator
  • I forgot to mention that I'm using open xml in my code.

    Is there any way to do that in WordprocessingDocument.


    In that case this question is better asked in the OPen XML SDK forum. The Word for Developers forum targets the Word object model (API), not OPen XML. I'm going to move the discussion to the Open XML SDK forum.

    Cindy Meister, VSTO/Word MVP

    Friday, July 6, 2012 8:41 AM
    Moderator
  • Hi,

    I inserted this field NumPages to header and implemented "Page xx of {NUMPAGES}", code like below, for your information. If you could not figure out what's the problem, please share code snippet how you insert field code.

     // Creates an Header instance and adds its children.
            public Header GenerateHeader()
            {
                Header header1 = new Header(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 wp14" }  };
                header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
                header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
                header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
                header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
                header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
                header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
                header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
                header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
                header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
                header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
                header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
                header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
                header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
                header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
                Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00733BC8", RsidRunAdditionDefault = "00733BC8" };
                ParagraphProperties paragraphProperties1 = new ParagraphProperties();
                ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Header" };
                paragraphProperties1.Append(paragraphStyleId1);
                ProofError proofError1 = new ProofError(){ Type = ProofingErrorValues.GrammarStart };
                Run run1 = new Run();
                Text text1 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
                text1.Text = "Page  ";
                run1.Append(text1);
                ProofError proofError2 = new ProofError(){ Type = ProofingErrorValues.GrammarEnd };
                Run run2 = new Run();
                FieldChar fieldChar1 = new FieldChar(){ FieldCharType = FieldCharValues.Begin };
                run2.Append(fieldChar1);
                Run run3 = new Run();
                FieldCode fieldCode1 = new FieldCode(){ Space = SpaceProcessingModeValues.Preserve };
                fieldCode1.Text = " PAGE  \\* MERGEFORMAT ";
                run3.Append(fieldCode1);
                Run run4 = new Run();
                FieldChar fieldChar2 = new FieldChar(){ FieldCharType = FieldCharValues.Separate };
                run4.Append(fieldChar2);
                Run run5 = new Run(){ RsidRunAddition = "000C154F" };
                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();
                runProperties1.Append(noProof1);
                Text text2 = new Text();
                text2.Text = "1";
                run5.Append(runProperties1);
                run5.Append(text2);
                Run run6 = new Run();
                FieldChar fieldChar3 = new FieldChar(){ FieldCharType = FieldCharValues.End };
                run6.Append(fieldChar3);
                Run run7 = new Run();
                Text text3 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
                text3.Text = " of  ";
                run7.Append(text3);
                SimpleField simpleField1 = new SimpleField(){ Instruction = " NUMPAGES   \\* MERGEFORMAT " };
                Run run8 = new Run(){ RsidRunAddition = "000C154F" };
                RunProperties runProperties2 = new RunProperties();
                NoProof noProof2 = new NoProof();
                runProperties2.Append(noProof2);
                Text text4 = new Text();
                text4.Text = "2";
                run8.Append(runProperties2);
                run8.Append(text4);
                simpleField1.Append(run8);
                paragraph1.Append(paragraphProperties1);
                paragraph1.Append(proofError1);
                paragraph1.Append(run1);
                paragraph1.Append(proofError2);
                paragraph1.Append(run2);
                paragraph1.Append(run3);
                paragraph1.Append(run4);
                paragraph1.Append(run5);
                paragraph1.Append(run6);
                paragraph1.Append(run7);
                paragraph1.Append(simpleField1);
                header1.Append(paragraph1);
                return header1;
            }

    thanks,

    Forrest Guo | MSDN Community Support | Feedback to manager

    Tuesday, July 10, 2012 3:48 PM
    Moderator