Excel RTD Servers: A Topic’s Initial Value

What is the initial value displayed for a particular topic? Before you come up with an answer take another look at the ConnectData method used by Excel to subscribe to a particular topic.

In IDL it looks like this:

HRESULT ConnectData([in] long topicId,
                    [in] SAFEARRAY(VARIANT)* strings,
                    [in, out] VARIANT_BOOL* newValue,
                    [out, retval] VARIANT* value);

In C# it looks like this (I’ve removed the MarshalAs attribute for clarity):

object ConnectData(int topicId,
                       ref Array strings,
                       ref bool newValue);

These are actually equivalent. The CLR just takes the retval parameter and uses that as the return value and converts the HRESULT into an exception. This is much the same as what Visual Basic and scripting languages do.

So ConnectData returns a VARIANT/object to Excel but is it the initial value displayed for a particular topic? Well it depends on the newValue parameter. What makes this interesting is that it’s an [in, out] parameter, called a ref parameter in C#. Why would that be? Clearly Excel is trying to tell us something.

What Excel tells the RTD server about the value

On input the newValue parameter indicates whether Excel already has a value to initially display. This will never happen if you start with a blank Excel workbook but can happen if you open a previously saved workbook that includes RTD functions. Excel effectively caches the last value of each RTD topic in the workbook. The idea is that Excel can display an initial value while the RTD server goes about any potentially time consuming operations to reestablish data connections.

So if Excel has a cached value to display then newValue will be false. If Excel does not have a cached value to display then newValue will be true, indicating that a new value is needed. Of course that doesn’t mean you have to provide one.

What the RTD server tells Excel about the value

On output the newValue parameter indicates whether Excel should use the returned value or not.

If newValue is false then Excel will ignore the value returned by ConnectData. If it doesn’t have a previously cached value then it displays the “#N/A” warning. Of course this is replaced with an actual value once it receives one via RefreshData.

If newValue is true then Excel will immediately replace whatever value it may already have with the value returned by ConnectData.

If you’re looking for one of my previous articles here is a complete list of them for you to browse through.

Produce the highest quality screenshots with the least amount of effort! Use Window Clippings.

No Comments