How To Pass a COleDispatchDriver as an Argument for a Method Expecting a VARIANT (253501)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002
  • Microsoft Excel 2000
  • Microsoft Excel 97 for Windows
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002
  • Microsoft PowerPoint 2000
  • Microsoft PowerPoint 97 for Windows
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • The Microsoft Foundation Classes (MFC)

This article was previously published under Q253501

SUMMARY

Some methods require that you pass a VARIANT that represents an Automation object. With MFC, these objects are typically handled by COleDispatchDriver-derived classes. To pass one of these to a method expecting a VARIANT, you can create a new VARIANT with its vt member set to VT_DISPATCH and its pdispVal member set to the COleDispatchDriver class's m_lpDispatch.

MORE INFORMATION

The following sample uses Microsoft Excel as the Automation server; however, the technique that is illustrated can be used with other Microsoft Office applications as well.

Sample Code

Microsoft Excel has several methods that require an object as a parameter. One such method is Worksheet::Add(). The following sample code demonstrates passing a COleDispatchDriver as a parameter to the Worksheet::Add() method:
   // Start Excel...
   _Application app;
    
   COleException e;
   if(!app.CreateDispatch("Excel.Application", &e)) {
      char buf[80];
      sprintf(buf, "Error on CreateDispatch(): %ld (%08lx)", e.m_sc, e.m_sc);
      AfxMessageBox(buf, MB_SETFOREGROUND);
      return;
   }
    
   // Make it visible...
   app.SetVisible(TRUE);
    
   // Add a workbook...
   COleVariant covOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
   Workbooks books(app.GetWorkbooks());
   _Workbook book(books.Add(covOpt));
    
   // Get sheets collection of book1...
   Worksheets sheets(book.GetSheets());
    
   // Get a reference to sheet1...
   _Worksheet sheet1(sheets.GetItem(COleVariant((short)1)));
    
   // encapsulate sheet1 object in a VARIANT
   VARIANT sheet1Var = {0};
   sheet1Var.vt = VT_DISPATCH;
   sheet1Var.pdispVal = sheet1.m_lpDispatch;
   sheet1.m_lpDispatch->AddRef();
    
   // Add a sheet just after sheet1. Excel's Sheets.Add method
   // requires the 'After' parameter to be a sheet object, not
   // a sheet name or index.
   _Worksheet newSheet(sheets.Add(covOpt, sheet1Var, covOpt, covOpt));
    
   AfxMessageBox("All done.", MB_SETFOREGROUND);
    
   // Cleanup...
   VariantClear(&sheet1Var);
   book.SetSaved(TRUE);
   app.Quit();
				

REFERENCES

For additional information on creating an MFC Automation client for Microsoft Office applications, click the following article number to view the article in the Microsoft Knowledge Base:

178749 How To Create Automation Project Using MFC and a Type Library

For additional information and sample for developing Office solutions, please visit:

Modification Type:MinorLast Reviewed:7/24/2006
Keywords:kbAutomation kbhowto KB253501