HOWTO: Insert Objects Without Using Insert Object Dialog Box

Last reviewed: October 10, 1997
Article ID: Q137357
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, versions 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2,

         4.0, 4.1, 5.0
    

SUMMARY

When building an OLE container or OLE server application using MFC OLE classes, you should insert an OLE embedded object programmatically, without using the InsertObject dialog box. This article show you how.

In a default MFC AppWizard-generated OLE container or OLE server application, a command handler is implemented to enable the user to insert an object by clicking Insert New Object on the Edit menu. The AppWizard- generated code makes use of the COleInsertDialog class, which is an MFC wrapper for the OLEUIINSERTOBJECT common dialog box. The COleInsertDialog data and member functions are used to embed the object.

MORE INFORMATION

The member function most responsible for the embedding of an OLE object is COleInsertDialog::CreateItem, which takes a pointer to a COleClientItem as a formal parameter.

When the user clicks Insert New Object on the Edit menu, a COleInsertDialog is created, and it is shown by calling its DoModal function. When the dialog box is dismissed, some of its data members are set by selections made by the user, such as Create From File or Create New.

The implementation of COleInsertDialog::CreateItem calls the COleClientItem member functions to embed the object, which leads to the solution of by- passing the COleInsertDialog class and calling COleClientItem to do the work.

Here is an excerpt from COleInsertDialog::CreateItem:

*******Begin Excerpt*******

   switch (selType)
   {
     case linkToFile:
      // link to file selected
     ASSERT(m_szFileName[0] != 0);
           bResult=pNewItem->CreateLinkFromFile(m_szFileName);
     break;

     case insertFromFile:
     // insert file selected
     ASSERT(m_szFileName[0] != 0);
     bResult=pNewItem->CreateFromFile(m_szFileName);
     break;

     default:
     // otherwise must be create new
     ASSERT(selType == createNewItem);
     bResult=pNewItem->CreateNewItem(m_io.clsid);
     break;
   }

*******End Excerpt*******

This code features a switch structure whose logic flow is controlled by the selType set by the user interaction with the COleInsertDialog dialog box.

The following code demonstrates how to insert an OLE embedded object programmatically. The code shows the creation of an instance of a COleClientItem object, which then calls its CreateNewItem member function to create and embed a Microsoft Excel worksheet.

Sample Code

   /* Compile options needed : None
   */


   void CMyView::OnInsertObject()
   {

     BeginWaitCursor();

     CMyOleClientItem* pItem = NULL;
     TRY
     {
         // Create new item connected to this document.
         CMyDoc* pDoc = GetDocument();
         ASSERT_VALID(pDoc);
         pItem = new CMyOleClientItem(pDoc);
         ASSERT_VALID(pItem);

         // Get Class ID for Excel sheet
         // This is used in creation
         CLSID clsid;
         if(FAILED(::CLSIDFromProgID("Excel.Sheet",&clsid)))
           AfxThrowMemoryException();

         // Create the Excel embedded item
         if(!pItem->CreateNewItem(clsid))
           AfxThrowMemoryException();  // any exception will do
         ASSERT_VALID(pItem);

         // Launch the server to edit the item.
         pItem->DoVerb(OLEIVERB_SHOW, this);

         ASSERT_VALID(pItem);

         // As an arbitrary user interface design, this sets the
         // selection to the last item inserted.

         // TODO: reimplement selection as appropriate for your
         // application

         m_pSelection = pItem;   // set selection to last inserted item
         pDoc->UpdateAllViews(NULL);
     }
     CATCH(CException, e)
     {
         if (pItem != NULL)
         {
           ASSERT_VALID(pItem);
           pItem->Delete();
         }
         AfxMessageBox(IDP_FAILED_TO_CREATE);
     }
     END_CATCH

     EndWaitCursor();
   }

NOTE: When using Visual C++ versions 5.0, 4.0, and 4.1, you need to typecast the 'Excel.Sheet' argument in the previous code sample with the T2COLE macro. In order to use this macro, you need to #include "afxpriv.h" in the view class's implementation file. Also, add the macro
'USES_CONVERSION' in the try block before the following line (which shows
the T2COLE macro added):

   if (FAILED(::CLSIDFromProgID(T2COLE("Excel.Sheet"),&clsid)))
      AfxThrowMemoryException();
 mikefra 9/25/97
Keywords          : MfcOLE kbcode
Technology        : kbMfc kbole
Version           : WINDOWS:1.5,1.51,1.52;WINDOWS NT:2.0,2.1,2.2,4.0,4.1,5.0;
Platform          : NT WINDOWS


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.