+ Reply to Thread
Results 1 to 5 of 5

Copy a worksheet throws "Exception from HRESULT: 0x800A03EC."

  1. #1
    i3tech
    Guest

    Copy a worksheet throws "Exception from HRESULT: 0x800A03EC."

    Hi,
    While iam trying to copy a worksheet from one file to another file in C#,
    VS.NET 2003 i get error message saying: "Exception from HRESULT:
    0x800A03EC."

    The Exact Code i used is:
    Excel.Application xlApp1 = new Excel.ApplicationClass();
    Excel.Application xlApp2 = new Excel.ApplicationClass();
    object missing = Type.Missing;

    xlApp1.Workbooks.Open(System.Windows.Forms.Application.StartupPath +
    @"\Reports.xls" ,
    missing , missing, missing, missing, missing,missing, missing, missing,
    missing, missing, missing, missing, missing, missing);
    xlApp2.Workbooks.Open(System.Windows.Forms.Application.StartupPath +
    @"\Template.xls" ,
    missing , missing, missing, missing, missing,missing, missing, missing,
    missing, missing, missing, missing, missing, missing);

    Excel.Workbook xlSrcBook;
    Excel.Workbook xlDestBook;

    xlSrcBook = xlApp2.ActiveWorkbook as Workbook;
    xlDestBook = xlApp1.ActiveWorkbook as Workbook;

    try
    {
    (xlSrcBook.Sheets[2] as
    Worksheet).Copy(missing,xlDestBook.Sheets[xlDestBook.Sheets.Count]);
    xlApp2.Visible = true;
    }
    catch(Exception ex)
    {
    MessageBox.Show(this,ex.Message);
    }
    finally
    {
    xlSrcBook.Saved = true;
    xlDestBook.Saved = true;
    xlSrcBook.Close(missing,missing,missing);
    xlDestBook.Close(missing,missing,missing);
    xlApp1.Quit();
    xlApp1 = null;
    xlApp2.Quit();
    xlApp2 = null;
    }

    Can someone please help me in finding out the mistake ?

  2. #2
    NickHK
    Guest

    Re: Copy a worksheet throws "Exception from HRESULT: 0x800A03EC."

    I've never tried it in VB/VBA, but I'd be surprised if you can copy/move
    worksheets between instances of Excel.
    Open both workbook in the same instance and you should be OK.

    NickHK

    "i3tech" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    > While iam trying to copy a worksheet from one file to another file in

    C#,
    > VS.NET 2003 i get error message saying: "Exception from HRESULT:
    > 0x800A03EC."
    >
    > The Exact Code i used is:
    > Excel.Application xlApp1 = new Excel.ApplicationClass();
    > Excel.Application xlApp2 = new Excel.ApplicationClass();
    > object missing = Type.Missing;
    >
    > xlApp1.Workbooks.Open(System.Windows.Forms.Application.StartupPath +
    > @"\Reports.xls" ,
    > missing , missing, missing, missing, missing,missing, missing, missing,
    > missing, missing, missing, missing, missing, missing);
    > xlApp2.Workbooks.Open(System.Windows.Forms.Application.StartupPath +
    > @"\Template.xls" ,
    > missing , missing, missing, missing, missing,missing, missing, missing,
    > missing, missing, missing, missing, missing, missing);
    >
    > Excel.Workbook xlSrcBook;
    > Excel.Workbook xlDestBook;
    >
    > xlSrcBook = xlApp2.ActiveWorkbook as Workbook;
    > xlDestBook = xlApp1.ActiveWorkbook as Workbook;
    >
    > try
    > {
    > (xlSrcBook.Sheets[2] as
    > Worksheet).Copy(missing,xlDestBook.Sheets[xlDestBook.Sheets.Count]);
    > xlApp2.Visible = true;
    > }
    > catch(Exception ex)
    > {
    > MessageBox.Show(this,ex.Message);
    > }
    > finally
    > {
    > xlSrcBook.Saved = true;
    > xlDestBook.Saved = true;
    > xlSrcBook.Close(missing,missing,missing);
    > xlDestBook.Close(missing,missing,missing);
    > xlApp1.Quit();
    > xlApp1 = null;
    > xlApp2.Quit();
    > xlApp2 = null;
    > }
    >
    > Can someone please help me in finding out the mistake ?




  3. #3
    i3tech
    Guest

    Re: Copy a worksheet throws "Exception from HRESULT: 0x800A03EC."

    Can you explain it please ?. Iam not getting it.
    if you open in same instance then how will i get two different workbook
    and their worksheets ?. ActiveWorkbook will give me only one workbook to
    copy ?

  4. #4

    Re: Copy a worksheet throws "Exception from HRESULT: 0x800A03EC."

    As Nick wrote you only require one instance of Excel. To get around
    your activeworkbook problem, set the reference when you open the
    workbook then open the new one: IE

    appxl.workbooks.open ("Filename")
    Book1 = Activeworkbook.name
    appxl.workbooks.open ("Filename2")
    Book2 = Activeworkbook.name

    This should then reference the correct books. You can then use
    appxl.Windows(Book1).activate to move back to workbook one etc.

    James

    i3tech wrote:

    > Can you explain it please ?. Iam not getting it.
    > if you open in same instance then how will i get two different workbook
    > and their worksheets ?. ActiveWorkbook will give me only one workbook to
    > copy ?



  5. #5
    i3tech
    Guest

    Re: Copy a worksheet throws "Exception from HRESULT: 0x800A03EC."

    Thank a lot Nick & James. It works great !

    "[email protected]" wrote:

    > As Nick wrote you only require one instance of Excel. To get around
    > your activeworkbook problem, set the reference when you open the
    > workbook then open the new one: IE
    >
    > appxl.workbooks.open ("Filename")
    > Book1 = Activeworkbook.name
    > appxl.workbooks.open ("Filename2")
    > Book2 = Activeworkbook.name
    >
    > This should then reference the correct books. You can then use
    > appxl.Windows(Book1).activate to move back to workbook one etc.
    >
    > James
    >
    > i3tech wrote:
    >
    > > Can you explain it please ?. Iam not getting it.
    > > if you open in same instance then how will i get two different workbook
    > > and their worksheets ?. ActiveWorkbook will give me only one workbook to
    > > copy ?

    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1