|
// *********************************************************
// リストボックスにExcelシートの一覧を追加
// 戻り値 : 成功 true, 失敗 false
// ※ Excel.Application 版
// *********************************************************
#import "C:\Program Files\Microsoft Office\Office\MSO9.DLL"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
#import "EXCEL9.OLB" \
rename("DialogBox", "DialogBoxXL") \
rename("RGB", "RBGXL") \
rename "DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces
using namespace Excel;
BOOL LboxEnumExcelSheet( HWND hList, int nIndex, LPCTSTR szFile )
{
if ( *szFile == 0x00 ) {
LboxListInsert( hList, nIndex, "*ERROR\tファイル名を指定して下さい" );
return false;
}
if ( !PathFileExists( szFile ) ) {
LboxListInsert( hList, nIndex, "*ERROR\tファイルが存在しません" );
return false;
}
LPTSTR lpExt;
lpExt = PathFindExtension( szFile );
if ( *lpExt != '.' ) {
LboxListInsert( hList, nIndex, "*ERROR\tExcelではありません" );
return false;
}
if( StrStrI( lpExt, "xls" ) == NULL ) {
LboxListInsert( hList, nIndex, "*ERROR\tExcelではありません" );
return false;
}
BOOL bRet;
_ApplicationPtr pXL;
WorkbooksPtr pBooks;
_WorkbookPtr pBook;
WorksheetsPtr pSheets;
_WorksheetPtr pSheet;
_bstr_t StringBuffer("");
StringBuffer.operator = (szFile);
long i,nCount;
_variant_t nIdx;
nIdx.vt = VT_I4;
char *szBuffer;
CoInitialize(NULL);
try {
pXL.CreateInstance(L"Excel.Application");
pXL->Visible = VARIANT_FALSE;
pBooks = pXL->Workbooks;
pBook = pBooks->Open( StringBuffer );
pSheets = pBook->Sheets;
nCount = pSheets->GetCount();
for( i = 1; i <= nCount; i++ ) {
nIdx.lVal = i;
pSheet = pSheets->GetItem( &nIdx );
StringBuffer.operator = (pSheet->Name);
szBuffer = StringBuffer.operator char * ();
LboxListInsert( hList, nIndex, szBuffer );
nIndex++;
}
pXL->Quit();
pXL.Release();
pXL = NULL;
bRet = true;
}
catch (_com_error &e)
{
LboxListInsert( hList, nIndex, e.ErrorMessage() );
bRet = false;
}
CoUninitialize();
return bRet;
}
| |