INFO: VB Example Using DDE LinkExecute to Word for Windows 2.0

ID: Q82879

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:

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:

1. 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.

2. 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.)

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 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

5. Add the following code to the Command3_Click event procedure:

   Sub Command3_Click ()
      text1.LinkPoke  ' Sends the contents of the text box.
   End Sub

6. 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

7. Press the F5 key to run the program.

8. Choose the Start Word button.

9. 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.

10. 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.

11. Choose the Print button to print the document in Word for Windows.

Additional query words: Keywords : kbVBp300 kbWord kbvbp200

Last Reviewed: January 7, 1999