private void cmdMisc_Click (object sender, EventArgs e)
{
rhsVBA cs = new rhsVBA();
int n = cs.mCount();
}
public class rhsVBA
{
//... Other routines ...//
public int mCount()
{
Microsoft.Office.Interop.Excel.Application xlApp;
Workbook wb;
// Get ahold of the application and the workbook.
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
wb = xlApp.Workbooks.Open("C:\\MyExcelFile.xlsm");
// Call the Excel function
int N = xlApp.Run("mCount");
// Let go of Excel
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
GC.Collect();
GC.WaitForPendingFinalizers();
// Return the data
return N;
}
}
Option Explicit
Option Base 0
' ... Variable definitions ...
' ... Other routines ...
Function mCount() As Integer
Dim m() As Double
m = GetValuesFromExcel("B4")
mCount = UBound(m) + 1
End Function
Function GetValuesFromExcel(startCell As String) As Double()
Dim x() As Double
Range(startCell).Select
Dim i As Integer
i = 0
Do Until IsEmpty(ActiveCell)
ReDim Preserve x(i)
x(i) = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
i = i + 1
Loop
GetValuesFromExcel = x
End Function
' ... Other routines ...
public class rhsVBA
{
Microsoft.Office.Interop.Excel.Application xlApp;
Workbook wb;
public rhsVBA()
{
// Get ahold of the application and the workbook.
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
wb = xlApp.Workbooks.Open("C:\\MyExcelFile.xlsm");
}
~rhsVBA()
{
// Let go of Excel
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
GC.Collect();
GC.WaitForPendingFinalizers();
}
//... Other routines ...//
public int mCount()
{
// Call the Excel function
int N = xlApp.Run("mCount");
// Return the data
return N;
}
}
private void cmdMisc_Click (object sender, EventArgs e)
{
rhsVBA cs = new rhsVBA();
int n = cs.mCount();
// I thought that the destructor for cs would be called here.
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE