ID: Q151269
The information in this article applies to:
SYMPTOMS
The Microsoft Access version 7.0 Visual Basic online Help Example for the DDEPoke Statement will fail if run from a Microsoft Excel module. It works when run from Microsoft Access, and there are no error messages or other indications of failure when run from Microsoft Excel. The data is simply not passed as expected.
The following example works in Microsoft Access, but not in Microsoft Excel:
On Error Resume Next ' Set up error handler.
Dim Chan, SheetName, I, TopicList ' Declare variables.
Chan = DDEInitiate("Excel", "System") ' Establish spreadsheet link.
If Err Then ' If error occurs, spreadsheet
Err = 0 ' isn't running. Reset error
I = Shell("Excel", 1) ' and start spreadsheet.
If Err Then Exit Sub ' If another error, exit.
Chan = DDEInitiate("Excel", "System") ' Establish spreadsheet link.
End If
DDEExecute Chan, "[New(1)]" ' Create new worksheet.
TopicList = DDERequest(Chan, "Selection") ' Get topic list, worksheet
' name.
SheetName = Left(TopicList, InStr(1, TopicList, "!") - 1)
DDETerminate Chan ' Terminate DDE link.
Chan = DDEInitiate("Excel", SheetName) ' Establish link with new
' worksheet.
For I = 1 To 10 ' Put some values into
DDEPoke Chan, "R1C" & I, I ' first row.
Next I
DDEExecute Chan, "[Select(""R1C1:R1C10"")][New(2,2)]" ' Make chart.
DDETerminateAll ' Terminate all links.
CAUSE
The DDEPoke command contains three arguments: ChannelNumber, Item, and Data. In Microsoft Excel the Data argument may only refer to text or numbers represented by a range object or reference. The Microsoft Access example fails in Microsoft Excel because the Data argument refers to the variable "I," which is not an object or cell reference. Also, Microsoft Excel does not support the DDETerminateAll method; Microsoft Excel uses the DDETerminate method.
WORKAROUND
Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code-- comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line- continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."
The following modified example will work from a Microsoft Excel module:
On Error Resume Next ' Set up error handler.
Dim chan, SheetName, I, TopicList, rangeToPoke ' Declare variables.
chan = DDEInitiate("Excel", "System") ' Establish spreadsheet link.
If Err Then ' If error occurs, spreadsheet
Err = 0 ' isn't running. Reset error
I = Shell("Excel", 1) ' and start spreadsheet.
If Err Then Exit Sub ' If another error, exit.
chan = DDEInitiate("Excel", "System") ' Establish spreadsheet link.
End If
DDEExecute chan, "[New(1)]" ' Create new worksheet.
TopicList = DDERequest(chan, "Selection") ' Get topic list, worksheet
' name.
SheetName = Left(TopicList, InStr(1, TopicList, "!") - 1)
DDETerminate chan ' Terminate DDE link.
chan = DDEInitiate("Excel", SheetName) ' Establish link with new
' worksheet.
For I = 1 To 10
Set rangeToPoke = ThisWorkbook.Worksheets(1).Range("a1")
rangeToPoke.Value = I ' Put some values into
DDEPoke chan, "R1C" & I, rangeToPoke ' first row.
Next I
DDEExecute chan, "[Select(""R1C1:R1C10"")][New(2,2)]" ' Make chart.
DDETerminate chan ' Terminate link.
The following changes were made to the original example:
1. Changed the Dim statement from
Dim chan, SheetName, I, TopicList
to:
Dim chan, SheetName, I, TopicList, rangeToPoke
adding rangeToPoke.
2. Changed the For..Next loop from
For I = 1 To 10
DDEPoke Chan, "R1C" & I, I
Next I
to:
For I = 1 To 10
Set rangeToPoke = ThisWorkbook.Worksheets(1).Range("a1")
rangeToPoke.Value = I
DDEPoke chan, "R1C" & I, rangeToPoke
Next I
3. Changed DDETerminate line from:
DDETerminateAll
to:
DDETerminate chan
For additional information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q139881
TITLE : XL: Sample Code Demonstrating DDEPoke and POKE()
REFERENCES
Microsoft Excel, version 5.0, "Visual Basic User's Guide," Chapter 10, "Using DDE"
For more information about DDEPoke in Microsoft Excel version 7.0, search on the word DDEPoke, using the Microsoft Excel Help Index.
Additional query words: 7.00 7.00a
Keywords :
Version : 7.0
Platform : WINDOWS
Last Reviewed: May 17, 1999