ID Number: Q74862
1.00
WINDOWS
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:
1. Prepare a Word for Windows document for active DDE.
2. Initiate a cold DDE link (information updated upon request from
the client) between the Visual Basic application (the client) and
the document loaded into Word for Windows (the server).
3. Use LinkRequest to update information in the Visual Basic client
based on information contained in the Word for Windows server.
4. Initiate a hot DDE link (information updated automatically from
server to client) between the Visual Basic client and the Word for
Windows server.
5. Use LinkPoke to send information from the Visual Basic client to
the Word for Windows server.
6. Change the LinkMode property between hot and cold.
This information applies to Microsoft Visual Basic programming system
version 1.0 for Microsoft Windows.
More Information:
A client application sends commands through DDE to the server
application to establish a link. Through DDE, the server provides data
to the client at the request of the client or accepts information at
the request of the client.
Example
-------
The steps below are an example of how to establish a DDE conversation
between a Visual Basic application and a document loaded into Word for
Windows (WINWORD.EXE).
First, create the server document in Word for Windows:
1. Start Word for Windows. "Document1" will be created by default.
2. From the Window menu, choose Arrange All. This unmaximizes the
document if it was maximized. Note that the title at the top of the
WINWORD.EXE main title bar is now "Microsoft Word" and NOT
"Microsoft Word - Document1".
3. Press CTRL+SHIFT+END to select to the end of the document.
4. From the Insert menu, choose Bookmark. Under Bookmark Name, type
"DDE_Link" (without quotation marks). Press ENTER. This sets a
bookmark for the entire document. This bookmark will function as
the LinkItem in the DDE conversation.
5. From the File menu, choose Save As, and save the document with the
name SERVER.DOC.
6. Exit Word for Windows. For this particular example to function
properly, WINWORD.EXE must not be loaded and running.
Second, create the client application in Visual Basic:
1. Start Visual Basic. Form1 will be created by default.
2. Create the following controls with the following properties on
Form1:
Default Name Caption CtlName
------------ ------- -------
Text1 (Not applicable) Text1
Option1 Cold Link ColdLink
Option2 Hot Link HotLink
Command1 Poke Poke
Command2 Request Request
3. Add the following code to the General Declaration section of Form1:
Const TRUE = -1
Const FALSE = 0
Const HOT = 1
Const COLD = 2
Const NONE = 0
4. Add the following code to the Load event procedure of Form1:
Sub Form_Load ()
'This procedure will start WINWORD.EXE, load the document that was
'created earlier and prepared for DDE by creating a bookmark to
'the whole document. This bookmark is necessary because it
'functions as the LinkItem for the server in the DDE conversation.
z% = Shell("C:\WinWord\WinWord C:\WinWord\Server.Doc",1)
z% = DoEvents () 'Process Windows events. This ensures
'that WinWord will be executed before
'any attempt is made to perform DDE
'with it.
Text1.LinkMode = NONE 'Clears DDE link if it already exists.
Text1.LinkTopic = "WinWord|\WinWord\Server" 'Sets up link with
'WINWORD.EXE.
Text1.LinkItem = "DDE_Link" 'Set link to bookmark on document.
Text1.LinkMode = COLD 'Establish a cold DDE link.
ColdLink.Value = TRUE
End Sub
5. Add the following code to the Click event procedure of the Cold
Link button:
Sub ColdLink_Click ()
Request.Visible = TRUE 'Make request button valid.
Text1.LinkMode = NONE 'Clear DDE Link.
Text1.LinkMode = COLD 'Reestablish new LinkMode.
End Sub
6. Add the following code to the Click event procedure of the Hot
Link button:
Sub HotLink_Click ()
Request.Visible = FALSE 'No need for button with hot link.
Text1.LinkMode = NONE 'Clear DDE Link.
Text1.LinkMode = HOT 'Reestablish new LinkMode.
End Sub
7. Add the following code to the Click event procedure of the Request
button:
Sub Request_Click ()
'With a cold DDE link this button will be visible, and clicking
'this button will request an update of information from the server
'application to the client 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 will be visible, and clicking
'this button will poke information from the client application
'into the server application.
Text1.LinkPoke
End Sub
You can now run the Visual Basic client application from the Visual
Basic VB.EXE environment (skip to step 4 below) or you can save the
application and create an EXE file and run that from Windows (continue
to step 1 below).
1. From the File menu, choose Save and save the form and project with
the name CLIENT.
2. From the File menu, choose Make EXE File with the name CLIENT.EXE.
3. Exit the Visual Basic environment (VB.EXE).
4. Run the application (from Windows if an .EXE file or from the Run
menu if from the Visual Basic environment.)
Form1 of the Visual Basic client application will be loaded, and
Word for Windows will automatically be started with the document
SERVER.DOC loaded.
5. Make sure that the main title bar in WINWORD.EXE reads "Microsoft
Word", NOT "Microsoft Word - SERVER.DOC". If the title bar is not
correct, choose Arrange All from the Window menu.
You can now experiment with DDE between Visual Basic and Word
for Windows:
1. Try typing some text into the document in Word for Windows, and
then click the Request button. The text appears in the text box.
2. Click Hot Link and 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 and
click the Poke button. The text is sent to the document in Word for
Windows.
Note that if in the WINWORD.EXE document, you delete the total
contents of the bookmark, the bookmark will also be deleted. Any
further attempt to perform DDE with this WINWORD.EXE session after the
bookmark has been deleted gives the following error message:
Foreign application won't perform DDE method or operation.
If this happens, you must recreate the bookmark in the document in
Word for Windows before performing any further DDE operations.
You can also establish DDE between applications at design time, as
described on page 356 of the "Microsoft Visual Basic: Programmer's
Guide" version 1.0 manual.
For additional information on dynamic data exchange (DDE) between
Visual Basic 1.0 and other Windows applications, query on the
following words:
DDE and Visual Basic
Additional reference words: 1.00