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

ID: Q82879


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0
  • Microsoft Word for Windows, versions 2.0, 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.


  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:


  4. 
       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 
  5. Add the following code to the Command2_Click event procedure:


  6. 
       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 
  7. Add the following code to the Command3_Click event procedure:


  8. 
       Sub Command3_Click ()
          text1.LinkPoke  ' Sends the contents of the text box.
       End Sub 
  9. Add the following code to the Command4_Click event procedure:


  10. 
       Sub Command4_Click ()
          text1.LinkExecute "[FilePrintDefault]"   ' Prints the doc with the
                                                   ' default printer settings.
       End Sub 
  11. Press the F5 key to run the program.


  12. Choose the Start Word button.


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


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


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


Additional query words:

Keywords : kbVBp300 kbWord kbvbp200
Version : WINDOWS:1.0,2.0,6.0
Platform : WINDOWS
Issue type :


Last Reviewed: December 17, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.