Solved

Error when trying to copy worksheet to another workbook

Posted on 2010-09-09
1
418 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
[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:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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