Excel Automation: any way to find out how many pages will be printed per sheet?

Several times, the sheets are too large to be printed, so I would like to avoid printing if that is the case.

My current program:

  • Opens an excel document
  • Copies the first sheet
  • Creates a new document
  • Paste text and formulas into a new document
  • Automatically validates columns when displaying text
  • Print text (not yet implemented)
  • Makes columns auto-validate when displaying formulas
  • Print Formulas (not yet implemented)

The reason for copying / pasting is the ability to automatically prepare columns, even if the document / sheet / cell is password protected.

But I would like to avoid printing it if, for example, the current sheet fills more than 100 pages. How can I check this?

        // Open Excel
        xlApp = new Excel.ApplicationClass();
        xlApp.Visible = true;

        // Open document
        xlWorkBook = xlApp.Workbooks.Open(filename, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

        // Select the sheet we want to copy
        Excel.Sheets xlSheets = null;
        xlSheets = xlWorkBook.Sheets as Excel.Sheets;
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        // Mark all cells and copy
        xlWorkSheet.UsedRange.Copy(misValue);

        // Make a new empty document
        Excel.Workbook xlWorkBook2;
        xlWorkBook2 = xlApp.Workbooks.Add(misValue);

        // Select the first sheet and insert
        Excel.Worksheet xlWorkSheet2;
        xlWorkSheet2 = (Excel.Worksheet)xlWorkBook2.Worksheets.get_Item(1);
        xlWorkSheet2.Name = "temp";

        // Just copies to range starting at cell A1
        Excel.Range range = (Excel.Range)((Excel.Worksheet)xlWorkSheet2).Cells[1, 1];
        range.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, misValue, misValue);
        range.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, misValue, misValue);

        // Adjust width of cells (autofit must be called on a column)
        xlWorkSheet2.Columns.AutoFit();

        // Show formulas
        xlWorkSheet2.Application.ActiveWindow.DisplayFormulas = true;
        xlWorkSheet2.Columns.AutoFit();

        xlWorkSheet2.PrintPreview(misValue);

        // Close Excel
        xlWorkBook2.Close(false, false, misValue);
        xlWorkBook.Close(false, false, misValue);
        xlApp.Quit();
+3
1

. :

int numberOfPages = xlWorkSheet2.PageSetup.Pages.Count;

.

+3

All Articles