Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 253501 - Last Review: May 14, 2007 - Revision: 8.1
How to pass a COleDispatchDriver as an argument for a method expecting a VARIANT
This article was previously published under Q253501
On This Page
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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=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:
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
| kbautomation kbhowto KB253501 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please