Create a Sample RealTimeData ServerThe following sample demonstrates how to create and use a RealTimeData server with Microsoft Excel 2002. This server simply provides a counter that is updated every 10 seconds on a worksheet. The server accepts up to two topic strings. The first topic string can be AAA, BBB, and CCC; any other topic string is considered invalid and the server returns #VALUE! to the RTD function. The second string is a numeric value that represents how the return value should be incremented. If the second string is omitted, the increment value defaults to 1. If the second string is not numeric, the server returns #NUM! to the RTD function.
- Start a new ActiveX DLL project in Visual Basic.
- On the Project menu, click References, select the Object Library for your version of Excel, and then click OK. For example, select one of the following:
- For Microsoft Office Excel 2007, select Microsoft Excel 12.0 Object Library.
- For Microsoft Office Excel 2003, select Microsoft Excel 11.0 Object Library.
- For Microsoft Excel 2002, select Microsoft Excel 10.0 Object Library.
- On the Project menu, click Project1 Properties. Change the Project Name to ExcelRTD, and then click OK.
- Change the Name property of the class module Class1 to RTDFunctions. Add the following code to RTDFunctions:
Implements IRtdServer 'Interface allows Excel to contact this RealTimeData server
Private m_colTopics As Collection
Private Function IRtdServer_ConnectData(ByVal TopicID As Long, Strings() As Variant, GetNewValues As Boolean) As Variant
'** ConnectData is called whenever a new RTD topic is requested
'Create a new topic class with the given TopicId and string and add it to the
Dim oTopic As New Topic
m_colTopics.Add oTopic, CStr(TopicID)
oTopic.TopicID = TopicID
oTopic.TopicString = Strings(0)
If UBound(Strings) >= 1 Then oTopic.SetIncrement Strings(1)
'For this example, the initial value for a new topic is always 0
IRtdServer_ConnectData = oTopic.TopicValue
Debug.Print "ConnectData", TopicID
Private Sub IRtdServer_DisconnectData(ByVal TopicID As Long)
'** DisconnectData is called whenever a specific topic is not longer needed
'Remove the topic from the collection
Debug.Print "DisconnectData", TopicID
Private Function IRtdServer_Heartbeat() As Long
'** Called by Excel if the heartbeat interval has elapsed since the last time
' Excel was called with UpdateNotify.
Private Function IRtdServer_RefreshData(TopicCount As Long) As Variant()
'** Called when Excel is requesting a refresh on topics. RefreshData will be called
' after an UpdateNotify has been issued by the server. This event should:
' - supply a value for TopicCount (number of topics to update)
' - return a two dimensional variant array containing the topic ids and the
' new values of each.
Dim oTopic As Topic, n As Integer
ReDim aUpdates(0 To 1, 0 To m_colTopics.Count - 1) As Variant
For Each oTopic In m_colTopics
aUpdates(0, n) = oTopic.TopicID
aUpdates(1, n) = oTopic.TopicValue
n = n + 1
TopicCount = m_colTopics.Count
IRtdServer_RefreshData = aUpdates
Debug.Print "RefreshData", TopicCount & " topics updated"
Private Function IRtdServer_ServerStart(ByVal CallbackObject As Excel.IRTDUpdateEvent) As Long
'** ServerStart is called when the first RTD topic is requested
Set oCallBack = CallbackObject
Set m_colTopics = New Collection
g_TimerID = SetTimer(0, 0, TIMER_INTERVAL, AddressOf TimerCallback)
If g_TimerID > 0 Then IRtdServer_ServerStart = 1 'Any value <1 indicates failure.
Private Sub IRtdServer_ServerTerminate()
'** ServerTerminate is called when no more topics are needed by Excel.
KillTimer 0, g_TimerID
'** Cleanup any remaining topics. This is done here since
' IRtdServer_DisconnectData is only called if a topic is disconnected
' while the book is open. Items left in the collection when we terminate
' are those topics left running when the workbook was closed.
Dim oTopic As Topic
For Each oTopic In m_colTopics
Set oTopic = Nothing
- On the Project menu, click Add Class Module. Change the class module Name property to Topic and change the Instancing property to Private. Add the following code to the Topic class module:
Private m_TopicID As Long
Private m_TopicString As String
Private m_Value As Variant
Private m_IncrementVal As Long
Private Sub Class_Initialize()
m_Value = 0
m_IncrementVal = 1
Friend Property Let TopicID(ID As Long)
m_TopicID = ID
Friend Property Get TopicID() As Long
TopicID = m_TopicID
Friend Property Let TopicString(s As String)
s = UCase(s)
If s = "AAA" Or s = "BBB" Or s = "CCC" Then
m_TopicString = s
m_Value = CVErr(xlErrValue) 'Return #VALUE if not one of the listed topics
Friend Sub Update()
On Error Resume Next 'the next operation will fail if m_Value is an error (like #NUM or #VALUE)
m_Value = m_Value + m_IncrementVal
Friend Sub SetIncrement(v As Variant)
On Error Resume Next
m_IncrementVal = CLng(v)
If Err <> 0 Then
m_Value = CVErr(xlErrNum) 'Return #NUM if Increment value is not numeric
Friend Property Get TopicValue() As Variant
If Not (IsError(m_Value)) Then
TopicValue = m_TopicString & ": " & m_Value
TopicValue = m_Value
- On the Project menu, select Add Module. Add the following code to the new module:
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Public Const TIMER_INTERVAL = 5000
Public oCallBack As Excel.IRTDUpdateEvent
Public g_TimerID As Long
Public Sub TimerCallback(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
- On the File menu, click Make ExcelRTD.dll to build the component.
Use the RTD Server in Excel
- Start a new workbook in Microsoft Excel.
- In cell A1, enter the following formula, and then press the ENTER key:=RTD("ExcelRTD.RTDFunctions",,"AAA", 5)The initial return value is "AAA: 0". After five seconds, the value updates to "AAA: 10" and after 10 seconds, the value updates to "AAA:15," and so on.
- In cell A2, enter the following formula and press ENTER:=RTD("ExcelRTD.RTDFunctions",,"BBB", 3)The initial return value is "BBB: 0". Every five seconds the cell value increments by 3.
- In cell A3, enter the following formula and press ENTER:=RTD("ExcelRTD.RTDFunctions",,"AAA", 5)The initial return value matches the contents of cell A1 because this is the same "topic" that is used in A1.
- In cell A4, enter the following formula and press Enter:=RTD("ExcelRTD.RTDFunctions",,"AAA", 10)The initial return value is "AAA: 0." Every five seconds the cell value increments as do the other cells. Note that the return value does not match the contents of cell A1 or A3 because the combination of parameters passed to the server is different.
To run in debug mode:
- Quit Microsoft Excel and switch to the project in Visual Basic.
- Press F5 to start the component. If the Project Properties dialog box appears, click OK to select the default option of Wait for components to be created.
- Make sure that the Immediate window in Visual Basic is displayed. As you enter formulas in the cells and as the cells are updated, examine the contents of the Immediate window in Visual Basic to see which actions are triggering the different events.
While Excel is a subscriber to your RTD server, it triggers the DisconnectData event when it no longer needs a topic (for example, when you delete or clear an RTD formula in a cell). However, Excel does not call DisconnectData on each topic for the RTD server when the workbook is closed or Excel quits; instead, Excel calls only ServerTerminate. When you are creating an RTD server, you should code for any necessary clean-up of topics or other objects when the ServerTerminate event fires.
(c) Microsoft Corporation 2001, All Rights Reserved. Contributions by Lori B. Turner, Microsoft Corporation.
ID do Artigo: 285339 - Última Revisão: 23/03/2009 - Revisão: 1