none
Word: inserting page x of y

    Question

  • Hi,
    I have made a table in my header, and in one of the cells, i want to insert the autotext: "Page x of y".
    I have not been able to do this, though. I have tried many examples i have found, but whenever i use myRange.Fields.Add(), i get a com-exception saying: "This command is not available."

    I am using Word2007.
    How on earth do i insert this?

    Nicolai Søndergaard LM Glasfiber A/S
    Wednesday, February 10, 2010 1:57 PM

Answers

  • Hello again Nicoolai,

    As Cindy said in the above link, AutoText in Word 2007 has probably been moved to a building. But you should be able to use this as the framework for indserting the two fields directly.

    For test, in my side, I have made a sample by create a Word 2007 Document project. And the code like this, 

      Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            Dim footer As Word.Range = Me.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
            footer.Fields.Add(footer, Word.WdFieldType.wdFieldNumPages, , False)
            footer.InsertBefore(" of ")
            footer.Collapse(Word.WdCollapseDirection.wdCollapseStart)
            footer.Fields.Add(footer, Word.WdFieldType.wdFieldPage, , False)
        End Sub

    Also, even if we could not add this AutoText directly, the add method should be not thrown an exception as above. So, I am thinkink it may be related to this myRange. Please check this code in your side, and see if there is a difference between us.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Nicoolai Tuesday, February 16, 2010 10:28 AM
    • Unmarked as answer by Nicoolai Tuesday, February 16, 2010 11:29 AM
    • Marked as answer by Nicoolai Wednesday, February 17, 2010 7:08 AM
    Tuesday, February 16, 2010 3:47 AM
  • Hello again Nicoolai,

    For this, please check out this thread: http://social.msdn.microsoft.com/forums/en-US/vsto/thread/5b945c0c-9454-4e22-9b4a-8081fc4a9378, where Cindy gives a total description.

    If you have any further question for this, please feel free to follow up.

    Best regards,
    Bessie
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Nicoolai Wednesday, February 17, 2010 7:08 AM
    Wednesday, February 17, 2010 2:47 AM

All replies

  • Hello,

    To insert page numbers to a document, basically, we could use Fields.Add. However, I think it is no need to create a table for this. Here is a thread where you could get an answer: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f73f2019-edb6-4d9d-a5a0-3de5ddbdc859/

    Another way is to use code in this resource, http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx. The code sample focuses on the footer of document. We just need to make a slight change with this line: this.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader

    If you have any concern for this, please feel free to follow up.

    Best regards,
    Bessie
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, February 12, 2010 2:39 AM
  • I assume it doesn't matter if i do it in a table or not.
    This is my function, which gives me the comexception: This command is not available.
            private void InsertPageNumbers(MSWord.Range myRange)
            {
                Object oTrue = true;
                Object CurrentPage = MSWord.WdFieldType.wdFieldEmpty;
                Object oPages = "AUTOTEXT  \"\"Page X of Y\"\" ";
                myRange.Fields.Add(myRange, ref CurrentPage, ref oPages, ref oTrue);
            }


    Nicolai Søndergaard LM Glasfiber A/S
    Monday, February 15, 2010 8:55 AM
  • Hello again Nicoolai,

    As Cindy said in the above link, AutoText in Word 2007 has probably been moved to a building. But you should be able to use this as the framework for indserting the two fields directly.

    For test, in my side, I have made a sample by create a Word 2007 Document project. And the code like this, 

      Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            Dim footer As Word.Range = Me.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
            footer.Fields.Add(footer, Word.WdFieldType.wdFieldNumPages, , False)
            footer.InsertBefore(" of ")
            footer.Collapse(Word.WdCollapseDirection.wdCollapseStart)
            footer.Fields.Add(footer, Word.WdFieldType.wdFieldPage, , False)
        End Sub

    Also, even if we could not add this AutoText directly, the add method should be not thrown an exception as above. So, I am thinkink it may be related to this myRange. Please check this code in your side, and see if there is a difference between us.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Nicoolai Tuesday, February 16, 2010 10:28 AM
    • Unmarked as answer by Nicoolai Tuesday, February 16, 2010 11:29 AM
    • Marked as answer by Nicoolai Wednesday, February 17, 2010 7:08 AM
    Tuesday, February 16, 2010 3:47 AM
  • It appears that you correct. If i pick a different range (like the entire footer) it seems to work.
    Thanks, i will sort out my range.

    Nicolai Søndergaard LM Glasfiber A/S
    Tuesday, February 16, 2010 10:28 AM
  • Sorry Bessie, i have unmarked your reply.
    It seems that whenever i use a table.cell(x,y).range, i get my com-exception.
    How on earth do i insert this into a table then?

    Nicolai Søndergaard LM Glasfiber A/S
    Tuesday, February 16, 2010 11:29 AM
  • Hello again Nicoolai,

    For this, please check out this thread: http://social.msdn.microsoft.com/forums/en-US/vsto/thread/5b945c0c-9454-4e22-9b4a-8081fc4a9378, where Cindy gives a total description.

    If you have any further question for this, please feel free to follow up.

    Best regards,
    Bessie
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Nicoolai Wednesday, February 17, 2010 7:08 AM
    Wednesday, February 17, 2010 2:47 AM
  • Excellent, thank you!
    Nicolai Søndergaard LM Glasfiber A/S
    Wednesday, February 17, 2010 7:08 AM