none
How to determine the page number in word 2010 through C# RRS feed

  • Question

  • How to determine the Current page number in word 2010 through C#?.   I need to add the page number in a bar code in the header of each page.   The number of pages may vary and is not known before the document is generated.

    Leonard Swarczinski Software Developer Postal Center International

    Tuesday, July 30, 2013 9:30 PM

All replies

  • Hi Leonard,

    Below is a sample about how to go through the whole word document and get the current page number:

            private void button1_Click(object sender, EventArgs e)
            {
                var app = Globals.ThisAddIn.Application;
    
                long currentPageNum;
    
                long totalPageNum = app.ActiveDocument.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages);
    
                do
                {
                    currentPageNum = app.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndPageNumber);
    
                    app.Selection.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);
    
                    // add your code here
                } while (currentPageNum < totalPageNum);
            }

    Regards,

    Jeffrey


    Jeffrey Chen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 31, 2013 2:51 PM
    Moderator
  • Thank you Jeffrey.

    This  is very useful.

    How can I locate a textbox shape in the header of the current page.  I need to insert a barcode that includes the page number.


    Leonard Swarczinski Software Developer Postal Center International

    Wednesday, July 31, 2013 7:57 PM
  • Better solution would be using a field code {PAGE}

    Regards,

    Michael

    Wednesday, July 31, 2013 9:50 PM
  • Thank you Michael

    If I used {PAGE} in the Header, can I extract its range so I can replace it with a barcode?


    Leonard Swarczinski Software Developer Postal Center International

    Wednesday, July 31, 2013 10:25 PM
  • I think it could be done, Leonard.

    Here is  a general idea on how I would approach it:

    1 Get the header range

    2 Loop through each paragraph in that range

    3 Loop through each field in the paragraph.Range.Fields to find {PAGE} field.

    4 If found, insert your barcode an remove the field.

    Regards,

    Michael

     

    Thursday, August 1, 2013 6:49 AM
  • I tried the following code.  While it appears to walk through the documents headers, it does not locate the text.

    The text is enclosed within a text box on the side margin of the headers.

       

    internal void WalkHeaders(ref Word.Document aDoc, ref Word.Application wordApp, List<Variable> VarList, string LetterType, string version)

        {

       

         

    object oMissing = Missing.Value;

         

    int NumberOfPages = aDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, ref oMissing);

          NumberOfPages++;

          wordApp.Browser.Target = Word.

    WdBrowseTarget.wdBrowseHeading;

         

    for (int i = 1; i <= NumberOfPages * 2; i++)

          {

            wordApp.Browser.Next();

            Word.

    Range rng = wordApp.Selection.Range;

           

           

    for (int ii = 1; ii <= rng.Paragraphs.Count; ii++)

            {

         Word.

    Paragraph para = rng.Paragraphs[ii];

              

    if (para.Range.Text.Contains("{PAGE}"))

               {

                 para.Range.Text =

    "Barcode";

               }

            }

          }


    Leonard Swarczinski Software Developer Postal Center International

    Tuesday, August 6, 2013 9:03 PM
  • Hi Leonard,

    You have to examine text boxes individually. If you are sure that the page numbers are actually in text boxes and the frame you might see around the page number is just not the field frame. This VBA code might help you.

    Sub findHeaderPageTextBox()
        Dim doc As Word.Document, rng As Word.Range, shp As Word.Shape
        Dim hdft As Word.HeaderFooter, sec As Word.Section, fld As Word.Field
        Set doc = ActiveDocument
        For Each sec In doc.Sections
            For Each hdft In sec.Headers
                If hdft.Shapes.Count > 0 Then
                     For Each shp In hdft.Shapes
                        If shp.Type = msoTextBox Then
                            If shp.TextFrame.TextRange.Fields.Count > 0 Then
                                For Each fld In shp.TextFrame.TextRange.Fields
                                    If fld.Type = wdFieldPage Then Debug.Print fld.result
                                Next fld
                            End If
                        End If
                    Next shp
                End If
            Next hdft
        Next sec
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, August 6, 2013 10:26 PM