?
Solved

copying excel sheets using automation

Posted on 1999-01-14
1
Medium Priority
?
308 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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

801 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