MORE INFORMATION
Word 97
For more information about and examples using DDE with Word 97, while in
the Visual Basic for Applications Editor click the Office Assistant, type
"DDE," click Search, and then click to view "Communicating with other
applications."
Word for Windows versions 2.x, 6.x, 7.x
Initiating a DDE Conversation with Word As a Server
When you initiate a DDE conversation with another application, or "dial up"
the other application, two things are required--an "application" name and
the "topic" of the conversation.
The "application" name is typically the name of the program's executable
file. For Word, the application name is WINWORD.
The "topic" of a DDE conversation can be either the name of a currently
open document or the special topic "system". A document can be either a
regular Word document, a template, or a macro. See "Using the System
Topic" for more information about the system topic.
Example:
The following macro demonstrates initiating a conversation with a
document as the topic.
Sub MAIN
ChanNum = DDEInitiate("winword", "sales.doc")
DDETerminate ChanNum
End Sub
In the example above, Word must be running and the Sales.doc document must
be open for the DDEInitiate command to return without error and with a
valid channel number.
Poking Data into and Requesting Data from a Word Document
To poke (send) or request information from a DDE topic, an "item" must be
specified. The item represents the specific piece of data or location in
the topic where data will be sent to or retrieved from. If a Word document
is the topic of the DDE conversation, the item is a piece of text or
location that has been bookmarked using the Insert Bookmark command.
NOTE: Word has a number of reserved bookmark names, such as \sel and
\page, that cannot be used in a DDE conversation. Only bookmarks that
have been inserted using the Insert Bookmark command can be used.
Example:
The following macro demonstrates poking to and requesting data and from a
Word document:
- Create a document in the second instance of Word and save it with the
name Sales.doc.
- Create two paragraphs in Sales.doc.
- In the first paragraph type Hello!, highlight it, click Bookmark on
the Insert menu, type request in the Name field, and click the OK
button.
- In the second paragraph, click Bookmark on the Edit menu (Insert menu
in Word 2.x), type poke in the Name field, and click the OK button.
- Run the following macro from the first instance of Word.
Sub MAIN
ChanNum = DDEInitiate("winword", "sales.doc")
MsgBox DDERequest$(ChanNum, "request")
DDEPoke ChanNum, "poke", "How are you!"
DDETerminate ChanNum
End Sub
Sending Keystrokes to Word for Windows
It is possible to send data to Word without using a DDE Poke. If you
perform a DDE Execute and do not put square brackets around the execute
string, Word types them into the document at the current cursor location.
Further, you can have Word perform special key commands, such as pressing
the ENTER key or SHIFT+F3, by following the key syntax used in the
WordBasic SendKeys macro command.
Example:
The following macro types five lines of data into the document Sales.doc at
the current cursor location:
Sub MAIN
ChanNum = DDEInitiate("winword", "sales.doc")
For i = 1 To 5
DDEExecute ChanNum, "Line" + Str$(i) + "{ENTER}"
Next i
DDETerminate ChanNum
End Sub
Using the System Topic
The system topic is a special topic supported by most DDE-aware
applications to allow the client to obtain specific information about the
current status of the server. The most common purpose is to get a list of
available topics that can be used to initiate a conversation, or to see
if the desired topic is available for use.
Once a DDE conversation with Word as the server has been initiated using
the system topic, a DDE request can be performed using the following items:
SysItems, Topics, and Formats. The list of information returned by this DDE
request will be tab delimited.
The following table summarizes the available information:
Item Available Information
-------- ---------------------
SysItems A list of all items you can use with the "system" topic.
Topics A list of all topics used to initiate a DDE conversation.
This includes a list of all currently open documents with
their path names, templates, macro windows, and the system
topic.
Formats A list of all the Clipboard formats supported by Word.
Example:
The following macro uses the system topic to get information on all three
system items: SysItems, Topics, and Formats. The macro parses the tab-
delimited list of information, putting it into an array for display in a
custom dialog box.
Sub MAIN
DDETerminateAll
ChanNum = DDEInitiate(gAppName$, "System")
SysItems$ = DDERequest$(ChanNum, "SysItems")
Topics$ = DDERequest$(ChanNum, "Topics")
Formats$ = DDERequest$(ChanNum, "Formats")
DDETerminate ChanNum
DisplayTabbedList(SysItems$, "List of System Items")
DisplayTabbedList(Topics$, "List of Topics")
DisplayTabbedList(Formats$, "List of Formats")
End Sub
Sub DisplayTabbedList(srcList$, Description$)
Tab$ = Chr$(9)
List$ = srcList$
' Count the number of tabs contained in a string
' which is equal to the number of items
cTabs = 0
FoundTab = InStr(1, List$, Tab$)
While FoundTab
cTabs = cTabs + 1
FoundTab = InStr(FoundTab + 1, List$, Tab$)
Wend
' Create the array to hold the information
Dim ListBox$(cTabs)
' Extract tab-delimited items and store in ListBox$ array
FoundTab = InStr(1, List$, Tab$)
For i = 1 To cTabs
ListBox$(i - 1) = Left$(List$, FoundTab - 1)
List$ = Right$(List$, Len(List$) - FoundTab)
FoundTab = InStr(1, List$, Tab$)
Next i
' Snag last item, if it exists!
If Len(List$) > 0 Then
ListBox$(cTabs) = List$
End If
' Create and display dialog box with items in listbox
Begin Dialog UserDialog 320, 144
ListBox 11, 23, 296, 84, ListBox$(), .ListBox
OKButton 11, 113, 296, 21
Text 11, 4, 296, 14, Description$
End Dialog
Dim dlg As UserDialog
Dialog dlg
End Sub
System Topic Pitfalls
The system topic was designed primarily for use in retrieving
information about Word using the SysItems, Topics, and Formats items.
However, some users may be tempted to use it for doing other activities,
such as DDE pokes and requests to bookmarks in open documents, or
performing DDE executes of macro instructions.
The problem is that most Word commands that can be executed must be
directed toward a specific document. Because Word can have up to nine
document windows open at a time, if you try to do a DDE execute to insert
some text into a document using the system topic, Word doesn't know which
open document to insert the text into (Word doesn't assume that the active
window is the desired window). Attempting to execute document-specific
commands with the system topic results in an error. The same holds true
if you attempt to request data from an item not supported by the system
topic described above.
Commands that are executed that are not document-specific, like opening or
creating a new document, work fine. In fact, a common practice among macro
writers is to use the Topics item to see if the desired document is
available to communicate with. If the document is not available, you can
execute the FileOpen command to open it. After the document (topic) is
open, you close the communication with the system topic and then initiate
a new conversation with the open document. The following macro demonstrates
this technique using Microsoft Excel.
Example:
The following macro demonstrates using the system topic in Microsoft Excel
to determine if a particular spreadsheet is open and available for a DDE
conversation. If it is not, a DDEExecute is issued to Excel to open the
spreadsheet.
Sub MAIN
Topic$ = "c:\excel\examples\amortize.xls"
chan = DDEInitiate("excel", "system")
Topics$ = DDERequest$(chan, "topics")
If InStr(UCase$(topics$), UCase$(Topic$)) = 0 Then
exe$ = "[OPEN(" + Chr$(34) + Topic$ + Chr$(34) + ")]"
DDEExecute chan, exe$
End If
DDETerminate chan
chan = DDEInitiate("excel", topic$)
MsgBox "We're talking to " + Topic$ + "!"
DDETerminateAll
End Sub
NOTE: Word operates the same with the exception of the DDEExecute
statement to open the file. We do not demonstrate this with Word
communicating with a second instance since the system topic always finds
the client instance, the second instance is ignored. Any attempt to do a
DDEExecute to the same instance of Word results in an error. A single
instance of Word cannot be both the client and server in a DDE
conversation. If this were possible, the exe$ variable would be set to
the following:
"[FileOpen " + Chr$(34) + Topic$ + Chr$(34) + "]"