The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0
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:
- Prepare a Word for Windows document for active DDE.
- Initiate a manual DDE link (information updated upon request from
the destination) between the Visual Basic application (the
destination) and the document loaded into Word for Windows (the source).
- Use LinkRequest to update information in the Visual Basic destination
based on information contained in the Word for Windows source.
- Initiate a automatic DDE link (information updated automatically from
source to destination) between the Visual Basic destination and the
Word for Windows source.
- Use LinkPoke to send information from the Visual Basic destination to
the Word for Windows source.
- Change the LinkMode property between automatic and manual.
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
- 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.
- 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
- Press CTRL+SHIFT+END to select to the end of the document.
- 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.
- 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.
- 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
- Start Visual Basic. Form1 is created by default.
- 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
- Add the following code to the General Declaration section of Form1:
Const AUTOMATIC = 1
Const MANUAL = 2
Const NONE = 0
- 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
- 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
- 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
- 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
- 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.
- From the File menu, choose Save, and save the form and project with
the name DEST.
- From the File menu, choose Make EXE File with the name DEST.EXE.
- Exit from the Visual Basic environment (VB.EXE).
- 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.
- 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
- Try typing some text into the document in Word for Windows. Then click
the Request button. The text appears in the text box.
- 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.
- 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