ID Number: Q82879
1.00
WINDOWS
Summary:
This article demonstrates how to send a LinkExecute event to Word
for Windows from Visual Basic using dynamic data exchange (DDE).
The commands available through DDE with Word for Windows are as
follows:
- Any Macro in Word for Windows
- Any embedded WordBasic command built into Word for Windows
A full explanation of the above commands can be found in Word for
Windows online Help under the topic "WordBasic."
This information applies to Microsoft Visual Basic version 1.0 for
Windows and Word version 2.0 for Windows.
More Information:
The following example program demonstrates how to:
- Automatically start Word for Windows
- Automatically send text typed in the Visual Basic text box to the
Word for Windows document
- Print the Word for Windows document to the selected printer.
1. Run Visual Basic, or from the File menu, choose New Project (ALT,
F, N) if Visual Basic is already running. Form1 is created by
default.
2. Create the following controls with the given properties on Form1:
Object CtrlName Caption
------ -------- -------
TextBox Text1
Button Command1 Start Word
Button Command2 Link
Button Command3 Send Text
Button Command4 Print
3. Add the following code to the Command1 Click event:
Sub Command1_Click ()
x = Shell("winword.exe", 7) 'start Word for windows
'minimized without the focus
x = DoEvents() 'this gives WinWord time to load
End Sub
4. Add the following code to the Command2 Click event:
Sub Command2_Click ()
text1.LinkMode = 0 'Clears DDE link if it already exists.
text1.LinkTopic = "WinWord|document1" 'Sets up link with
'WINWORD.EXE.
text1.LinkMode = 2 'Establish a cold DDE link.
text1.linktimeout = 60 'set the time for a response to 6 seconds
'if a DDETIMEOUT occurs increase the Text1.Linktimeout
text1.LinkExecute "[InsertBookmark .Name ="+Chr$(34)+"Foo"+Chr$(34)+"]"
'(Note that the space is necessary as shown before .Name in the above
' LinkExecute statement.)
text1.LinkItem = "Foo" 'Set link to a bookmark on document.
End Sub
5. Add the following code to the Command3 Click event:
Sub Command3_Click ()
text1.LinkPoke 'sends the contents of the text box
End Sub
6. Add the following code to the Command4 Click event:
Sub Command4_Click ()
text1.LinkExecute "[FilePrintDefault]" 'prints the doc with the
'default printer settings
End Sub
7. Press F5 to run the program.
6. Choose the Start Word button.
7. Choose the Link button. This will establish a DDE conversation with
Word's Document1 and create a bookmark called Foo using LinkExecute
and the embedded InsertBookmark WordBasic command. It will then set
the LinkItem to this newly created bookmark in Document1.
8. Type some text in the text box and choose the Send Text command
button to send the contents of the text box to Word for Windows.
9. Choose the Print button to print the document in Word for Windows.
Additional reference words: 1.00