Microsoft KB Archive/93657

= WD: Running Word for Windows as a DDE Server =

Article ID: 93657

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Word 1.0 Standard Edition
 * Microsoft Word 1.1 Standard Edition
 * Microsoft Word 1.1 Standard Edition
 * Microsoft Word 2.0 Standard Edition
 * Microsoft Word 2.0a
 * Microsoft Office Word 2003
 * Microsoft Word 2.0c
 * Microsoft Word 6.0 Standard Edition
 * Microsoft Word 6.0a
 * Microsoft Word 6.0c
 * Microsoft Word 95 Standard Edition
 * Microsoft Word 95a
 * Microsoft Word 97 Standard Edition

-



This article was previously published under Q93657



SUMMARY
This article covers what DDE topics and items are used to communicate with Word for Windows as a DDE server. This article assumes that you have a working knowledge of DDE concepts.

Word for Windows sample macros are provided to demonstrate the DDE server functionality.

NOTE: Some DDE functions do not work correctly when executed from a single instance of Word for Windows. In other words, a single copy of Word cannot be both the client and the server in a DDE conversation at the same time. For the macro examples below, run two copies of Word for Windows simultaneously with one being the client and the other the server.

Word 2.x: You must have the MS-DOS SHARE command line present in your AUTOEXEC.BAT file to have two copies of Word running simultaneously.

Word 1.x: You must make a copy of the WINWORD.EXE file on your system and rename this copy to something else. The two copies can be run with one as the client and the other the server.



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 &quot;DDE,&quot; click Search, and then click to view &quot;Communicating with other applications.&quot;

Initiating a DDE Conversation with Word As a Server
When you initiate a DDE conversation with another application, or &quot;dial up&quot; the other application, two things are required--an &quot;application&quot; name and the &quot;topic&quot; of the conversation.

The &quot;application&quot; name is typically the name of the program's executable file. For Word, the application name is WINWORD.

The &quot;topic&quot; of a DDE conversation can be either the name of a currently open document or the special topic &quot;system&quot;. A document can be either a regular Word document, a template, or a macro. See &quot;Using the System Topic&quot; 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(&quot;winword&quot;, &quot;sales.doc&quot;) 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 &quot;item&quot; 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(&quot;winword&quot;, &quot;sales.doc&quot;) MsgBox DDERequest$(ChanNum, &quot;request&quot;) DDEPoke ChanNum, &quot;poke&quot;, &quot;How are you!&quot; 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(&quot;winword&quot;, &quot;sales.doc&quot;) For i = 1 To 5 DDEExecute ChanNum, &quot;Line&quot; + Str$(i) + &quot;{ENTER}&quot; 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 &quot;system&quot; 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$, &quot;System&quot;) SysItems$ = DDERequest$(ChanNum, &quot;SysItems&quot;) Topics$ = DDERequest$(ChanNum, &quot;Topics&quot;) Formats$ = DDERequest$(ChanNum, &quot;Formats&quot;) DDETerminate ChanNum

DisplayTabbedList(SysItems$, &quot;List of System Items&quot;) DisplayTabbedList(Topics$, &quot;List of Topics&quot;) DisplayTabbedList(Formats$, &quot;List of Formats&quot;) 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$ = &quot;c:\excel\examples\amortize.xls&quot; chan = DDEInitiate(&quot;excel&quot;, &quot;system&quot;) Topics$ = DDERequest$(chan, &quot;topics&quot;) If InStr(UCase$(topics$), UCase$(Topic$)) = 0 Then exe$ = &quot;[OPEN(&quot; + Chr$(34) + Topic$ + Chr$(34) + &quot;)]&quot; DDEExecute chan, exe$ End If  DDETerminate chan chan = DDEInitiate(&quot;excel&quot;, topic$) MsgBox &quot;We're talking to &quot; + Topic$ + &quot;!&quot; 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: <pre class="fixed_text">  &quot;[FileOpen &quot; + Chr$(34) + Topic$ + Chr$(34) + &quot;]&quot;

<div class="references_section">