Robot Structural Analysis 2025-Close Excel Dynamo

Hello everyone,

In Robot structural analysis 2022 with Dynamo we are using a python script that closes the excel file that’s beiing used before the new one gets opened.
At the moment we are trying to upgrade to RSA 2025 and want to use the same script. but we get the following error:

stefanyC6JZ7_0-1722870458060.png

Please find the code from the script below :

import sys
import clr
import System 

clr.AddReference('Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
from Microsoft.Office.Interop import Excel
from System.Runtime.InteropServices import Marshal

def excel_close(lst_namefile):
    xlApp = Excel.Application(Marshal.GetActiveObject("Excel.Application"))
    print(xlApp)
    allWorkbooks = Excel.Workbooks(xlApp.get_Workbooks())
    print([x for x in allWorkbooks])
    lst_WkbsName = [Excel.Workbook(x).get_Name() for x in allWorkbooks]
    for namefile in lst_namefile:
        if namefile in lst_WkbsName:
            Tesluiten = Excel.Workbook(allWorkbooks.get_Item(namefile))
            Tesluiten.Close()
    xlApp.Quit()

toList = lambda x : x if isinstance(x, list) else [x]
OUT = excel_close(toList(IN[0]))

@jacob.small @gwizdzm hope you know/ or maybe know can help us with this.

due to holidays, this topic is also on the RSA-Forum

Many thanks in advance

Gr Edward

Which Python engine are you using? CPython?

Hi @jacob.small ,

Not exactly sure where to find it. see image below.

That is CPython - you can see the engine at the bottom of the node. We’re you using IronPython in 2022?

No,
Also Cpython, only with different Dynamo for RSA 2022

Ok, this indicates the issue is likely with the .NET 8 update.

Can you copy the full error message from the Graph node manager UI? (Open it from the extensions menu, find the node in the list and copying the message should be straightforward)

Bit outside the areas I am working in this week (ok light years away) and I’m on vacation the next two so I may not be of much help, but let me see what I can find.

Here You are, already many thanks.

Message

FileNotFoundException : Unable to find assembly ‘Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’. at Python.Runtime.CLRModule.AddReference(String name)

at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)

at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr) [’ File "

Hmmm… Try executing Microsoft’s online repair for Excel as this looks shockingly similar to the corrupted office installation messages.

for info

Marshal.GetActiveObject is not available in .Net 8

As a workaround, you can create an ZT node and reimplement this function

examples

Note
if you know the path of the Excel file, you can possibly use BindToMoniker to get current COM Workbook

2 Likes

Hi @c.poupin

I don’t fully understand what you exactly mean.
To give you some insight how we(the computers most times at night) work.


We have a dynamo file, in that file we fill the names of all the excel files that needs to be calculated [it needs to be one file at the time], in the end of the ‘.DYF node’ the file should close, before the next file get opened.

So this is why we are using this script.

Hopes this makes sense to you?

Gr Edward

Hi,
there are 2 issues

  • the switch to Net 8 (.Net Core) means that we can no longer use directly assemblies in the windows GAC (like Excel Interrop)
    so clr.AddReference('Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c') failed

  • Marshal.GetActiveObject is not available in .Net 8

Maybe you can use OpenXml to read / write the Excel file ?