Microsoft KB Archive/253501

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


How to pass a COleDispatchDriver as an argument for a method expecting a VARIANT

Article ID: 253501

Article Last Modified on 5/14/2007



APPLIES TO

  • Microsoft Office Word 2007
  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
  • Microsoft Word 97 Standard Edition
  • Microsoft Office Excel 2007
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Office PowerPoint 2007
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Foundation Class Library 4.2



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 that has the vt member set to VT_DISPATCH and the pdispVal member set to the m_lpDispatch of the COleDispatchDriver class.

MORE INFORMATION

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

Sample code

Excel has several methods that require an object as a parameter. One method is the Worksheet::Add method. 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 more information about how to create 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 an automation project using MFC and a type library


For more information and for a sample for developing Office solutions, visit the following Microsoft Web sites:


Additional query words: WD2007 WD2003 XL2003 XL2007 PPT2003 PPT2007

Keywords: kbautomation kbhowto KB253501