Search code examples
c#ms-wordinteropoffice-interop

How to get starting page number of a word document?


I want to print only first 10 pages of a word document to PDF/TIFF images. I was able to print the word document using following code.

    Microsoft.Office.Interop.Word.Application WordApp = null;
    Microsoft.Office.Interop.Word.Documents WordDocs = null;
    Microsoft.Office.Interop.Word.Document WordDoc = null;

    WordApp = new Microsoft.Office.Interop.Word.Application();
    WordDocs = WordApp.Documents;

    object MissingValue = System.Reflection.Missing.Value;

    object fileName = @"file path here", oConfirmConversions = false, oReadOnly = true, oAddToRecentFiles = false, oRevert = true, oVisible = true, oOpenAndRepair = true, oNoEncodingDialog = true;

    try
    {
        WordDoc = WordDocs.OpenNoRepairDialog(ref fileName, ref oConfirmConversions, ref oReadOnly, ref oAddToRecentFiles, ref MissingValue, ref MissingValue,
            ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref oVisible, ref oOpenAndRepair, ref MissingValue,
            ref oNoEncodingDialog, ref MissingValue);

        object oBackground = false, oRange = WdPrintOutRange.wdPrintFromTo, oFrom = "1", oTo = "10", oCopies = 1, oPageType = 0, oPrintToFile = false, oItem = 0;

        WordDoc.PrintOut(ref oBackground, ref MissingValue, ref oRange, ref MissingValue, ref oFrom, ref oTo, ref oItem, ref oCopies, ref MissingValue,
            ref oPageType, ref oPrintToFile, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
            ref MissingValue);


    }
    catch (COMException e)
    {
    }
    finally
    {
        try
        {
            if (WordDoc != null)
                Marshal.FinalReleaseComObject(WordDoc);
            if (WordDocs != null)
                Marshal.FinalReleaseComObject(WordDocs);
            if (WordApp != null)
                Marshal.FinalReleaseComObject(WordApp);
        }
        catch { }

        System.GC.Collect();
        System.GC.WaitForPendingFinalizers();
        System.GC.Collect();
        System.GC.WaitForPendingFinalizers();
    }

However when I test on a word document with the starting page other than 1, I didn't get the expected result. So, I realize I should not print out the document starting from 1. Instead I need to use the starting page number.

So, how do I get the starting pagenumber for the word document?

I have really hard time looking into the documentation provided in here. https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word


Solution

  • I got the answer I needed. thanks to the post How to change the starting page number in word in microsoft interop? At first the solution was not so clear as I was looking to get the index 0 of sections. But found that index starts from 1 not 0.

    Working code

    public int getStartingNumber(Document WordDoc)
    {
        var sections = WordDoc.Sections;
        for (int i = 1; i <= sections.Count; i++)
        {
            var section = sections[i];
            var headers = section.Headers;
            foreach (HeaderFooter headerfooter in headers)
            {
                var pageNumbers = headerfooter.PageNumbers;
                return pageNumbers.StartingNumber;
            }
        }
        return -1;
    }