EDN Admin
Well-known member
I have just leant a bit of COM, and I know that the VBA programming is based on COM component provided by MS. But I do not now know how to programming office with c++, because I do not know how to import type library or something for my c++ programme.
Here is my code to count the words of a doc file, but failed, could you help me to correct it, thanks.<br/>
<br/>
<pre class="prettyprint #include <objbase.h>
#include <stdio.h>
#include <assert.h>
#include <atlbase.h>
#include <atlconv.h>
#pragma comment(lib, "ole32.lib")
//0002095C-0000-0000-C000-000000000046
IID IID_Words = { 0x0002095C, 0x0000, 0x0000, {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46} };
//#import "msword.olb" how???
IDispatch* GetWordsInterface(LPCWSTR wszFileName);
int main()
{
IDispatch *pDisp = NULL;
LPOLESTR pwszFuncName = L"Count";
DISPID dispID;
HRESULT hr;
hr = CoInitialize(NULL);
assert( hr == S_OK );
pDisp = GetWordsInterface(L"D:\test.doc");
assert( pDisp != NULL );
hr = pDisp->GetIDsOfNames( IID_NULL, &pwszFuncName, 1, LOCALE_SYSTEM_DEFAULT, &dispID );
assert( hr == S_OK );
VARIANT result;
hr = pDisp->Invoke(
dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET,
NULL, &result, NULL, NULL);
assert( hr == S_OK );
printf("the count of words is %ld n", result.dblVal);
CoUninitialize();
return 0;
}
IDispatch* GetWordsInterface(LPCWSTR pwszFileName)
{
HRESULT hr = S_FALSE;
IBindCtx *pbc = NULL;
IMoniker *pMk = NULL;
LPWSTR strClsid = NULL;
LPWSTR strDisplayName = NULL;
IUnknown *pUnk = NULL;
IDispatch *pWords = NULL;
CLSID clsid;
hr = CreateBindCtx( 0, &pbc );
assert( pbc != NULL && hr == S_OK );
hr = CreateFileMoniker(pwszFileName, &pMk);
assert( hr == S_OK && pMk != NULL );
hr = pMk->GetClassID(&clsid);
assert( hr == S_OK );
StringFromCLSID(clsid, &strClsid);
wprintf(L"CLSID : %sn", strClsid);
CoTaskMemFree(strClsid);
hr = pMk->GetDisplayName(pbc, NULL, &strDisplayName);
assert( hr == S_OK && strDisplayName != NULL );
CW2A ascii(strDisplayName);
printf("Display Name : %sn", ascii);
//wprintf(L"Display Name : %sn", strDisplayName);
CoTaskMemFree(strDisplayName);
// hr = pMk->BindToObject(pbc, NULL, IID_IUnknown, (void**)&pUnk);
// assert( hr == S_OK && pUnk != NULL );
//
// hr = pUnk->QueryInterface(IID_Words, (void**)&pWords);
// assert( hr == S_OK && pWords != NULL );
hr = pMk->BindToObject(pbc, NULL, IID_Words, (void**)&pWords); // FAILED with E_NOINTERFACE!!!
assert( hr == S_OK && pWords != NULL );
pUnk->Release();
pMk->Release();
return pWords;
}[/code]
<br/>
<br/>
<br/>
View the full article
Here is my code to count the words of a doc file, but failed, could you help me to correct it, thanks.<br/>
<br/>
<pre class="prettyprint #include <objbase.h>
#include <stdio.h>
#include <assert.h>
#include <atlbase.h>
#include <atlconv.h>
#pragma comment(lib, "ole32.lib")
//0002095C-0000-0000-C000-000000000046
IID IID_Words = { 0x0002095C, 0x0000, 0x0000, {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46} };
//#import "msword.olb" how???
IDispatch* GetWordsInterface(LPCWSTR wszFileName);
int main()
{
IDispatch *pDisp = NULL;
LPOLESTR pwszFuncName = L"Count";
DISPID dispID;
HRESULT hr;
hr = CoInitialize(NULL);
assert( hr == S_OK );
pDisp = GetWordsInterface(L"D:\test.doc");
assert( pDisp != NULL );
hr = pDisp->GetIDsOfNames( IID_NULL, &pwszFuncName, 1, LOCALE_SYSTEM_DEFAULT, &dispID );
assert( hr == S_OK );
VARIANT result;
hr = pDisp->Invoke(
dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET,
NULL, &result, NULL, NULL);
assert( hr == S_OK );
printf("the count of words is %ld n", result.dblVal);
CoUninitialize();
return 0;
}
IDispatch* GetWordsInterface(LPCWSTR pwszFileName)
{
HRESULT hr = S_FALSE;
IBindCtx *pbc = NULL;
IMoniker *pMk = NULL;
LPWSTR strClsid = NULL;
LPWSTR strDisplayName = NULL;
IUnknown *pUnk = NULL;
IDispatch *pWords = NULL;
CLSID clsid;
hr = CreateBindCtx( 0, &pbc );
assert( pbc != NULL && hr == S_OK );
hr = CreateFileMoniker(pwszFileName, &pMk);
assert( hr == S_OK && pMk != NULL );
hr = pMk->GetClassID(&clsid);
assert( hr == S_OK );
StringFromCLSID(clsid, &strClsid);
wprintf(L"CLSID : %sn", strClsid);
CoTaskMemFree(strClsid);
hr = pMk->GetDisplayName(pbc, NULL, &strDisplayName);
assert( hr == S_OK && strDisplayName != NULL );
CW2A ascii(strDisplayName);
printf("Display Name : %sn", ascii);
//wprintf(L"Display Name : %sn", strDisplayName);
CoTaskMemFree(strDisplayName);
// hr = pMk->BindToObject(pbc, NULL, IID_IUnknown, (void**)&pUnk);
// assert( hr == S_OK && pUnk != NULL );
//
// hr = pUnk->QueryInterface(IID_Words, (void**)&pWords);
// assert( hr == S_OK && pWords != NULL );
hr = pMk->BindToObject(pbc, NULL, IID_Words, (void**)&pWords); // FAILED with E_NOINTERFACE!!!
assert( hr == S_OK && pWords != NULL );
pUnk->Release();
pMk->Release();
return pWords;
}[/code]
<br/>
<br/>
<br/>
View the full article