Solved

Error when trying to copy worksheet to another workbook

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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