Compté le nombre de pages d'un document excel
Bonjour j'essaie de compter le nombre des pages dans un document excel. Voici le code que j'ai mis au point:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
using MSOExcel2003 = Microsoft.Office.Interop.Excel;
using MSOWord2003 = Microsoft.Office.Interop.Word;
//...
public uint GetPageCount()
{
MSOExcel2003.ApplicationClass oMSOExcel2003Application;
MSOExcel2003.Workbook oMSOExcel2003Workbook;
MSOExcel2003.Sheets oMSOExcel2003Sheets;
MSOWord2003.ApplicationClass oMSOWord2003Application;
MSOWord2003.Document oMSOWord2003Document;
object oParamFileName;
object oParamMissing;
object oParamFalse;
object oParamTrue;
uint uiPageCount;
oParamMissing = System.Reflection.Missing.Value;
oParamFalse = false;
oParamTrue = true;
uiPageCount = 0;
switch (m_eInputDocType)
{
case EDocumentType.MSOExcel2003:
oMSOExcel2003Application = new MSOExcel2003.ApplicationClass();
oMSOExcel2003Application.Visible = false;
oMSOExcel2003Workbook = oMSOExcel2003Application.Workbooks.Open(m_oInput.Folder + Path.DirectorySeparatorChar + m_oInput.Filename, oParamMissing, oParamTrue, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing, oParamMissing);
for (int i = 0; i < oMSOExcel2003Workbook.Worksheets.Count; i++)
{
oMSOExcel2003Sheets = (MSOExcel2003.Sheets)oMSOExcel2003Workbook.Sheets[i];
uiPageCount += 1 + (uint)oMSOExcel2003Sheets.HPageBreaks.Count;
uiPageCount += (uint)oMSOExcel2003Sheets.VPageBreaks.Count;
} |
Mon problème c'est lorsque je parcours la collection avec
Code:
oMSOExcel2003Sheets = (MSOExcel2003.Sheets)oMSOExcel2003Workbook.Sheets[i];
Une exception est lancé par le module COM.
Citation:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Headhunter.exe
Additional information: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
Du coups, je suis bloqué et je vois mal comment résoudre cette impasse. Notez que j'ai également essayé avec
Code:
oMSOExcel2003Sheets = (MSOExcel2003.WorkSheets)oMSOExcel2003Workbook.WorkSheets[i];
Mais sans succès.
Si vous voyez une façon de résoudre ma problèmatique merci de m'informer, je suis un peut bloquer par ce bug :|
Merci et bonne journée!