excelvbadymola

Read values from Dymola using Excel VBA


I used an Excel macro for Dymola simulation with Dymola 2020x and 2022x.

//Dymola model

model Unnamed2
  parameter Real x=1;
  Real y;
equation 
  y=2*x;
  annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
        coordinateSystem(preserveAspectRatio=false)));
end Unnamed2;

//Excel macro

Sub ExcelMacro()
Cells(1, 1) = Application.DDERequest(Application.DDEInitiate("dymola", "xxx"), "ModelicaString:x")
End Sub

I run the Excel macro after simulating Dymola model.

In Dymola 2022x cell A1 returns 1.

Dymola 2024x returns #REF!.


Solution

  • There seems to be some preventing this from working in Dymola 2024x; it will be corrected in a future version. A work-around is to use:

    Cells(1, 1) = Application.DDERequest(Application.DDEInitiate("dymola", "xxx"), "MatlabString:x")
    

    The syntax for matrices and vectors will be slightly different in that case, but scalars will continue to work.