EternalWindows
�I�[�g���[�V���� / �R���N�V�����I�u�W�F�N�g

Office�A�v���P�[�V�������쐬����I�u�W�F�N�g�ɂ́A�R���N�V�����I�u�W�F�N�g�ƌĂ΂���ނ�����܂��B �R���N�V�����I�u�W�F�N�g�́A�����̃I�u�W�F�N�g���Ǘ�����I�u�W�F�N�g�̂��ƂŁA ���Ƃ���Word��Documents�I�u�W�F�N�g��Document�I�u�W�F�N�g���A Excel��Workbooks�I�u�W�F�N�g��Workbook�I�u�W�F�N�g���Ǘ����Ă��܂��B �܂��A�R���N�V�����I�u�W�F�N�g�ɂ͕K�{�����ƂȂ��Ă���v���p�e�B������A Count�v���p�e�B�͊Ǘ����Ă���I�u�W�F�N�g�̐���Ԃ��A Item�v���p�e�B�͎w�肳�ꂽ�C���f�b�N�X�Ŏ��ʂ����I�u�W�F�N�g��Ԃ��܂��B

����̃v���O�����́AExcel�̃��[�N�u�b�N�Ɋ܂܂�郏�[�N�V�[�g�̖��O��\�����܂��B ���[�N�V�[�g��3�‘��݂���̂ł���΁A3�񖼑O���\������邱�ƂɂȂ�܂��B ���[�N�u�b�N�́Ac:\\sample.xls�ł���Ɖ��肵�Ă��܂��B

#include <windows.h>

HRESULT Invoke(IDispatch *pDispatch, LPOLESTR lpszName, WORD wFlags, VARIANT *pVarArray, int nArgs, VARIANT *pVarResult);

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
{
	IDispatch *pApplication;
	IDispatch *pWorkbooks;
	IDispatch *pWorkbook;
	IDispatch *pWorksheets;
	IDispatch *pWorksheet;
	LONG      i;
	LONG      lCount;
	CLSID     clsid;
	HRESULT   hr;
	VARIANT   var;
	VARIANT   varResult;

	CoInitialize(NULL);
	
	hr = CLSIDFromProgID(L"Excel.Application", &clsid);
	if (FAILED(hr)) {
		CoUninitialize();
		return 0;
	}
	
	hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&pApplication));
	if (FAILED(hr)) {
		CoUninitialize();
		return 0;
	}
	
	var.vt = VT_I4;
	var.lVal = 1;
	Invoke(pApplication, L"Visible", DISPATCH_PROPERTYPUT, &var, 1, NULL);

	VariantInit(&varResult);
	Invoke(pApplication, L"Workbooks", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
	pWorkbooks = varResult.pdispVal;
	
	var.vt = VT_BSTR;
	var.bstrVal = SysAllocString(L"C:\\sample.xls");
	VariantInit(&varResult);
	hr = Invoke(pWorkbooks, L"Open", DISPATCH_METHOD, &var, 1, &varResult);
	pWorkbook = varResult.pdispVal;
	SysFreeString(var.bstrVal);
	if (FAILED(hr)) {
		MessageBox(NULL, TEXT("���[�N�u�b�N�̃I�[�v���Ɏ��s���܂����B"), NULL, MB_ICONWARNING);
		pWorkbooks->Release();
		pApplication->Release();
		CoUninitialize();
		return 0;
	}
	
	VariantInit(&varResult);
	Invoke(pWorkbook, L"Worksheets", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
	pWorksheets = varResult.pdispVal;

	VariantInit(&varResult);
	Invoke(pWorksheets, L"Count", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
	lCount = varResult.lVal;

	for (i = 0; i < lCount; i++) {
		var.vt = VT_I4;
		var.lVal = i + 1;
		VariantInit(&varResult);
		Invoke(pWorksheets, L"Item", DISPATCH_PROPERTYGET, &var, 1, &varResult);
		pWorksheet = varResult.pdispVal;

		VariantInit(&varResult);
		Invoke(pWorksheet, L"Name", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
		MessageBoxW(NULL, (LPWSTR)varResult.bstrVal, TEXT("OK"), MB_OK);
		VariantClear(&varResult);

		pWorksheet->Release();
	}

	pWorksheets->Release();
	pWorkbook->Release();
	pWorkbooks->Release();
	pApplication->Release();
	CoUninitialize();
	
	return 0;
}

HRESULT Invoke(IDispatch *pDispatch, LPOLESTR lpszName, WORD wFlags, VARIANT *pVarArray, int nArgs, VARIANT *pVarResult)
{
	DISPPARAMS dispParams;
	DISPID     dispid;
	DISPID     dispidName = DISPID_PROPERTYPUT;
	HRESULT    hr;

	hr = pDispatch->GetIDsOfNames(IID_NULL, &lpszName, 1, LOCALE_USER_DEFAULT, &dispid);
	if (FAILED(hr))
		return hr;
	
	dispParams.cArgs = nArgs;
	dispParams.rgvarg = pVarArray;
	if (wFlags & DISPATCH_PROPERTYPUT) {
		dispParams.cNamedArgs = 1;
		dispParams.rgdispidNamedArgs = &dispidName;
	}
	else {
		dispParams.cNamedArgs = 0;
		dispParams.rgdispidNamedArgs = NULL;
	}

	hr = pDispatch->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, wFlags, &dispParams, pVarResult, NULL, NULL);

	return hr;
}

���[�N�V�[�g��񋓂���ɂ́AWorksheets�I�u�W�F�N�g���擾���Ȃ���΂Ȃ�܂���B ���̂��߂̏����͎��̂悤�ɂȂ��Ă��܂��B

VariantInit(&varResult);
Invoke(pApplication, L"Workbooks", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
pWorkbooks = varResult.pdispVal;

var.vt = VT_BSTR;
var.bstrVal = SysAllocString(L"C:\\sample.xls");
VariantInit(&varResult);
hr = Invoke(pWorkbooks, L"Open", DISPATCH_METHOD, &var, 1, &varResult);
pWorkbook = varResult.pdispVal;
SysFreeString(var.bstrVal);

VariantInit(&varResult);
Invoke(pWorkbook, L"Worksheets", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
pWorksheets = varResult.pdispVal;

Worksheets�I�u�W�F�N�g���擾����ɂ́A�܂�Workbooks�I�u�W�F�N�g���擾���A �����Workbook�I�u�W�F�N�g���擾���Ȃ���΂Ȃ�܂���B Workbooks�I�u�W�F�N�g�́AApplication�I�u�W�F�N�g��Workbooks���\�b�h���Ăяo�����ƂŎ擾�ł��܂��B Workbooks�I�u�W�F�N�g���擾������AOpen���\�b�h���Ăяo����1�‚̃��[�N�u�b�N���J�����Ƃ��ł��邽�߁A �����Workbook�I�u�W�F�N�g���擾�ł������ƂɂȂ�܂��B ��͂��̃I�u�W�F�N�g��Worksheets�v���p�e�B���Ăяo���΁AWorksheets�I�u�W�F�N�g���擾�ł������ƂɂȂ�܂��B

Worksheets�I�u�W�F�N�g���擾������ACount�v���p�e�B���Ăяo���ăI�u�W�F�N�g�̐����擾���܂��B �����āA���̐�����Item�v���p�e�B���Ăяo����Worksheet�I�u�W�F�N�g���擾���܂��B

VariantInit(&varResult);
Invoke(pWorksheets, L"Count", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
lCount = varResult.lVal;

for (i = 0; i < lCount; i++) {
	var.vt = VT_I4;
	var.lVal = i + 1;
	VariantInit(&varResult);
	Invoke(pWorksheets, L"Item", DISPATCH_PROPERTYGET, &var, 1, &varResult);
	pWorksheet = varResult.pdispVal;

	VariantInit(&varResult);
	Invoke(pWorksheet, L"Name", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
	MessageBoxW(NULL, (LPWSTR)varResult.bstrVal, TEXT("OK"), MB_OK);
	VariantClear(&varResult);

	pWorksheet->Release();
}

Item�v���p�e�B�̓I�u�W�F�N�g�̃C���f�b�N�X��1���琔���邽�߁A +1���w�肵����ԂŃC���f�b�N�X���w�肷�邱�ƂɂȂ�܂��B pdispVal����擾����Worksheet�I�u�W�F�N�g�ւ̃|�C���^��Invoke�Ɏw�肳��A Name�v���p�e�B�̌Ăяo���Ɏg�p����Ă��܂��B ���̃v���p�e�B�́A���[�N�V�[�g�̖��O��BSTR�^�ŕԂ����Ƃ��ł��܂��B VariantClear���Ăяo���Ă���̂́A����BSTR�^���J�����邽�߂ł��B

_NewEnum�v���p�e�B��IEnumVARIANT

�R���N�V�����I�u�W�F�N�g���Ǘ�����I�u�W�F�N�g�́A_NewEnum�v���p�e�B���Ăяo�����Ƃɂ���Ă��񋓂��邱�Ƃ��ł��܂��B ���̃v���p�e�B���Ԃ��I�u�W�F�N�g��IEnumVARIANT�Ƃ����C���^�[�t�F�[�X���������Ă��邽�߁A ������擾���ăI�u�W�F�N�g��񋓂��܂��B

#include <windows.h>

HRESULT Invoke(IDispatch *pDispatch, LPOLESTR lpszName, WORD wFlags, VARIANT *pVarArray, int nArgs, VARIANT *pVarResult);

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
{
	IDispatch    *pApplication;
	IDispatch    *pWorkbooks;
	IDispatch    *pWorkbook;
	IDispatch    *pWorksheets;
	IDispatch    *pWorksheet;
	IDispatch    *pNewEnum;
	IEnumVARIANT *pEnumVariant;
	CLSID        clsid;
	HRESULT      hr;
	VARIANT      var;
	VARIANT      varResult;

	CoInitialize(NULL);
	
	hr = CLSIDFromProgID(L"Excel.Application", &clsid);
	if (FAILED(hr)) {
		CoUninitialize();
		return 0;
	}
	
	hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&pApplication));
	if (FAILED(hr)) {
		CoUninitialize();
		return 0;
	}
	
	var.vt = VT_I4;
	var.lVal = 1;
	Invoke(pApplication, L"Visible", DISPATCH_PROPERTYPUT, &var, 1, NULL);

	VariantInit(&varResult);
	Invoke(pApplication, L"Workbooks", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
	pWorkbooks = varResult.pdispVal;
	
	var.vt = VT_BSTR;
	var.bstrVal = SysAllocString(L"C:\\sample.xls");
	VariantInit(&varResult);
	hr = Invoke(pWorkbooks, L"Open", DISPATCH_METHOD, &var, 1, &varResult);
	pWorkbook = varResult.pdispVal;
	SysFreeString(var.bstrVal);
	if (FAILED(hr)) {
		MessageBox(NULL, TEXT("���[�N�u�b�N�̃I�[�v���Ɏ��s���܂����B"), NULL, MB_ICONWARNING);
		pWorkbooks->Release();
		pApplication->Release();
		CoUninitialize();
		return 0;
	}
	
	VariantInit(&varResult);
	Invoke(pWorkbook, L"Worksheets", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
	pWorksheets = varResult.pdispVal;
	
	VariantInit(&varResult);
	Invoke(pWorksheets, NULL, DISPATCH_PROPERTYGET, NULL, 0, &varResult);
	pNewEnum = varResult.pdispVal;
	pNewEnum->QueryInterface(IID_IEnumVARIANT, (void **)&pEnumVariant);
	
	VariantInit(&varResult);
	while (pEnumVariant->Next(1, &varResult, NULL) == S_OK) {
		if (varResult.vt != VT_DISPATCH)
			continue;
		pWorksheet = varResult.pdispVal;
		
		VariantInit(&varResult);
		Invoke(pWorksheet, L"Name", DISPATCH_PROPERTYGET, NULL, 0, &varResult);
		MessageBoxW(NULL, (LPWSTR)varResult.bstrVal, TEXT("OK"), MB_OK);
		VariantClear(&varResult);

		pWorksheet->Release();
	}

	pEnumVariant->Release();
	pNewEnum->Release();
	pWorksheets->Release();
	pWorkbook->Release();
	pWorkbooks->Release();
	pApplication->Release();
	CoUninitialize();
	
	return 0;
}

HRESULT Invoke(IDispatch *pDispatch, LPOLESTR lpszName, WORD wFlags, VARIANT *pVarArray, int nArgs, VARIANT *pVarResult)
{
	DISPPARAMS dispParams;
	DISPID     dispid;
	DISPID     dispidName = DISPID_PROPERTYPUT;
	HRESULT    hr;

	if (lpszName != NULL) {
		hr = pDispatch->GetIDsOfNames(IID_NULL, &lpszName, 1, LOCALE_USER_DEFAULT, &dispid);
		if (FAILED(hr))
			return hr;
	}
	else
		dispid = DISPID_NEWENUM;
	
	dispParams.cArgs = nArgs;
	dispParams.rgvarg = pVarArray;
	if (wFlags & DISPATCH_PROPERTYPUT) {
		dispParams.cNamedArgs = 1;
		dispParams.rgdispidNamedArgs = &dispidName;
	}
	else {
		dispParams.cNamedArgs = 0;
		dispParams.rgdispidNamedArgs = NULL;
	}

	hr = pDispatch->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, wFlags, &dispParams, pVarResult, NULL, NULL);

	return hr;
}

Worksheets�I�u�W�F�N�g���擾������_NewEnum�v���p�e�B���Ăяo���܂��B ����͏�������ȃv���p�e�B�ł���A�^�C�v���C�u������MSDN����͂��̖��O���m�F���邱�Ƃ��ł��܂���B �������ADISPID_NEWENUM�Ƃ���DISPID��IDispatch::Invoke�Ɏw�肷��ΌĂяo�����Ƃ��ł���̂ŁA Invoke�̑�2������NULL���w�肵���ꍇ�͂��̂悤�ȏ��������s����邱�ƂɂȂ��Ă��܂��B _NewEnum�v���p�e�B���Ԃ����I�u�W�F�N�g�͗񋓂��s�����߂ɕK�v��IEnumVARIANT���������Ă���̂ŁA QueryInterface��IEnumVARIANT��IID���w�肵�Ď擾���܂��B IEnumVARIANT�ւ̃|�C���^���擾������ANext���\�b�h���Ăяo�����ƂŃI�u�W�F�N�g��񋓂��邱�Ƃ��ł��܂��B



�߂�