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.
Any help would be greatly appreciated!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




Reply With Quote
