Solved

Error when trying to copy worksheet to another workbook

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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