// InsertAfterSheets.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...) { va_list marker; va_start(marker, cArgs); if(!pDisp) { //_exit(0); } DISPPARAMS dp = { NULL, NULL, 0, 0 }; DISPID dispidNamed = DISPID_PROPERTYPUT; DISPID dispID; HRESULT hr; char buf[200]; char szName[200]; WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL); hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID); if(FAILED(hr)) { sprintf(buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx", szName, hr); printf("%s\n",buf); return hr; } VARIANT *pArgs = new VARIANT[cArgs+1]; for(int i=0; i<cArgs; i++) { pArgs[i] = va_arg(marker, VARIANT); } dp.cArgs = cArgs; dp.rgvarg = pArgs; if(autoType & DISPATCH_PROPERTYPUT) { dp.cNamedArgs = 1; dp.rgdispidNamedArgs = &dispidNamed; } hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, NULL, NULL); if(FAILED(hr)) { sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx", szName, dispID, hr); printf("%s\n",buf); return hr; } va_end(marker); delete [] pArgs; return hr; } int _tmain(int argc, _TCHAR* argv[]) { HRESULT hresult; CLSID xlCLSID; IDispatch *xlApp; VARIANT pvResult; hresult = OleInitialize(NULL); if(SUCCEEDED(hresult)){ CLSIDFromProgID(L"Excel.Application",&xlCLSID); hresult = CoCreateInstance(xlCLSID,NULL, CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER, IID_IDispatch,(void **)&xlApp); if(SUCCEEDED(hresult)){ if(SUCCEEDED(hresult)){ VARIANT x; x.vt = VT_I4; x.lVal =1; hresult = AutoWrap(DISPATCH_PROPERTYPUT,NULL,xlApp,L"Visible",1, x); if(SUCCEEDED(hresult)){ IDispatch *xlBooks; VariantInit(&pvResult); hresult = AutoWrap(DISPATCH_PROPERTYGET, &pvResult, xlApp, L"Workbooks", 0); if(SUCCEEDED(hresult)){ xlBooks = pvResult.pdispVal; IDispatch *xlBook; // Add a new workbook hresult = AutoWrap(DISPATCH_PROPERTYGET,&pvResult, xlBooks,L"Add",0); xlBook = pvResult.pdispVal; IDispatch *xlSheets; // Get sheet collection hresult = AutoWrap(DISPATCH_PROPERTYGET,&pvResult, xlBook,L"Sheets",0); xlSheets = pvResult.pdispVal; if(SUCCEEDED(hresult)){ IDispatch *xlSheet; VARIANT p1; p1.vt = VT_INT; p1.intVal = 1; // Get Sheet1 hresult = AutoWrap(DISPATCH_PROPERTYGET,&pvResult, xlSheets,L"Item",1,p1); if(SUCCEEDED(hresult)){ xlSheet = pvResult.pdispVal; IDispatch *xlRange; // Get Range "A1" hresult = AutoWrap(DISPATCH_PROPERTYGET, &pvResult,xlSheet,L"Cells",2,p1,p1); if(SUCCEEDED(hresult)){ xlRange = pvResult.pdispVal; VARIANT Value; Value.vt = VT_ARRAY | VT_VARIANT; { SAFEARRAYBOUND sab[2]; sab[0].lLbound = 1; sab[0].cElements = 1; sab[1].lLbound = 1; sab[1].cElements = 1; Value.parray = SafeArrayCreate( VT_VARIANT,2,sab); } long address[] = {1,1}; VARIANT element; element.vt = VT_BSTR; element.bstrVal = SysAllocString( L"Hello World!"); SafeArrayPutElement(Value.parray,address, (void *)&element); // Set Hello World! hresult = AutoWrap(DISPATCH_PROPERTYPUT, NULL,xlRange,L"Value",1,Value); } } } } } } } else printf("No\n"); } system("pause"); CoUninitialize(); return 0; }