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 pass a COleDispatchDriver as an argument for a method expecting a VARIANT


View products that this article applies to.

This article was previously published under Q253501

↑ Back to the top


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.

↑ Back to the top


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();
				

↑ Back to the top


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:

↑ Back to the top


Keywords: kbautomation, kbhowto, KB253501

↑ Back to the top

Article Info
Article ID : 253501
Revision : 10
Created on : 5/14/2007
Published on : 5/14/2007
Exists online : False
Views : 703