Using DDE to Run a Word for Windows Macro from Access

ID: Q111783

2.x 6.00 6.00a 6.00c 7.00 WINDOWS kbusage kbmacro
The information in this article applies to:
  • Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, version 7.0


SUMMARY

This article describes a very simple example of using dynamic data exchange (DDE) to execute a Word for Windows macro from Microsoft Access. The example in this article has very little error checking and assumes a working knowledge of both products.


MORE INFORMATION

The commands used--DDEInitiate, DDEExecute--are very similar between Word and Microsoft Access, so the process should be familiar if you have worked with DDE using Word. One of the challenges of getting this process to work properly is that the command string for running a macro that is sent to Word for Windows is syntactically rigid. The syntax for the macro command in the DDE conversation must be:


[ToolsMacro .Name = "Macroname", Run] 
Representing this string in Microsoft Access can look visually confusing because the quotation marks are used in the command string and also in the assignment syntax for Microsoft Access. If this were to be written explicitly it would look like:

c$="[ToolsMacro .Name="+chr$(34)+"macroname"+chr$(34)+", .Run]" 
In the example below, this string has been broken up to help explain how the command line is built. Concatenating these yields the required syntax for the DDEExecute argument.
q$ is defined as ASCII character 34, which is the quotation mark.

mn$ is the name of the macro that is to be run.

tm$ is the beginning of the ToolsMacro string, and tme$ is the end of that string.
To create a simple example in Microsoft Access that runs a Word for Windows macro:

  1. In Microsoft Access create a sample database called DDEtest.


  2. In this database create a new module called DDEExample.


  3. In this module create a new function called runmacro(). The code for runmacro is:


  4. 
       Function runmacro ()
        Dim chan As Variant
        On Error GoTo Cantstart:
        Macrochan = DDEInitiate("Winword", "System")
        q$ = Chr$(34)
        mn$ = "Amacro"
        tm$ = "[ToolsMacro .name ="
        tme$ = ", .Run]"
    
        cmd$ = tm$ + q$ + mn$ + q$ + tme$
    
        DDEExecute Macrochan, cmd$
        DDETerminate Macrochan
       Exit Function
    
       Cantstart:
        MsgBox "Problem with DDEInitiate"
        Resume Next
       End Function 
  5. Create a Microsoft Access form with a single push button labeled Start that has an Onpush property of =RunMacro().


  6. Create a Word for Windows macro called "Amacro." For example:


  7. 
       Sub Main
        Filenewdefault
        Insert "This is from Amacro"
       End Sub 
  8. Make sure that Word for Windows is running.


  9. To watch the Microsoft Access macro and Word function, tile the windows so that Microsoft Access and Word for Windows are on the screen, simultaneously.


  10. Open the form in Microsoft Access and choose the Start button.


Additional query words: winword2 2.00a 2.00a-CD 2.00b 2.00c word7 word6 winword word95

Keywords :
Version : WINDOWS:1.0,1.1,1.1a,2.0,2.0a,2.0a-CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0
Platform : WINDOWS
Issue type :


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