Article ID: 266318
Article Last Modified on 1/27/2007
APPLIES TO
- Microsoft Excel 2000 Standard Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Foundation Class Library 4.2
- Microsoft Excel 97 Standard Edition
- Microsoft PowerPoint 97 Standard Edition
- Microsoft Word 97 Standard Edition
- Microsoft PowerPoint 2000 Standard Edition
- Microsoft Word 2000 Standard Edition
- Microsoft Excel 2002 Standard Edition
- Microsoft PowerPoint 2002 Standard Edition
- Microsoft Word 2002 Standard Edition
This article was previously published under Q266318
SUMMARY
This article describes how to get the name of the Office document that contains an MFC ActiveX control. You might need the document name if the ActiveX control uses the object model of an Office container and calls a method or property that requires the document name.
MORE INFORMATION
One technique for retrieving the name of the document containing your ActiveX control is to obtain a Dispatch pointer for the top level Application object and then use Automation to determine the name of the active document. For example, if a Microsoft Excel workbook is the host document, you can use:
Application.ActiveWorkBook.ActiveSheet.Name
If a Microsoft Word document is the host document, you can use:
Application.ActiveDocument.Name
However, because Microsoft Word 97, Excel 97, Excel 2000, and Excel 2002 are multiple-document interface (MDI) applications, it is possible for a user to open more than one document in the same instance of the application. In this situation, you cannot be certain that the document that is hosting your ActiveX control is the active document.
A more reliable technique for retrieving the name of the document containing your MFC ActiveX control is to use the COleControl::GetClientSite() method to get the current client site of the container, then use IOleClientSite::GetMoniker() to get the full moniker for the client site and, finally, use IMoniker::GetDisplayName() to get the display name of the moniker. IMoniker::GetDisplayName() returns the display name in a form such as:
ExcelWorkBookName!SheetName!ObjectName
-or-
WordDocumentName!ObjectName
You can then parse this display name to retrieve the name of the host document.
The following function, when added to the COleControl derived class of an ActiveX control, demonstrates this technique:
void CMyActiveXCtrl::GettheNameofContainerDocument() { IMoniker* ptrfullMoniker = NULL; char objectname[300]; LPOLESTR ppszDisplaynamefull; IBindCtx* pbcfull = NULL; LPOLECLIENTSITE pOleClientSite = GetClientSite(); if(pOleClientSite) { pOleClientSite->AddRef(); 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; }
REFERENCES
For additional information on ActiveX Controls in Office, click the article numbers below to view the articles in the Microsoft Knowledge Base:
168392 OFF97: Limitations of ActiveX Control Support in Office Document
190985 How To Get IDispatch of an Excel or Word Document from an OCX
243240 PRB: MFC ActiveX Control Fails to Insert into PowerPoint 2000
For additional information and samples for developing Office solutions, please visit the following Microsoft Web sites:
Additional query words: ocx embed embedded
Keywords: kbctrl kbctrlcreate kbhowto kbprogramming KB266318