Hi guys
When I put code into the button "excel" to generate schedule in a loan calculator,
I had, and still have errors, mostly: " excel.sheet,excel application not defined..., excel not declared".
I wrote the imports line, and I added the appropriate reference to excel.However, there's errors.
I don't know how to fix the problem. See attched file.
Please help.
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Public Class Form1
Dim dLoan, dRate, dPayment As Double
Dim iYears As Integer
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnCompute.Click
dLoan = CDbl(txtLoan.Text.Trim)
dRate = CDbl(txtRate.Text.Trim)
txtLoan.Text = dLoan.ToString("N2")
If dRate > 1 Then dRate = dRate / 100
iYears = CInt(nudYear.Value)
dPayment = Pmt(dRate / 12, _
iYears * 12, -1 * dLoan)
txtPayment.Text = _
Format(dPayment, "#,##0.00")
txtPayment.Refresh()
End Sub
Private Sub btnExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnExcel.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("C:\Users\Pieter\Documents\Pieter.xls")
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
'display the cells value B2
MsgBox(xlWorkSheet.Cells(2, 2).value)
'edit the cell with new value
xlWorkSheet.Cells(1, 1) = txtLoan.Text
xlWorkSheet.Cells(1, 2) = txtRate.Text
xlWorkSheet.Cells(1, 3) = nudYear.Text
xlWorkSheet.Cells(1, 4) = txtPayment.Text
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnExit.Click
Me.Close()
End Sub
Again, I truly thank you for your quick respond and help.
But I still have errorsambiguous 'application' in space name...')
Just a question: have you run the application after modifying the code?
Or does the application work fine for you?
Again, I truly thank you for your quick respond and help.
But I still have errorsambiguous 'application' in space name...')
Just a question: have you run the application after modifying the code?
Or does the application work fine for you?
The app works fine for me.
Maybe you should recreate the from and implant the code.
You do have to have the excel file already existing in the given path.
Pieter