Hi everyone
I have made several scripts depending on comfortable data exchange with excel during the past few years. Up to rhino 7 I was using the following method to connect to an open and active excel application inside grasshoppers vb component:
' Get the active Excel application and workbook
Dim excelApp As Object = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
Dim activeWorkbook As Object = excelApp.ActiveWorkbook
' Get the worksheetA by name
Dim worksheetA As Object = Nothing
For Each sheet As Object In activeWorkbook.Sheets
If sheet.Name = "Name" Then
worksheetA = sheet
Exit For
End If
Next
' If the worksheetA doesn't exist, create a new one
If worksheetA Is Nothing Then
worksheetA = activeWorkbook.Sheets.Add(,
activeWorkbook.Sheets(activeWorkbook.Sheets.Count), Type.Missing, Type.Missing)
worksheetA.Name = "Name"
End If
' Select the worksheetA
worksheetA.Select()
Now that I updated to rhino 8 âSystem.Runtime.InteropServices.Marshal.GetActiveObjectâ does not work any more because Rhino 8 is using .net 7.
I have now tried for two days to find a workaround but did not have any success. Connecting to an open Excel application seems quite impossible
Choosing the workbook file in a file dialogue would be fine as well. I managed to get at least as far as choosing one and also tried to dynamically load âMicrosoft.Office.Interop.Excelâ to open the file:
Dim excelAssembly As System.Reflection.Assembly
excelAssembly = System.Reflection.Assembly.Load("Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
If R Then
Dim openFileDialog As New OpenFileDialog()
' Set the initial directory to the directory of the active Rhino document (3dm file)
openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(Rhino.RhinoDoc.ActiveDoc.Path)
' Set the filter options for Excel files and All Files
openFileDialog.Filter = "Excel-Files (*.xlsx;*.xlsm;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls;*.xla;*.xlt;*.xlm;*.xlw)|*.xlsx;*.xlsm;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls;*.xla;*.xlt;*.xlm;*.xlw|All Files (*.*)|*.*"
If openFileDialog.ShowDialog() = DialogResult.OK Then
' Get the selected file path
Dim selectedFilePath As String = openFileDialog.FileName
' Now, you can use the dynamically loaded excelAssembly to work with Excel
Dim excelType As Type = excelAssembly.GetType("Microsoft.Office.Interop.Excel.Application")
Dim excelApp As Object = Activator.CreateInstance(excelType)
' Open the Excel file
Dim workbooks As Object = excelApp.Workbooks
' Open the Excel file using the Add method of the workbooks collection
Dim workbook As Object = workbooks.Add(selectedFilePath)
End If
End If
However the line starting with âDim excelType As Typeâ always returns ânullâ and I canât seem to find any other approach.
Is there any conveniant (or at least doable) way to still propperly connect to Excel in Rhino 8 using VB (without forcing it to use the old .net framework at startup)?
I also tried pretty much any 3rd party plugin component but none of them reliably writes to already open instances or lets you choose the file in a dialogue you can trigger with a boolean button.
Iâd really appreciate any hint.
Thanks in advance!