I am trying to print a receipt from Excel
but unable to do so. I have tried release in reverse but I seems can't find what is missing. Kindly help! Thank you!
This is what i have done so far :
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
#Region "dim"
Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
Dim xlPath = IO.Path.Combine(exeDir.DirectoryName, "SampleReceipt.xls")
Dim app As Excel.Application = Nothing
Dim books As Excel.Workbooks = Nothing
Dim book As Excel.Workbook = Nothing
Dim sheets As Excel.Sheets = Nothing
Dim sheet As Excel.Worksheet = Nothing
Dim cell As Excel.Range = Nothing
#End Region
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch ex As Exception
o = Nothing
Finally
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
app = New Excel.Application()
books = app.Workbooks
book = books.Open(xlPath)
sheets = book.Sheets
sheet = book.Sheets(1)
cell = sheet.Range("A1")
cell.Value = "Lorem Ipsum"
book.SaveAs("C:\Temp\ExcelBook.xls")
book.Close()
app.Quit()
Finally
NAR(cell)
NAR(sheet)
NAR(sheets)
NAR(book)
NAR(books)
NAR(app)
End Try
End Sub
End Class