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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include "stdafx.h"
#include "afxwin.h"
#include <conio.h>
#define USE_PROGID 1
#define USE_LIBID 0
#if USE_PROGID #import "progid:Excel.Sheet" auto_search auto_rename rename_search_namespace("Office10")
#elif USE_LIBID #import "libid:{00020813-0000-0000-C000-000000000046}" auto_search auto_rename version(1.3) lcid(0) no_search_namespace #else
#pragma message ("Make sure the path to excel.exe is correct") #import "c:\office\office10\excel.exe" auto_search auto_rename dual_interfaces
#endif
BOOL Fichier_OK;
void dump_com_error(_com_error &e)
{
Fichier_OK = FALSE;
_tprintf(_T("\tFICHIER HS !\n\n"));
_tprintf(_T("\tCode = %08lx\n"), e.Error());
_tprintf(_T("\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
struct StartOle
{
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
UINT MyThreadProc(LPVOID pParam)
{
HWND hwnd;
HWND hwnd1;
HWND hwnd2;
char pszBuffer[513];
HWND g_HwndWordDlg = 0;
hwnd = FindWindow("XLMAIN", NULL);
_tprintf(_T("\tHandle parent= %d\n"), hwnd);
hwnd1 = GetWindow(hwnd,GW_CHILD);
_tprintf(_T("\tHandle child = %d\n"), hwnd1);
GetClassName(hwnd1,pszBuffer,512);
_tprintf(_T("\tLe class name de cette fenetre CHILD est : %s\r\n"), (LPCTSTR) pszBuffer); hwnd2 = GetWindow(hwnd,GW_ENABLEDPOPUP);
_tprintf(_T("\tHandle POPUP = %d\n"), hwnd2);
GetClassName(hwnd2,pszBuffer,512);
_tprintf(_T("\tLe class name de cette fenetre POPUP est : %s\r\n"), (LPCTSTR) pszBuffer);
if ((hwnd2!=0) || (hwnd1==0))
{
if (!strcmp(pszBuffer,"#32770"))
{
_tprintf(_T("\tLe fichier est HS (pas de header xls)\r\n"));
}
if (!strcmp(pszBuffer,"bosa_sdm_XL9"))
{
_tprintf(_T("\tLe fichier est protege par mot de passe\r\n"));
}
PostMessage(hwnd2, WM_KEYDOWN, VK_ESCAPE, 0);
PostMessage(hwnd2, WM_KEYUP, VK_ESCAPE, 0);
Fichier_OK = FALSE;
}
return 0;
}
void main()
{
using namespace Excel;
_ApplicationPtr pXL;
CWinThread* pRunningThread = NULL;
HANDLE hfind;
WIN32_FIND_DATA wfd;
char szroot[MAX_PATH],temporaire[MAX_PATH];
pXL.CreateInstance(L"Excel.Application");
pXL->Visible[0] = VARIANT_TRUE;
WorkbooksPtr pBooks = pXL->Workbooks;
strcpy( szroot, "c:\\*.xls"); //Je teste tous les .xls à la racine de C:
hfind = FindFirstFile(szroot, &wfd);
if(hfind == INVALID_HANDLE_VALUE)
{
FindClose(hfind);
exit;
}
do
{
strcpy( temporaire, "c:\\");
strcat( temporaire, wfd.cFileName );
_tprintf(_T("Open = %s\r\n"), temporaire);
Fichier_OK = TRUE; //initialise la variable booleene
pRunningThread = AfxBeginThread(MyThreadProc, NULL);
try
{
pXL->Workbooks->Open(temporaire);
::WaitForSingleObject(pRunningThread->m_hThread, 6000);
}
catch(_com_error &e)
{
dump_com_error(e);
}
if (Fichier_OK==TRUE)
{
_tprintf(_T("FICHIER OK : %s\r\n\r\n"), temporaire);
}
else
{
_tprintf(_T("FICHIER HS : %s\r\n\r\n"), temporaire);
}
Sleep(100);
pXL->Workbooks->Close(1);
} while(FindNextFile(hfind, &wfd));
FindClose(hfind);
pXL->Quit();
getch();
} |
Partager