Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to retrieve the name of an Office document that contains an MFC ActiveX control


View products that this article applies to.

This article was previously published under Q266318

↑ Back to the top


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.

↑ Back to the top


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;
}
				

↑ Back to the top


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:

↑ Back to the top


Keywords: kbctrl, kbctrlcreate, kbhowto, kbprogramming, KB266318

↑ Back to the top

Article Info
Article ID : 266318
Revision : 8
Created on : 1/27/2007
Published on : 1/27/2007
Exists online : False
Views : 609