Forum Discussion
the_dev1645
Dec 21, 2023Copper Contributor
RTD EXCEL user defined data type, e.g. Employee with Name and Age
Hi,
I have been trying to write an RTD com server in C++ to display the user defined data. My question here is: Could we send a user defined data type, i.e. Employee, from the RTD server in the callback and unpack in the VBA code?
struct Employee {
int age;
std::string name;
};
HRESULT CNAHERTDServer::RefreshData(long * topicCount, SAFEARRAY * * parrayOut) {
//it works
value.vt = VT_BSTR;
_bstr_t bstrt(itr->second.c_str());
value.bstrVal = bstrt;
SafeArrayPutElement(*parrayOut, index, &value);
}
VBA code: //get employee here, unpack and put in the excel
Private Sub Worksheet_Calculate()
Dim changedRange As Range
On Error Resume Next
Set changedRange = Range("Lookup!tbData")
On Error GoTo 0
If Not changedRange Is Nothing And changedRange.Count = 1 Then
Debug.Print changedRange.value
End If
End Sub
Thanks
- AshaKantaSharmaIron ContributorSerialization in C++: Convert your user-defined data type to a string format in the RTD server code.
SAFEARRAY Handling: Use SAFEARRAY to pass the string data from the server to Excel.
Deserialization in VBA: Parse the string in VBA to extract and use the individual data fields.