c/c++ 利用com API 非MFC 操作word 如何 插入表格??
程序代码:
#include <cstdio> #include <conio.h> #include <clocale> #include <windows.h> #include <ole2.h> //-lole32 -loleaut32 -luuid //-fexec-charset=gbk -finput-charset=gbk // AutoWrap() - Automation helper function... HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...) { // Begin variable-argument list... va_list marker; va_start(marker, cArgs); if(!pDisp) { MessageBox(nullptr, "nullptr IDispatch passed to AutoWrap()", "Error", 0x10010); _exit(0); } // Variables used... DISPPARAMS dp = { nullptr, nullptr, 0, 0 }; DISPID dispidNamed = DISPID_PROPERTYPUT; DISPID dispID; HRESULT hr; char buf[200]; char szName[200]; // Convert down to ANSI WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, nullptr, nullptr); // Get DISPID for name passed... 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); MessageBox(nullptr, buf, "AutoWrap()", 0x10010); _exit(0); return hr; } // Allocate memory for arguments... VARIANT *pArgs = new VARIANT[cArgs+1]; // Extract arguments... for(int i=0; i<cArgs; i++) { pArgs[i] = va_arg(marker, VARIANT); } // Build DISPPARAMS dp.cArgs = cArgs; dp.rgvarg = pArgs; // Handle special-case for property-puts! if(autoType & DISPATCH_PROPERTYPUT) { = 1; dp.rgdispidNamedArgs = &dispidNamed; } // Make the call! hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, nullptr, nullptr); if(FAILED(hr)) { sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx", szName, dispID, hr); MessageBox(nullptr, buf, "AutoWrap()", 0x10010); _exit(0); return hr; } // End variable-argument section... va_end(marker); delete [] pArgs; return hr; } int main() { IDispatch* m_pWApp; IDispatch* m_pDocuments; IDispatch* m_pActiveDocument; IDispatch* m_pDocApp; IDispatch* m_pSelection; HRESULT m_hr; setlocale(LC_ALL,""); CoInitialize(nullptr); CLSID clsid; m_hr = CLSIDFromProgID(L"Word.Application", &clsid); if (SUCCEEDED(m_hr)) { m_hr = CoCreateInstance(clsid, nullptr, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&m_pWApp); } { VARIANT x; x.vt = VT_I4; x.lVal = 1; m_hr = AutoWrap(DISPATCH_PROPERTYPUT, nullptr, m_pWApp, (LPOLESTR) L"Visible", 1, x); } { VARIANT result; VariantInit(&result); m_hr = AutoWrap(DISPATCH_PROPERTYGET, &result, m_pWApp, (LPOLESTR)L"Documents", 0); m_pDocuments = result.pdispVal; } { VARIANT result; VariantInit(&result); m_hr = AutoWrap(DISPATCH_METHOD, &result, m_pDocuments,(LPOLESTR) L"Add", 0); m_pActiveDocument = result.pdispVal; } { VARIANT result; VariantInit(&result); m_hr = AutoWrap(DISPATCH_PROPERTYGET, &result, m_pActiveDocument, (LPOLESTR)L"Application", 0); m_pDocApp= result.pdispVal; } { VARIANT result; VariantInit(&result); m_hr = AutoWrap(DISPATCH_PROPERTYGET, &result, m_pDocApp, (LPOLESTR)L"Selection", 0); m_pSelection=result.pdispVal; } ///////////////////////////////////////////// IDispatch* m_pTables; { VARIANT result; VariantInit(&result); AutoWrap(DISPATCH_PROPERTYGET, &result, m_pActiveDocument, (LPOLESTR)L"Tables", 0); m_pTables=result.pdispVal; } printf("1111111111\n"); _getch(); IDispatch* m_pRange; { VARIANT result; VariantInit(&result); AutoWrap(DISPATCH_PROPERTYGET, &result, m_pSelection, (LPOLESTR)L"Range", 0); m_pRange = result.pdispVal; } printf("222222222222\n"); _getch(); IDispatch* m_pTable; { //这句是 VBA语句: ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:=5, DefaultTableBehavior:=1, AutoFitBehavior:=2 VARIANT NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior; NumRows.vt = VT_I4; NumRows.lVal = 6; NumColumns.vt = VT_I4; NumColumns.lVal = 4; DefaultTableBehavior.vt = VT_I4; DefaultTableBehavior.lVal = 1; AutoFitBehavior.vt = VT_I4; AutoFitBehavior.lVal = 2; VARIANT result; VariantInit(&result); m_hr = AutoWrap(DISPATCH_METHOD, &result, m_pTables, (LPOLESTR) L"Add", 5, AutoFitBehavior, DefaultTableBehavior, NumColumns, NumRows, m_pRange); // m_hr = AutoWrap(DISPATCH_METHOD, &result, m_pTables, (LPOLESTR) L"Add", 3, NumColumns, NumRows, m_pSelection); m_pTable = result.pdispVal; } printf("3333333333\n"); _getch(); //////////////////////////////////////// m_pRange->Release(); m_pSelection->Release(); m_pDocApp->Release(); m_pTable->Release(); m_pTables->Release(); m_pActiveDocument->Release(); m_pDocuments->Release(); m_pWApp->Release(); CoUninitialize(); _getch(); return 0; } (win7 x64系统 mingw64) 程序运行到Add时,出错了? 大神,我改怎么写这句!!!!!