0

i am very new to VB.net and i'm trying to proceed step by step with my application. The application i'm trying to build will collect a series of macros i've written in Excel VBA environment. Now, the following code pasted below, is the initial part, where basically i try to load a workbook (to be used as Active workbook) and to "unload it". The issue comes when, after "unloading" the workbook, i try to open the very same workbook in excel. Excel application return an error that is "Open in read-only". This cannot be accepted, and i need to understand how to unload the workbook and release it from the myAPP.

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Dim workbook As Excel.Workbook
    Dim worksheet As Excel.Worksheet
    Dim APP As New Excel.Application

Private Sub opn_btn_Click(sender As Object, e As EventArgs) Handles opn_btn.Click
    Dim strname As String
    Dim cellname As String

    With OpenFileDialog1
        .InitialDirectory = "E:\Vs_Excel"
        .Title = "Open xlsx file"
        .ShowDialog()
    End With

    workbook = APP.Workbooks.Open(OpenFileDialog1.FileName)
    worksheet = workbook.Worksheets("sheet1")
    cellname = worksheet.Range("A1").Value

    strname = OpenFileDialog1.FileName
    Me.TextBox1.Text = strname
    Me.TextBox2.Text = cellname

    Dim lvwReport As View = View.List
    With Me.ListView1
        .GridLines = True
        .View = lvwReport
        .CheckBoxes = True
    End With

    'LoadListView() 'thi sroutine is written but not used yet. Must solve first the problem wioth closing ExcelApplication

End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    workbook.Close()
    APP.Quit()

    Me.TextBox1.Text = ""
    Me.TextBox2.Text = ""

    ReleaseObject(worksheet)
    worksheet = Nothing
    ReleaseObject(workbook)
    workbook = Nothing
    ReleaseObject(APP)
    APP = Nothing

End Sub
Private Sub ReleaseObject(ByVal obj As Object)
    Try
        Dim intRel As Integer = 0
        Do
            intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        Loop While intRel > 0
        MsgBox("Final Released obj # " & intRel)
    Catch ex As Exception
        MsgBox("Error releasing object" & ex.ToString)
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub
End class
Mesut Akcan
  • 899
  • 7
  • 19
  • Do you observe this behaviour [when you debug](https://stackoverflow.com/a/17131389/11683)? Does it happen in the release build? And note that you [don't need](https://stackoverflow.com/a/25135685/11683) to manually `ReleaseComObject`s. – GSerg Apr 12 '20 at 19:31
  • I see that during the debug. I just started to write code for the app. – Daniele Amadei Apr 12 '20 at 21:25
  • Google is your friend. Look what I found by typing "Microsoft.Office.Interop.Excel close xls" https://stackoverflow.com/questions/17367411/cannot-close-excel-exe-after-interop-process . It shows that proper sequencing of Open and matching Close must be followed. – donPablo Apr 13 '20 at 00:44
  • @donPablo Please click the second link from my comment above. – GSerg Apr 13 '20 at 06:56
  • @DanieleAmadei It is supposed to happen when you debug, like you will find by visiting both links from my comment above. It will not happen in a release build. – GSerg Apr 13 '20 at 06:57
  • Thanks Gserg and donpablo, both very useful comments and tips. If i use the release version i don't see any problem. Thanks again! – Daniele Amadei Apr 13 '20 at 08:57

0 Answers0