INFO: DDE Example Between Visual Basic and Word for Windows

ID: Q74862

The information in this article applies to:

SUMMARY

This article outlines the steps necessary to initiate dynamic data exchange (DDE) between a Microsoft Visual Basic application and a Microsoft Word for Windows (WINWORD.EXE) document at run time.

This article demonstrates how to:

MORE INFORMATION

This information is included with the Help file provided with Microsoft Professional Toolkit for Visual Basic version 1.0, Microsoft Visual Basic version 2.0, and Microsoft Visual Basic version 3.0.

A destination application sends commands through DDE to the source application to establish a link. Through DDE, the source provides data to the destination at the request of the destination or accepts information at the request of the destination.

Example Showing How to Establish a DDE Conversation

The steps below give an example of how to establish a DDE conversation between a Visual Basic application and a document loaded into Word for Windows (WINWORD.EXE).

STEP ONE: Create the Source Document in Word for Windows

1. Start Microsoft Word for Windows. Document1 is created by default.

   NOTE: The Tip of the Day option in Microsoft Word version 6.0 for
   Windows must be turned off in order for this to work.

2. From the Window menu, choose Arrange All. This removes maximization if
   the document was maximized. Note that the title at the top of the
   WINWORD.EXE main title bar is now:

      Microsoft Word

   instead of:

      Microsoft Word - Document1

3. Press CTRL+SHIFT+END to select to the end of the document.

4. From the Insert menu (or the Edit menu in Microsoft Word version 6.0),

   choose Bookmark. Under Bookmark Name, type:

      DDE_Link

   Press the ENTER key. This sets a bookmark for the entire document. This
   bookmark functions as the LinkItem in the DDE conversation.

5. From the File menu, choose Save As, and save the document with the
   name SOURCE.DOC.

   NOTE: SOURCE.DOC must be saved to the working directory used by the
   Visual Basic application or the DDE won't work. Specifying the path in
   the SHELL statement in Visual Basic will not work.

6. Exit from Word for Windows. For this particular example to function
   correctly, WINWORD.EXE must not be loaded and running.

STEP TWO: Create the Destination Application in Visual Basic

1. Start Visual Basic. Form1 is created by default.

2. Create the following controls on Form1, giving the controls the

   properties shown in the table:

   Default Name   Caption            Name
   ----------------------------------------------
   Text1          (Not applicable)   Text1
   Option1        Manual Link        ManualLink
   Option2        Automatic Link     AutomaticLink
   Command1       Poke               Poke
   Command2       Request            Request

3. Add the following code to the General Declaration section of Form1:

   Const AUTOMATIC = 1
   Const MANUAL = 2
   Const NONE = 0

4. Add the following code to the Load event procedure of Form1:

   Sub Form_Load ()
      'This procedure starts WINWORD.EXE, loads the document that was
      'created earlier, and prepares for DDE by creating a bookmark to
      'the whole document. This bookmark is necessary because it
      'functions as the LinkItem for the source in the DDE conversation.

      z% = Shell("WinWord Source.Doc",1)

      z% = DoEvents ()  'Process Windows events to ensure that
                        'WINWORD.EXE is executed before any attempt is
                        'made to perform DDE with it.

      Text1.LinkMode = NONE              'Clears DDE link if it exists.
      Text1.LinkTopic = "WinWord|Source" 'Sets up link with WINWORD.EXE.
      Text1.LinkItem = "DDE_Link"        'Set link to bookmark on document.
      Text1.LinkMode = MANUAL            'Establish a manual DDE link.
      ManualLink.Value = TRUE
   End Sub

5. Add the following code to the Click event procedure of the Manual
   Link button:

   Sub ManualLink_Click ()
      Request.Visible = TRUE  'Make request button valid.
      Text1.LinkMode = NONE   'Clear DDE Link.
      Text1.LinkMode = MANUAL 'Reestablish new LinkMode.
   End Sub

6. Add the following code to the Click event procedure of the Automatic
   Link button:

   Sub AutomaticLink_Click ()
      Request.Visible = FALSE     'No need for button with automatic link.
      Text1.LinkMode = NONE       'Clear DDE Link.
      Text1.LinkMode = AUTOMATIC  'Reestablish new LinkMode.
   End Sub

7. Add the following code to the Click event procedure of the Request
   button:

   Sub Request_Click ()
      'With a manual DDE link this button is visible. Clicking this button
      'requests an update of information from the source application to the
      'destination application.
      Text1.LinkRequest
   End Sub

8. Add the following code to the Click event procedure of the Poke button:

   Sub Poke_Click ()
      'With any DDE link, this button is visible. Clicking this button
      'pokes information from the destination application into the source
      'application.
      Text1.LinkPoke
   End Sub

STEP THREE: Try it out

Now, you have two choices. You can run the Visual Basic destination application from the Visual Basic VB.EXE environment by skipping to step 4 below, or you can save the application, create an .EXE file, and run that from Windows by beginning with step 1 below.

1. From the File menu, choose Save, and save the form and project with

   the name DEST.

2. From the File menu, choose Make EXE File with the name DEST.EXE.

3. Exit from the Visual Basic environment (VB.EXE).

4. Run the application. Run an .EXE file from Windows, or if you're in the

   Visual Basic environment, from the Run menu, choose Start.

   Form1 of the Visual Basic destination application will be loaded, and
   Word for Windows will automatically start and load SOURCE.DOC.

5. Make sure the main title bar in WINWORD.EXE reads "Microsoft Word,"
   not "Microsoft Word - SOURCE.DOC." If the title bar is not correct,
   choose Arrange All from the Window menu.

STEP FOUR: Experiment with DDE Between Visual Basic and Word for Windows

1. Try typing some text into the document in Word for Windows. Then click

   the Request button. The text appears in the text box.

2. Click the Automatic Link button. Then type some more text into the
   document in Word for Windows. The text is automatically updated in the
   Visual Basic text box.

3. Type some text in the text box in the Visual Basic application. Then
   click the Poke button. The text goes to the Word for Windows document.

Note that if in the WINWORD.EXE document, you delete the total contents of the bookmark, the bookmark is also deleted. Any attempt to perform DDE with this WINWORD.EXE session after deleting the bookmark causes this error:

   Foreign applications won't perform DDE method or operation.

If this happens, you must re-create the bookmark in the document in Word for Windows before performing any further DDE operations.

In Visual Basic version 1.0, you need to add the following two global constants to the form's general declarations section:

   CONST TRUE = -1
   CONST FALSE = NOT TRUE

Additional query words: Keywords : kbVBp300 kbWord kbvbp200

Last Reviewed: January 7, 1999