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

ID: Q85857

The information in this article applies to:

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          : kbinterop kbprg kb16bitonly kbVBp400 IAPDDE VB4WIN vbwin 
Version           : WINDOWS:1.0 2.0 3.0 4.0
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: October 1, 1997