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
- Microsoft Word for Windows, versions 2.0 and 6.0
SUMMARY
This article demonstrates how to send a LinkExecute event to Microsoft
Word for Windows from Microsoft Visual Basic for Windows 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."
MORE INFORMATION
The following example program demonstrates how to:
- Automatically start Word for Windows
- Automatically send text typed in a Visual Basic for Windows text
box to the Word for Windows document
- Print the Word for Windows document to the selected printer.
- Start Visual Basic for Windows, or from the File menu, choose New
Project (press ALT, F, N) if Visual Basic for Windows is already
running. Form1 is created by default.
- Create the following controls with the given properties on Form1:
Object Name Caption
----------------------------------
TextBox Text1
Button Command1 Start Word
Button Command2 Link
Button Command3 Send Text
Button Command4 Print
(In Visual Basic version 1.0 for Windows set the CtlName Property
for the above objects instead of the Name property.)
- 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
- Add the following code to the Command2_Click event procedure:
Sub Command2_Click ()
text1.LinkMode = 0 ' Clears DDE link if it already exists.
text1.LinkTopic = "WinWord|document1" ' Set up link w/ WINWORD.EXE.
text1.LinkMode = 2 ' Establish a manual DDE link.
text1.linktimeout = 60 ' Set the time for a response to 6 seconds.
' If a DDETIMEOUT occurs increase the Text1.Linktimeout.
' Enter the following two lines as one, single line:
text1.LinkExecute
"[InsertBookmark .Name="+Chr$(34)+"Test"+Chr$(34)+"]"
' NOTE: the space is necessary as shown before .Name in the above
' LinkExecute statement.
' For Microsoft Word version 6.0, use the following instead and
' enter the two lines as one single line -- after removing the
' single quotation mark from the start of both lines:
' text1.LinkExecute
' "[EditBookmark .Name ="+Chr$(34)+"Test"+Chr$(34)+"]"
text1.LinkItem = "Test" ' Set link to a bookmark on document.
End Sub
- Add the following code to the Command3_Click event procedure:
Sub Command3_Click ()
text1.LinkPoke ' Sends the contents of the text box.
End Sub
- Add the following code to the Command4_Click event procedure:
Sub Command4_Click ()
text1.LinkExecute "[FilePrintDefault]" ' Prints the doc with the
' default printer settings.
End Sub
- Press the F5 key to run the program.
- Choose the Start Word button.
- Choose the Link button. This will establish a DDE conversation with
Word's Document1 and create a bookmark called Test using LinkExecute
and the embedded InsertBookmark WordBasic command. It will then set
the LinkItem to this newly created bookmark in Document1.
- 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.
- Choose the Print button to print the document in Word for Windows.
|