Solved

copying excel sheets using automation

Posted on 1999-01-14
1
298 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
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now