?
Solved

Using MFC , Automate Microsoft Excel to create a new workbook and then adding some features in the worksheets

Posted on 2003-03-30
4
Medium Priority
?
1,993 Views
Last Modified: 2007-12-19
" Using MFC , Automate Microsoft Excel to create a new workbook and then adding some features in the worksheets .

I am using MFC of VC++ to Automate excel .
My program is successfully invoking excel sheet through VC++ environment.And i am able to do changes(Read /Write ) in the Excel sheet thru MFC of VC++.
Problem :I am not able to save a particular sheet with some name of a particular workbook of some name .
That is i am not able to give name to workbook created and hence worksheet created .
I am taking the help from following link to automate excel sheet.
Link :http://support.microsoft.com/default.aspx?scid=kb;EN-US;179706
( This link is about HOWTO: Use MFC to Automate Excel and Create/Format a New Workbook )

Could you send me a sample program in which the program can invoke a particular workbook with some name let workbook1 and then can write,read,copy,delete some specific sheet of that workbook1.
It will be helpful for me if you mail me some material on the same to study.
Programs/Software/Hardware:MFC Wizard(exe),Window 98,Excel 2000.

Program code :
void CAutoProjectDlg::OnRun()
{
// TODO: Add your control notification handler code here
// Commonly used OLE variants.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);


_Application app;
app.o
Workbooks books;
_Workbook book;


Worksheets sheets;
_Worksheet sheet;
Range range;
Font font;
Range cols;

// Start Excel and get Application object.

if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel and get Application object.");
return;
}


//Get a new workbook.
//books = app.GetWorkbooks();
books = app.GetWorkbooks ();
book = books.Add (covOptional);

//Get the first sheet.
sheets =book.GetSheets();
sheet = sheets.GetItem(COleVariant((short)1));
//Here now the moment i am trying to save this workbook or its respective sheet i am not getting the respective method and thus not able to do the same.
rest code is working successfully as given below.
range = sheet.GetRange(COleVariant("A1"),COleVariant("A1"));
range.SetValue(COleVariant("First Name"));
range = sheet.GetRange(COleVariant("B1"),COleVariant("B1"));

Waiting for reply!
Thanks
0
Comment
Question by:naresh1508
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Author Comment

by:naresh1508
ID: 8236440
Even Earlier i mailed you my this question but no response .
Waiting for ur reply !
0
 
LVL 2

Accepted Solution

by:
alligatorsql earned 120 total points
ID: 8707825
Hello,

i have not idea how to save an EXCEL sheet without changes. In my project it does not work ... but
if you first change the sheet and the use the following code, your EXCEL sheet will be saved

//Get the first sheet.
sheets =book.GetSheets();
sheet = sheets.GetItem(COleVariant((short)1));

// no SAVE here

rest code is working successfully as given below.
range = sheet.GetRange(COleVariant("A1"),COleVariant("A1"));
range.SetValue(COleVariant("First Name"));
range = sheet.GetRange(COleVariant("B1"),COleVariant("B1"));

// save your sheet here cause you have made changes to the sheet
COleVariant oFileName("TEST.XLS");
COleVariant oFormat((short)33);    // typical excel format xls
COleVariant vtMissing((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant oSave((long)FALSE, VT_BOOL);

book.SaveAs(oFileName, oFormat, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
book.Close(vtMissing, vtMissing, vtMissing);

That´s it ...

You can also save the sheet in the close method :

COleVariant oSave((long)TRUE, VT_BOOL);

// book.SaveAs(oFileName, oFormat, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
book.Close(oSave, oFileName, vtMissing);


Hope that helps ?

Manfred Peter
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9551839
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: alligatorsql

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question