1

I'm trying to access a page in Word by it's name or number. I thought I was going in the right direction but there doesn't seem to be a page.name or page.number property. The issue is in my if statement where I'm trying to say if there is a page named Page 4 Content then select it.

    var wordApplication = (word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
    string path = CommonMethods.getFile(fileName);

    myDoc = wordApplication.Documents.Open(path);
    wordApplication.Visible = true;
    wordApplication.WindowState = word.WdWindowState.wdWindowStateMaximize;


    word.Pages pages = myDoc.ActiveWindow.ActivePane.Pages;

    foreach (word.Page p in pages )
    {
        if (p.)
        {

        }
    }
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • 1
    Hi... have a look at this answer https://stackoverflow.com/questions/28987095/get-pages-of-word-document – Wheels73 Jun 08 '17 at 08:27
  • I'm assuming `pages` has a collection of `page` objects ( a list or array?). If so, you should be able to access a specific page number by using it's index; e.g. something like `pages.Item[0]` – T_Bacon Jun 08 '17 at 08:30
  • @T_Bacon pages is a collection I tried doing something like this `for (int i = 0; i < pages.Count; i++) { pages[i]; }` However it causes an error. I wanted to do something like this `pages[i].select` but there is no property called select – Craig Gallagher Jun 08 '17 at 08:37
  • 1
    Yes, but you need to use the `Item` property of the `Pages` object to return a specific object from the collection. So, as in my comment above, you should use `pages.Item[i]` to get the specific page object you are after. However, looking at the docs, I don't think the `Page` object offers any `Select` functionality. It looks like you need to use a `Range` for this. See https://stackoverflow.com/questions/11806763/select-page-in-docx-using-c-sharp. – T_Bacon Jun 08 '17 at 10:14

2 Answers2

2

As you already mentioned there is no number or name property on the page object.

In order to get the page number you have to access the Information property of a Range or Selection object on that page.

In addition to that I recommend to study the article Selecting or referring to a page in the Word object model by Shauna Kelly. In her article she explains in detail why it is often not a good idea to rely on the page object for automated document processing. The reason for that is that Word uses a flow layout instead of a fixed layout. In order to determine the current page rendering Word has to talk to the current printer driver. This means that your page breaks may vary depending on your printer.

Andre Kraemer
  • 2,633
  • 1
  • 17
  • 28
0

I ended up doing the following and it works like a charm.

        object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
        object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
        object Miss = System.Reflection.Missing.Value;
        word.Pages pages = doc.ActiveWindow.ActivePane.Pages;

        for (int i = 0; i < pages.Count; i++)
        {

            if (i == pageNumber)
            {
                doc.Application.Selection.GoTo(ref What, ref Which, pageNumber, ref Miss);
            }



        }
    }
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52