Solved

Error when trying to copy worksheet to another workbook

Posted on 2010-09-09
1
410 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

14 Experts available now in Live!

Get 1:1 Help Now