1. I try to connect vb.net and vba by this code
This is my vb.net code
Imports Microsoft.Office.Core Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oBooks As Excel.Workbooks
Dim oSheets As Excel.Sheets
Dim oSheet As Excel.Worksheet
Try
Dim FirstName As String
FirstName = TextBox1.Text
oExcel = CreateObject("Excel.Application")
oBooks = oExcel.Workbooks
oExcel.Visible = True 'if you want to see it FALSE if not
oBook = oBooks.Open("C:\Documents and Settings\Administrator\Desktop\7typesexcel10032012.xlsm")
DirectCast(DirectCast(oExcel.Workbooks(1).Sheets(1), Excel.Worksheet).Cells(5, "B"), Excel.Range).Value = FirstName
oSheet = oBook.Worksheets(1)
'oSheet.Range(oSheet.Cells(1, 1)).Value = Name
oExcel.Run("Solver.xlam!Aut
pen")
oExcel.AddIns.Add("C:\Program Files\Microsoft Office\OFFICE12\Library\SOLVER\SOLVER.XLAM")
oExcel.DisplayAlerts = True
Dim tmpIntValue As Integer = 44 'This is the Value that should be the result
oExcel.Run("Sheet8.CommandButton2_Click")
Console.Write(tmpIntValue)
Catch
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 TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick
Dim FirstName As String
FirstName = TextBox1.Text
Dim xlsInstance As New Excel.Application()
xlsInstance.Visible = True
xlsInstance.Workbooks.Open("C:\Documents and Settings\Administrator\Desktop\7typesexcel10032012.xlsm")
DirectCast(DirectCast(xlsInstance.Workbooks(1).Sheets(1), Excel.Worksheet).Cells(1, "A"), Excel.Range).Value = FirstName
End Sub
End Class
2. This is my vba code
Private Sub CommandButton2_Click()
' Public Sub sub1(ByVal SomeValue As Integer)
Range("C5:L14").ClearContents
Application.Run "Solver.xlam!SolverReset"
SolverOk SetCell:="B17", MaxMinVal:=1, ByChange:="C5:L14"
SolverAdd CellRef:="C18", Relation:=2, FormulaText:="E23"
SolverAdd CellRef:="C19", Relation:=2, FormulaText:="E24"
SolverAdd CellRef:="C20", Relation:=2, FormulaText:="E25"
SolverAdd CellRef:="C21", Relation:=2, FormulaText:="E26"
SolverAdd CellRef:="C22", Relation:=2, FormulaText:="E27"
SolverAdd CellRef:="C23", Relation:=2, FormulaText:="E23"
SolverAdd CellRef:="C24", Relation:=2, FormulaText:="E24"
SolverAdd CellRef:="C25", Relation:=2, FormulaText:="E25"
SolverAdd CellRef:="C26", Relation:=2, FormulaText:="E26"
SolverAdd CellRef:="C27", Relation:=2, FormulaText:="E27"
SolverSolve UserFinish:=True
SolverReset
'finish
End Sub
3. After I got the result from solver which are printed on excel cell, I want to extract all content from cells to vb.net in form of string