void CMyActiveXCtrl::GettheNameofContainerDocument() { IMoniker* ptrfullMoniker = NULL; char objectname[300]; LPOLESTR ppszDisplaynamefull; IBindCtx* pbcfull = NULL;LPOLECLIENTSITE pOleClientSite = GetClientSite();if(pOleClientSite) { if(SUCCEEDED(pOleClientSite->GetMoniker(OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_OBJFULL, &ptrfullMoniker)))<BR/> // The typedefs for OLEGETMONIKER and OLWHICHMK are in oleidl.h { if (SUCCEEDED(CreateBindCtx( 0, &pbcfull ))) { if(SUCCEEDED(ptrfullMoniker->GetDisplayName(pbcfull,NULL,<BR/> &ppszDisplaynamefull))) { wcstombs(objectname,ppszDisplaynamefull,300); AfxMessageBox(ExtractDocumentName(objectname)); ptrfullMoniker->Release(); } } pbcfull->Release(); } pOleClientSite->Release(); } }char* CMyActiveXCtrl::ExtractDocumentName(char* objectname) { char* ptrchar; char* ptrdocname; //reverse the string ptrchar = _strrev(objectname); //Ignore the first token , this is the name of the embedded object strtok(ptrchar,"!"); //get the remainder of the string and reverse it to get the Document name ptrdocname = strrev(strtok(NULL,"\0")); return ptrdocname; }
http://support.microsoft.com/kb/q266318/
to zhoujingsheng712您的代码,我一回拿过去试一下,谢谢!! 不过,我更想要的是,我的控件是嵌入在word里面,如何通过GetClientSite函数的方式, 来获包容器的指针!
,然后添加如下代码
CLSID clsid;
HRESULT hr;
IUnknown *pUnknown=NULL;
IDispatch *pDispatch=NULL;
_Application app=NULL;
hr = ::CLSIDFromProgID(L"Word.Application",&clsid); //通过ProgID取得CLSID
if(FAILED(hr))
{
AfxMessageBox(_T("您没有安装OFFICE"));
return;
}
hr = ::GetActiveObject(clsid,NULL,&pUnknown); //查找是否有WORD程序在运行
if(FAILED(hr))
{
AfxMessageBox(_T("没有正在运行中的WORD应用程序"));
return;
}
hr = pUnknown->QueryInterface(IID_IDispatch,(LPVOID *)&app);
if(FAILED(hr)) AfxMessageBox(_T("没有取得IDispatchPtr"));
pUnknown->Release(); pUnknown=NULL;
{
IMoniker* ptrfullMoniker = NULL;
char objectname[300];
LPOLESTR ppszDisplaynamefull;
IBindCtx* pbcfull = NULL;LPOLECLIENTSITE pOleClientSite = GetClientSite();if(pOleClientSite)
{
if(SUCCEEDED(pOleClientSite->GetMoniker(OLEGETMONIKER_FORCEASSIGN,
OLEWHICHMK_OBJFULL, &ptrfullMoniker)))<BR/>
// The typedefs for OLEGETMONIKER and OLWHICHMK are in oleidl.h
{
if (SUCCEEDED(CreateBindCtx( 0, &pbcfull )))
{
if(SUCCEEDED(ptrfullMoniker->GetDisplayName(pbcfull,NULL,<BR/>
&ppszDisplaynamefull)))
{
wcstombs(objectname,ppszDisplaynamefull,300);
AfxMessageBox(ExtractDocumentName(objectname));
ptrfullMoniker->Release();
}
}
pbcfull->Release();
}
pOleClientSite->Release();
}
}char* CMyActiveXCtrl::ExtractDocumentName(char* objectname)
{
char* ptrchar;
char* ptrdocname;
//reverse the string
ptrchar = _strrev(objectname);
//Ignore the first token , this is the name of the embedded object
strtok(ptrchar,"!");
//get the remainder of the string and reverse it to get the Document name
ptrdocname = strrev(strtok(NULL,"\0"));
return ptrdocname;
}
不过,我更想要的是,我的控件是嵌入在word里面,如何通过GetClientSite函数的方式,
来获包容器的指针!
能不能详细介绍一下呢,,