Solved

copying excel sheets using automation

Posted on 1999-01-14
1
307 Views
Last Modified: 2010-04-02
i'm using automation to automate excel and do a few things automatically.. (isn't auto such a great word?) anyway, i'm trying to copy a worksheet from one workbook to another, but i can't figure out how.. documentation on this is pretty vague.. the program is actually an mfc based program, but to automate excel, i just created the excel classes from the excel type library..

i figure i need to use the worksheet::add function, BUT i don't understand the parameters if that's what i'm really supposed to use..

any suggestions?
0
Comment
Question by:adresko
[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
1 Comment
 

Accepted Solution

by:
izi13 earned 50 total points
ID: 1182114
Hi,
I don't know if you still need the answer, but anyway here is a function, which can help you:

void copy()
{
      _Application            app;
      COleVariant            wsSource("Sheet1"), // name of sheet to copy
                        wsDest("Sheet2");      // name of sheet to copy before
      

      LPCTSTR wbSource="Book1.xls";      // name of source workbook
      LPCTSTR wbDest="Book2.xls";            // name of destination workbook

      if (!app.CreateDispatch("Excel.Application"))
      {
            MessageBox("Failed to instantiate Excel");
            return;
      }

      try
      {

            Workbooks wbs(app.GetWorkbooks());

            _Workbook wbToCopyFrom(wbs.Open(wbSource,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing));
            _Workbook wbToCopyTo(wbs.Open(wbDest,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing));

            Worksheets wsheetsSource(wbToCopyFrom.GetWorksheets());
            Worksheets wsheetsDest(wbToCopyTo.GetWorksheets());

            _Worksheet wsToCopy(wsheetsSource.GetItem(wsSource));
            _Worksheet wsToCopyBefore(wsheetsDest.GetItem(wsDest));

            VARIANT            var;
            var.vt=VT_DISPATCH;
            var.pdispVal=wsToCopyBefore.m_lpDispatch;

            wsToCopy.Copy(var,vtMissing);
            wbToCopyTo.Save();

      }
      catch(COleDispatchException* e)
      {
            TRACE(e->m_strDescription);
            MessageBox(e->m_strDescription,e->m_strSource, MB_OK);
      }

      app.Quit();
      app.ReleaseDispatch();
}

0

Featured Post

Independent Software Vendors: 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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

688 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