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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <windows.h>
#include <tchar.h>
#include <cstdio>
const CLSID CLSID_XLApplication = {0x00024500,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}}; // CLSID of Excel
const IID IID_Application = {0x000208D5,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}}; // IID of _Application
int main()
{
DISPPARAMS NoArgs={NULL,NULL,0,0};
IDispatch* pXLApp=NULL;
DISPPARAMS DispParams;
VARIANT CallArgs[1];
void* pMsgBuf=NULL;
VARIANT vResult;
DWORD dwFlags;
DISPID dispid;
HRESULT hr;
LCID lcid;
CoInitialize(NULL);
dwFlags=FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM;
hr=CoCreateInstance(CLSID_XLApplication, NULL, CLSCTX_LOCAL_SERVER, IID_Application, (void**)&pXLApp);
if(SUCCEEDED(hr))
{
OLECHAR* szVisible=(OLECHAR*)L"Visible";
lcid=GetUserDefaultLCID();
hr=pXLApp->GetIDsOfNames(IID_NULL,&szVisible,1,lcid,&dispid);
if(SUCCEEDED(hr))
{
VariantInit(&CallArgs[0]);
CallArgs[0].vt = VT_BOOL;
CallArgs[0].boolVal = TRUE;
DISPID dispidNamed = DISPID_PROPERTYPUT;
DispParams.rgvarg = CallArgs;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 1;
VariantInit(&vResult); // Call or Invoke _Application::Visible(true);
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT,&DispParams,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
OLECHAR* szWorkbooks=(OLECHAR*)L"Workbooks";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szWorkbooks,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
IDispatch* pXLBooks=NULL; // Get Workbooks Collection
VariantInit(&vResult); // Invoke _Application::Workbooks(&pXLBooks) << returns IDispatch** of Workbooks Collection
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLBooks=vResult.pdispVal;
IDispatch* pXLBook=NULL; // Try to Open Workbook
OLECHAR* szOpen=(OLECHAR*)L"Open";
hr=pXLBooks->GetIDsOfNames(IID_NULL,&szOpen,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
VariantInit(&vResult); // Invoke Workbooks::Open(&Workbook) << returns IDispatch** of Workbook Object
CallArgs[0].vt = VT_BSTR;
CallArgs[0].bstrVal = SysAllocString(L"C:\\Code\\CodeBlks\\XL_Dispatch\\Book1.xls");
DispParams.rgvarg = CallArgs;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0; // Invoke _Worksheet::Range("A1") << returns IDispatch** to dispinterface Range
hr=pXLBooks->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD|DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
// [id(0x000000b5), helpcontext(0x000100b5)] HRESULT Add([in, lcid] long lcid, [out, retval] Workbook** RHS);
// [id(0x000002aa), helpcontext(0x000102aa)] HRESULT Open([in] BSTR Filename, [in, lcid] long lcid, [out, retval] Workbook** RHS);
if(SUCCEEDED(hr))
{
printf("We Succeeded!\n");
pXLBook=vResult.pdispVal;
OLECHAR* szActiveSheet=(OLECHAR*)L"ActiveSheet";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szActiveSheet,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
IDispatch* pXLSheet=NULL; // Try To Get ActiveSheet
VariantInit(&vResult); // Invoke _Application::ActiveSheet(&pXLSheet); << ret IDispatch** to Worksheet (Worksheet)
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLSheet=vResult.pdispVal;
OLECHAR* szRange=(OLECHAR*)L"Range";
hr=pXLSheet->GetIDsOfNames(IID_NULL,&szRange,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
IDispatch* pXLRange=NULL;
VariantInit(&vResult);
CallArgs[0].vt=VT_BSTR,
CallArgs[0].bstrVal=SysAllocString(L"A1");
DispParams.rgvarg = CallArgs;
DispParams.rgdispidNamedArgs = 0;
DispParams.cArgs = 1; // Try to get Range
DispParams.cNamedArgs = 0; // Invoke _Worksheet::Range("A1") << returns IDispatch** to dispinterface Range
hr=pXLSheet->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLRange=vResult.pdispVal;
OLECHAR* szValue=(OLECHAR*)L"Value";
hr=pXLRange->GetIDsOfNames(IID_NULL,&szValue,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
printf("dispid (Value) = %d\n",(int)dispid);
VariantClear(&CallArgs[0]);
hr=pXLRange->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
wprintf(L"vResult.bstrVal = %s\n",vResult.bstrVal);
VariantClear(&vResult);
}
else
{
FormatMessage(dwFlags,NULL,hr,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&pMsgBuf,0,NULL);
printf("%s\n",(char*)pMsgBuf);
LocalFree(pMsgBuf);
}
pXLRange->Release();
}
}
}
pXLSheet->Release();
}
}
pXLBook->Release();
}
else
{
FormatMessage(dwFlags,NULL,hr,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&pMsgBuf,0,NULL);
printf("%s\n",(char*)pMsgBuf);
LocalFree(pMsgBuf);
}
}
pXLBooks->Release();
}
}
}
getchar();
}
VariantInit(&vResult); // Try to do _Application::Close()
hr=pXLApp->Invoke(0x0000012e,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&NoArgs,&vResult,NULL,NULL);
pXLApp->Release();
}
CoUninitialize();
return 0;
}
|