HOWTO: Use DDE to Run a Word for Windows Macro from VB

Last reviewed: September 29, 1997
Article ID: Q85857
The information in this article applies to:
  • Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit only, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic for Windows, version 1.0
  • Microsoft Word for Windows, versions 2.0, 6.0, 6.0a

SUMMARY

This article demonstrates how to send a LinkExecute command to Microsoft Word for Windows from Visual Basic using dynamic data exchange (DDE) to run a macro.

MORE INFORMATION

The following sample program demonstrates how to automatically start Word for Windows and execute a Word macro called MyMacro.

Steps to Create Sample Program

  1. Start Word and start a new project in Visual Basic. Form1 is created by default.

  2. Place the following controls on Form1, and give them these properties:

          Object     Name         Caption
          ---------------------------------
          TextBox    Text1
          Button     Command1     Start Word
          Button     Command2     MyMacro
    
    

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

  4. Add the following code to the Command2 Click event:

          Sub Command2_Click ()
             Text1.LinkMode = 0         'Clear DDE link if it already exists.
             Text1.LinkTopic = "Winword|document1"  'Set up WINWORD.EXE link.
             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
    
             'VB3Line: Enter the following lines as one line
             Text1.LinkExecute _
                "[ToolsMacro .Name ="+Chr$(34)+"MyMacro"+Chr$(34)+",.Run]"
             ' NOTE: the space is necessary as shown before .Name in the
             ' above LinkExecute statement.
          End Sub
    
    

  5. Create a macro called MyMacro in Word that inserts "hello world" in the document:

    a. Switch to Word.

    b. From the Tools menu, choose Macro.

    c. Type "MyMacro" in the Macro Name field. Choose the Edit button.

    d. Type the following:

             Insert "Hello World"
    
       e. From the File menu, choose Close. At the "Do you want to keep the
          changes to Global: MyMacro?" prompt, choose Yes. (This will save the
          newly created MyMacro macro.)
    
       f. From the File menu, choose Exit. At the "Do you want to save the
          global glossary and command changes?" prompt, choose Yes. (The
          MyMacro macro has been added to the Word NORMAL.DOT file.)
    
    

  6. Press the F5 key to run the Visual Basic program.

  7. Choose the Start Word button.

  8. Choose the MyMacro button. This will establish a DDE conversation with Word Document1 and execute the MyMacro macro.

  9. Switch to Word to verify that the Document1 contains "Hello World," confirming that the MyMacro macro has been run (the CTRL+HOME key combination will move the cursor back to the beginning of the document).
Keywords          : IAPDDE vb416 VB4WIN vbwin GnrlVb kbinterop kbprg kbfasttip
Technology        : kbvba
Version           : WINDOWS:1.0 2.0 3.0 4.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.