Doing Excel Automation witch MSVC
Please bear in mind, that I will describe one special task here: storing some information as an Excel sheet with auto-sized coloumns. I am using MSVC 5.0 and the MFC.
I already have a prepared text file containing the information I would like to store. I did this using semikolon (;) separated ascii-output with new-line\carriage-return pairs at the end of each line. (To say it in plain MFC, I used the CStdioFile class with the WriteString method)
If you are working with the Excel classes, you need to deal with the VARIANT data type or, more convient when using MFC, with the COleDispatch class, which has a built-in interface and is more easy to use.
A caveat of this approach that you have to modify the automation classes by hand, but this caveat is supersed by the ability of removing unneeded (and sometime ununderstood) parameters from the method calls.
Before going through the step-by-step procedure, something tells me that you probably like to download my project file (96K).
Step zero: Record your task as Excel macro
First of all, you will have to be used to Excel's VBA macro language. It is VERY helpful if you first do the task you'd like to do in Excel while recording this task as a macro.The macro will look somewhat like this, hence, that VBA is nicely object
oriented:
Sub Macro1()
Workbooks.OpenText Filename:="C:\Example.txt",
Origin:= _
xlWindows, StartRow:=1,
DataType:=xlDelimited, TextQualifier _
:=xlDoubleQuote, ConsecutiveDelimiter:=False,
Tab:=False, _
Semicolon:=True, Comma:=False,
Space:=False, Other:=False, _
FieldInfo:=Array(1,
1)
Cells.Select
Selection.Columns.AutoFit
Workbook(1).SaveAs Filename:="C:\temp\README.xls",
FileFormat:= _
xlNormal, Password:="",
WriteResPassword:="", _
ReadOnlyRecommended:=False,
CreateBackup:=False
ActiveWindow.Close
End Sub
Step one: Create the Excel automation classes
In your Excel program directory you will find the Type Library for the Excel automation. I think, all of you will have the english version, Xl5en32.olb, along with perhaps some localized version of this (e.g. the german version is shipped with a Xl5de32.olb).Using the Class Wizard, choose "New Class" and "from a type library"
(I have the german version, so I don't know the exact english menu commands).
You are then asked to enter the path and filename of the type library.
Having done you get a list of classes. Looking at the Excel macro you see
which classes are needed. I needed five flasses (Application, Range, Workbook,
Workbooks, Worksheet). When in doubt you may choose all classes, but then
you have a bunch of classes that you don't need.
Step two: Add methods for opening and closing Excel
In your class needing Excel create a member variable holding the Excel's application objectclass CMyDemoDlg : public
CDialog
{
// the ususal things
...
private:
Application m_Excel_en;
...
// other usual things
}
and the two functions OpenExcel() and CloseExcel() as follows:
bool CMyDemoDlg::OpenExcel()
bool CMyDemoDlg::CloseExcel()
{
if (!m_bExcelStarted)
// this is a member-variable of type bool
if (m_Excel.CreateDispatch("Excel.Application"))
{
//
// un-comment these lines, is you want to
see what Excel is doing
//
//
CWnd *pExelWnd = CWnd::FindWindow("XLMAIN", NULL);
//
if (pExelWnd )
//
{
//
pExelWnd->ShowWindow(SW_SHOWNORMAL);
//
pExelWnd->UpdateWindow();
//
pExelWnd->BringWindowToTop();
//
}
//
BringWindowToTop();
m_bExcelStarted=true;
}
return m_bExcelStarted;
}
{
if (m_bExcelStarted)
{
m_Excel_en.Quit();
m_Excel_en.ReleaseDispatch();
m_bExcelStarted=false;
}
return m_bExcelStarted;
}
In you application class add to the following lines to InitInstance():
// do all the other things
BOOL CMyDemoApp::InitInstance()
{
// initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return
FALSE;
}
...
return TRUE;
}
In your stdafx-Header you need to add the following line to include
the MFC automation support
...
#include <afxdisp.h>
// MFC OLE-automation classes
...
Step three-and-four-combined: Start building the Excel macro as C++ Code while modifying the automation classes.
For every parameter you need, create a local variable of type COleVariant. In Excel's VBA you can omit parameter not needed. In the Excel automation classes you can omit the parameters you do not need IF AND ONLY IF they are at the end of the method's parameters list. In the Excel-class create a new overloaded member function with fewer parameters.All automation functions, whose return value is used to create another automation object should be modified to return a LPDISPATCH pointer rather than a VARIANT!
So, for example, you should take the method
VARIANT Application::Worksheets(const VARIANT&
Index)
{
VARIANT result;
static BYTE parms[]
= VTS_VARIANT;
InvokeHelper(0x1ee, DISPATCH_METHOD, VT_VARIANT,
(void*)&result, parms, &Index);
return result;
}
and create two new methods (the second with an omitted parameter)
LPDISPATCH Application::Workbooks(const VARIANT& Index)
LPDISPATCH Application::Workbooks()
{
// function changed by
T.B.
LPDISPATCH result;
static BYTE parms[]
= VTS_VARIANT;
InvokeHelper(0x23c, DISPATCH_METHOD, VT_DISPATCH,
(void*)&result, parms,
&Index);
return result;
}
{
// new by T.B.
LPDISPATCH result;
InvokeHelper(0x23c, DISPATCH_METHOD, VT_DISPATCH,
(void*)&result, NULL);
return result;
}
To keep in mind, that you did something with the type library it is good practice to enter a short comment saying "I did something".
Do this anytime you are using a return value implicit or explicit.
My macro, ported to C++ now looks like this
void CMyDemoDlg::DoExcelConversion(CString
File)
COleVariant FilenameOpen(File),
TRY
Wbs.OpenText(FilenameOpen, Origin,
StartRow, DataType,
Range ran(m_Excel_en.Columns(COleVariant("A:Z")));
Workbook Wb(m_Excel_en.Workbooks(COleVariant((short)1)));
Wb.SaveAs(FilenameSave, Fileformat);
Wb.Close(Save);
// perhaps
you want to delete the text file,
{
CString XlsFile =
Do.Some.Operations.To.Calculate.The.Excel.File.Name(File);
// or say simply:
// XlsFile="C:\\temp\\demo.xls";
FilenameSave(XlsFile),
Origin((short)2),
// xlWindows
StartRow((short)1),
DataType((short)1),
// xlDelimited
TextQualifier((short)1),
// xlDoubleQuote
ConsecutiveDelimiter((long)FALSE,
VT_BOOL),
Tab((long)FALSE,
VT_BOOL),
Semicolon((long)TRUE,
VT_BOOL),
Fileformat((short)33),
// xlExcel4
Save((long)FALSE,
VT_BOOL);
{
Workbooks Wbs(m_Excel_en.Workbooks());
TextQualifier, ConsecutiveDelimiter, Tab, Semicolon);
ran.AutoFit();
// because
now you have an Excel sheet
// DeleteFile(File);
}
CATCH(COleDispatchException, e)
{
TRACE(e->m_strDescription);
MessageBox("Error
creating Excel-file:\n"+e->m_strDescription,
"My Demo Dialog", MB_OK);
}
END_CATCH;
}
Some people wondered how I know the numerical value of the Excel constants, like xlWindows, xlExcel4 and so on. This is simple, but not trivial. Follow this step by step procedure:
A) Run Excel with an empty (or not) worksheet
B) Use Insert - Macro - Visual Basic Module
C) Type in a short dummy function. For example
Sub Dummy()
test = 1
End Sub
Make sure you have the proper language settings, this is important only for users having a localized (non-english) version. For these languages Excel allows you to use the VBA with language-dependend keywords. If you are somewhat used in Basic with it'`s english keywords then it feels HORRIBLE to use the (badly translated) german-or-whatever-language-you-use keywords :)
[For our german-tongued friends:
For i=1 To 11 Step 2
Do.Some.Thing()
Next i
will become
F|r i=1 bis 11 Schrittweite 2
Tue.Irgend.Etwas()
Ndchste i
horrible...... :( ]
D) Put the cursor back to the second line (test=1) and start the single step debugger(F8 key on my german system, or use Execute - single Step)
E) The debugger window pops up. Go to the "Watch" tab, make a right mouse click into the upper half (the watch area) and choose "Add Watch", just like you do in DevStudio. As the watch expression just enter the constant you need the value of, for example xlExcel4.
F) Ready. The watch window now tells you the value of xlWhateverYouLike
Some people wondered how I know the numerical value of the Excel constants. This is easy: just start excel with a short makro like this one:
Sub dummy()
anotherDummy = 1
End Sub
Then trace into this macro with the single step debugger, go to the watch window and add as watch variable the constant you need. If you enter xlWindows,
for example the watch window will tell you it has the value 2. There you are!
Step five: At the end, put all together
Now you are nearly done. Somehow retrieve the full path of the file you wish to convert and do the following tree lines: OpenExcel(); // may
be called more than one time
DoExcelConversion(FullPathName);
CloseExcel(); // may be
called without a preceeding OpenExcel()
Now, at the end of my 200 lines page, I hope you are successful creating
your Excel classes and all the rest. If you someday have created a neatly
sold application using this knowledge and have made a billion dollar, remember
me and send me a check :)
IMPORTANT NOTE (due to a lot of emails concerning this): this demonstration code DOES NOT WORK with Excel 97. It was only tested and proved working with Excel 5.0 and Excel 7.0 (some call it Excel 95) on both Windows NT 4.0 and Windows 95. I do not have Excel 97, if anyone is willing to sponsor me a licence, so please, and I will try to do a good job.... If you find any errors or typos, please feel free to drop a mail.
Posted: 15 May 1998

Comments
FNwJU Zey gcBH
Posted by eHSrqxUHoW on 04/17/2013 01:05ambuy tramadol online tramadol ls 100 mg - tramadol withdrawal after 3 weeks
Replyworker
Posted by Craig Lytle on 05/02/2012 06:31amcan u get me a workinging sp7en32.olb mine doesn't work
Replyhttp://www.ucancode.net
Posted by Legacy on 11/14/2003 12:00amOriginally posted by: UCCDraw ActiveX Control is an ActiveX control that allows creation and editing of Visio-style charts from within your application. Allows you to create flow charts, vector drawings, raster images and more with the ability to include hyperlinks and various shading and coloring effects. You can group objects together, include images and text, link them together and apply custom drawing effects to create charts similar to Microsoft Visio, Adobe Illustrator, and CorelDRAW
Great!
Replyhow to avoid Oledlg prompt when use VC++6.0 to access Excel
Posted by Legacy on 11/11/2003 12:00amOriginally posted by: jason
ReplyHow to kill excel.exe from memory?
Posted by Legacy on 09/23/2003 12:00amOriginally posted by: Mottet
ReplyExcel 2000 automation with MFC
Posted by Legacy on 06/30/2003 12:00amOriginally posted by: Rajesh
Hello,
I am trying to automate the process of importing text data from a text file with delimiters as tabs.But when i follow the code suggested by thomas.I doubt at
LPDISPATCH Application::Workbooks()
{
// new by T.B.
LPDISPATCH result;
InvokeHelper(0x23c, DISPATCH_METHOD, VT_DISPATCH, (void*)&result, NULL);
return result;
}
I am getting an error member not found.I dont know where I have done wrong as this is the first time I am playing with excel.Can any body please suggest me what might be wrong.
Thanks in advance,
Rajesh Kanaparti
-
Replylearning
Posted by qiaoli on 09/23/2009 11:34pmI am just a beginning learner!
ReplyExcel 8 automation
Posted by Legacy on 10/07/2002 12:00amOriginally posted by: Thomas Blenkers
I have just stumbled about this link providing at least some help to automate something newer than XL 97:
http://ourworld.compuserve.com/homepages/John_Maddock/
But still sorry, I can't help automating s.th. other than described here!
Thomas
ReplyThings have changed with newer versions of Excel
Posted by Legacy on 04/05/2002 12:00amOriginally posted by: Nathan Schultz
Things have changed dramatically in Excel 2000 and Excel 2002. You don't need to do a lot of these things if you use the correct type libraries for the later versions. Here is a list of the type libraries:
ReplyOffice Application Type library
Word 95 and prior wb70en32.tlb
Excel 95 and prior xl5en32.olb
PowerPoint 95 and prior PowerPoint.tlb
Access 95 and prior msaccess.tlb
Binder 95 binder.tlb
Schedule+ sp7en32.olb
Project pj4en32.olb
Team Manager mstmgr1.olb
Word 97 msword8.olb
Excel 97 excel8.olb
PowerPoint 97 msppt8.olb
Access 97 msacc8.olb
Binder 97 msbdr8.olb
Graph 97 graph8.olb
Outlook 97 msoutl8.olb
Outlook 98 msoutl85.olb
Word 2000 msword9.olb
Excel 2000 excel9.olb
PowerPoint 2000 msppt9.olb
Access 2000 msacc9.olb
Outlook 2000 msoutl9.olb
Word 2002 msword.olb
Excel 2002 excel.olb
PowerPoint 2002 msppt.olb
Access 2002 msacc.olb
Outlook 2002 msoutl.olb
Images and shapes in EXCEL
Posted by Legacy on 02/12/2002 12:00amOriginally posted by: Paul
ReplyNo need to change the return type from VARIANT to LPDISPATCH
Posted by Legacy on 02/18/2001 12:00amOriginally posted by: Jiju
There is no need to change the return type of all automation functions, whose return value is used to create another automation object to LPDISPATCH pointer rather than a VARIANT. Instead the pdispVal member in the returned VARIANT can be used to create the required automation object using the AttachDispatch() function.
eg:
VARIANT result
Workbook XLActiveBook;
result = m_Excel.GetActiveWorkbook();
XLActiveBook.AttachDispatch(result.pdispVal);
ReplyRgds,
Jiju
Loading, Please Wait ...