WD: Running Word for Windows as a DDE Server

Last reviewed: February 11, 1998
Article ID: Q93657
The information in this article applies to:
  • Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Microsoft Word 97 for Windows

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.

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:

  1. Create a document in the second instance of Word and save it with the name Sales.doc.

  2. Create two paragraphs in Sales.doc.

  3. 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.

  4. 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.

  5. 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) + "]"

REFERENCES

"Microsoft Word Technical Reference for Windows and OS/2," pages 307-312.


Additional query words: dynamic data exchange ole object linking embedding
appnote wordbasic
Keywords : kbwordvba winword winword2 word6 word7 word8 word95 word97 wordnt kbmacro kbole
Version : 1.x 2.x 6.0 6.0a 6.0c 7.0 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 11, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.