Solved

Error when trying to copy worksheet to another workbook

Posted on 2010-09-09
1
411 Views
Last Modified: 2012-05-10
Hello,
I am trying to copy a worksheet and keep getting the following exception error
Exception from HRESULT: 0x800A03EC.  It should be a simple matter of coping the worksheet returned by function GenerateWorksheet to the workbook created by Utilities.CreateWorkbook
Dim dpl As New List(Of DatasetPair)



        'Try

        Me.Cursor = Cursors.WaitCursor

        Dim intSheetCount As Integer = 0

        For i As Integer = 0 To lvMatchedDvid.Items.Count - 1

            Dim dp As New DatasetPair

            dp.DataVersionIdOne = lvMatchedDvid.Items(i).Text.ToString

            dp.DataVersionIdTwo = lvMatchedDvid.Items(i).SubItems(1).Text.ToString

            intSheetCount += 1

            dp.SheetId = intSheetCount

            dpl.Add(dp)

        Next





       



        Dim wb As Excel.Workbook = Utilities.CreateWorkbook()





        Dim strFilePath As String = wb.Path.ToString & "\" & wb.Name.ToString

        MessageBox.Show(strFilePath)

        For Each dp As DatasetPair In dpl

            Dim ws As New Excel.Worksheet

            ws = GenerateWorksheet(dp)



                     ws.Copy(Before:=wb.Worksheets(1))     

           

        Next

Open in new window

0
Comment
Question by:chtullu135
1 Comment
 

Accepted Solution

by:
chtullu135 earned 0 total points
ID: 33641481
I finally figured out the problem and determined that it was caused by trying to copy a worksheet from one instance of Excel to a workbook running in another instance of excel.  Below is the code.  I have another error but will post it as another question
  Dim dpl As New List(Of DatasetPair)



        'Try

        Me.Cursor = Cursors.WaitCursor

        Dim intSheetCount As Integer = 0

        For i As Integer = 0 To lvMatchedDvid.Items.Count - 1

            Dim dp As New DatasetPair

            dp.DataVersionIdOne = lvMatchedDvid.Items(i).Text.ToString

            dp.DataVersionIdTwo = lvMatchedDvid.Items(i).SubItems(1).Text.ToString

            intSheetCount += 1

            dp.SheetId = intSheetCount

            dpl.Add(dp)

        Next



        Dim wb As Excel.Workbook = Utilities.CreateWorkbook()



        Dim strFilePath As String = wb.Path.ToString & "\" & wb.Name.ToString



        MessageBox.Show(strFilePath)

        For Each dp As DatasetPair In dpl

            Dim excelApp As New Excel.Application

            Dim ws As New Excel.Worksheet

            ws = GenerateWorksheet(dp)

            ws.SaveAs(wb.Path.ToString)





            Dim strSourceFilePath As String

            strSourceFilePath = wb.Path.ToString & "\" & wb.Name.ToString

            MessageBox.Show(strSourceFilePath)



            'wb.Close()

            Dim wbkDest As Excel.Workbook = excelApp.Workbooks.Open(strFilePath)





            Dim wbkSource As Excel.Workbook = excelApp.Workbooks.Open(strSourceFilePath)

            ws.Copy(Before:=wbkDest.Worksheets.Count - 1)



           

        Next

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

24 Experts available now in Live!

Get 1:1 Help Now