Hello everyone!
I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.
I have made a MS Word document, with a table. Table has 4 columns.
I would like to insert random text into columns, using C++ and pure Win32 API.
I believe this can be done via OLE and automation, but I am new to that, so If that is the only way to do it, please help me by posting some sample code example, if that is not a big deal, so I can see the logic behind using OLE and automation, I would be very grateful, since I couldn't handle the online documentation on MSDN.
That would be all, I just need help for this, in my opinion, simple task-> to fill a table in document made in MS Word with some random text.
EDIT:
---------------------------------------------------------
I have decided to use OLE and C++ for this task, following the exaple from this article:
http://support.microsoft.com/kb/216686/en-us[
^]
I have managed to open the Word file, make it invisible, but I need help with selecting the existing table.
That would be enough for now, since it would enable me to try inserting text into table on my own.
Here is the code:
NOTE:
-----------------------
The handle provided in AutoWrap() is for a dialog that must be closed if error occurs, everything else is the same.
-----------------------
HRESULT AutoWrap(HWND hwnd,int autoType, VARIANT *pvResult, IDispatch *pDisp,
LPOLESTR ptName, int cArgs...)
{
va_list marker;
va_start(marker, cArgs);
if(!pDisp)
{
MessageBox( NULL, L"NULL IDispatch passed to AutoWrap()", L"Error", MB_ICONERROR );
EndDialog(hwnd, IDCANCEL);
}
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID;
HRESULT hr;
wchar_t 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))
{
swprintf_s( buf, L"IDispatch::GetIDsOfNames(\"%s\") failed with error 0x%08lx",
szName, hr );
MessageBox( NULL, buf, L"AutoWrap()", MB_ICONERROR );
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) )
{
swprintf_s(buf, L"IDispatch::Invoke(\"%s\"=%08lx) failed with error 0x%08lx",
szName, dispID, hr);
MessageBox( NULL, buf, L"AutoWrap()", MB_ICONERROR );
return hr;
}
va_end(marker);
delete [] pArgs;
return hr;
}
------------------------------
When dialog button is pressed, open word and fill pre made table with some text:
-------------------------------
case IDOK:
{
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);
if(FAILED(hr))
{
MessageBox( NULL, L"CLSIDFromProgID() failed", L"Error",
MB_ICONERROR );
EndDialog(hwnd, IDCANCEL);
}
IDispatch *pWordApp;
hr = CoCreateInstance( clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch,
(void **)&pWordApp );
if(FAILED(hr))
{
MessageBox( NULL, L"Word not registered properly", L"Error",
MB_ICONERROR );
EndDialog(hwnd, IDCANCEL);
}
{
VARIANT x;
x.vt = VT_I4;
x.lVal = 1; AutoWrap( hwnd, DISPATCH_PROPERTYPUT, NULL, pWordApp,
L"Visible", 1, x);
}
IDispatch *pDocuments;
{
VARIANT result;
VariantInit(&result);
hr = AutoWrap( hwnd, DISPATCH_PROPERTYGET, &result, pWordApp,
L"Documents", 0);
pDocuments = result.pdispVal;
}
IDispatch *pActiveDocument;
{
wchar_t tmp[ _MAX_PATH ];
memset( &tmp, '\0', sizeof(tmp) );
if( DWORD nSize = GetModuleFileName( NULL, tmp, _MAX_PATH ) )
{
for (int i = nSize - 1; i >= 0; i--)
{
if ( tmp[i] == L'\\' || tmp[i] == L'/')
{
tmp[i + 1] = L'\0';
nSize = i + 1;
break;
}
}
wcscat_s( tmp, _MAX_PATH, L"print\\Tabela.docx" );
}
else
{
MessageBox( NULL, L"Грешка при учитавању ресурса за штампу!",
L"", MB_ICONERROR );
EndDialog(hwnd, IDCANCEL);
}
VARIANT result;
VariantInit(&result);
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(tmp);
AutoWrap( hwnd, DISPATCH_METHOD, &result, pDocuments,
L"Open", 1, x );
pActiveDocument = result.pdispVal;
SysFreeString(x.bstrVal);
}
MessageBox( hwnd, L"Added to give me time to see the effects", L"", MB_OK );
{
VARIANT x;
x.vt = VT_BOOL;
x.boolVal = false;
AutoWrap( hwnd, DISPATCH_METHOD, NULL, pActiveDocument, L"Close",
1, x );
AutoWrap( hwnd, DISPATCH_METHOD, NULL, pWordApp, L"Quit", 0);
}
pActiveDocument->Release();
pDocuments->Release();
pWordApp->Release();
CoUninitialize();
}
break;
Thank you.
--------------------------------------------------------------------------
I work in MS Visual Studio Express 2008, on Windows XP, in C++, using pure WIN32 API.