Results 1 to 3 of 3

Thread: [RESOLVED] Error when trying to reopen excel file after closing

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    10

    Resolved [RESOLVED] Error when trying to reopen excel file after closing

    Okay so I'm able to open an embedded excel file in my form, edit it, save and close it but when I open it back up I get the error:

    COM object that has been separated from its underlying RCW cannot be used.

    From what I understand this is because I closed the process but other than that I have no idea as I'm only a beginner.

    Code:
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Excel
    Imports Microsoft.Office
    Imports Microsoft.Office.Core
    Imports System.Runtime.InteropServices
    
    
    Public Class Form1
    
        Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
        Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Private Const WM_SYSCOMMAND As Integer = 274
        Private Const SC_MAXIMIZE As Integer = 61488
    
        Private Sub releaseObject(ByVal obj As Object)
            Try
    
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
    
    
    
            Catch ex As Exception
    
            Finally
                obj = Nothing
    
    
            End Try
        End Sub
    
        Dim APP As New Excel.Application
        Dim worksheet As Excel.Worksheet
        Dim workbook As Excel.Workbook
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            OpenFileDialog1.ShowDialog()
            workbook = APP.Workbooks.Open(OpenFileDialog1.FileName)
        
    
    
            SetParent(APP.Hwnd, Panel1.Handle)
            SendMessage(APP.Hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
        End Sub
    
    
    
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            workbook.Save()
            workbook.Close()
            APP.Quit()
    
    
            releaseObject(worksheet)
            releaseObject(workbook)
            releaseObject(APP)
        End Sub
    Any help would be greatly appreciated!

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Error when trying to reopen excel file after closing

    You quit the excel application in Button3_Click (shame on your naming!). Then you attempt to reuse it in Button2_Click. I think if you initialize the object everytime, it may solve your problem.

    Code:
        Dim APP As Excel.Application
        Dim worksheet As Excel.Worksheet
        Dim workbook As Excel.Workbook
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            APP = New Excel.Application
    
            OpenFileDialog1.ShowDialog()
            workbook = APP.Workbooks.Open(OpenFileDialog1.FileName)
        
    
    
            SetParent(APP.Hwnd, Panel1.Handle)
            SendMessage(APP.Hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
        End Sub
    
    
    
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            workbook.Save()
            workbook.Close()
            APP.Quit()
    
    
            releaseObject(worksheet)
            releaseObject(workbook)
            releaseObject(APP)
        End Sub
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    10

    Re: Error when trying to reopen excel file after closing

    This worked perfectly, thank you

    I even renamed my buttons!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width