Microsoft KB Archive/97522

= ACC: Sample DDE Macro Communicates with Microsoft Excel 4.0 =

Article ID: 97522

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Excel 4.0 Standard Edition

-



This article was previously published under Q97522



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

The text below demonstrates communication between two applications through dynamic data exchange (DDE). Specifically, one example shows a macro developed for Microsoft Excel version 4.0 for Windows that starts Microsoft Access, loads a database and runs a macro. A second example shows an Access Basic function that loads Microsoft Excel and runs a macro.



Running a Microsoft Access Macro from Microsoft Excel
Perform the following three steps to demonstrate this behavior. These steps assume that a &quot;MESSAGE&quot; macro already exists in Microsoft Access.

 In Microsoft Excel, choose New from the File menu, select Macro Sheet and choose OK.  Enter the following macro into the macro sheet. Substitute the appropriate location for files on your computer.

     =EXEC(&quot;c:\access\msaccess.exe c:\access\db4.mdb&quot;) Chan=INITIATE(&quot;MSACCESS&quot;,&quot;system&quot;) =APP.ACTIVATE(&quot;Microsoft Access&quot;) =EXECUTE(Chan,&quot;MESSAGE&quot;) =TERMINATE(Chan) =RETURN  To run the macro, select the first cell, select Run from the Macro menu and choose OK.

The EXEC function in the macro loads Microsoft Access minimized and loads the DB4.MDB database. The macro initiates a DDE channel and assigns it to the variable Chan. The APP.ACTIVATE switches to the Microsoft Access window to show the macro actions running.

The EXECUTE function runs a macro named &quot;MESSAGE,&quot; as follows.

  Name Summary Application Name: &quot;MSACCESS&quot; Window Title: &quot;Microsoft Access&quot; Topic: &quot;System&quot; Item: &quot;Message&quot;

Running a Microsoft Excel Macro from Microsoft Access
Perform the following two steps to demonstrate this behavior. These steps assume that a macro named &quot;Message&quot; already exists in a Microsoft Excel macro sheet named &quot;MACRO1.XLM.&quot;

  In Microsoft Access, create a new function that contains the following code. Substitute the appropriate location for files on your computer. Function CallExcel Dim Chan x = Shell(&quot;c:\excel\excel.exe c:\excel\macro1.xlm&quot;, 1) Chan = DDEInitiate(&quot;Excel&quot;, &quot;System&quot;) DDEExecute Chan, &quot;[Run(&quot;&quot;macro1.xlm!Message&quot;&quot;)]&quot; DDETerminate Chan End Function  Create a new blank form and add a command button.</li> Select the button.</li> From the View menu, choose Properties.</li> Set the button's OnPush property to the following:

= CallExcel

Note that the OnPush property is named OnClick in Microsoft Access versions 2.0 and 7.0.</li> View the form in Form view.</li> Choose the command button to start the function.</li></ol>

The Shell function loads Microsoft Excel and the &quot;MACRO1.XLM&quot; macro sheet full screen and leaves the focus on Microsoft Excel. The macro initiates a DDE channel and assigns it to the variable Chan.

The EXECUTE function runs a macro named &quot;Message.&quot;

<pre class="fixed_text">  Name Summary -  Application Name: &quot;Excel&quot; Window Title: &quot;Microsoft Excel&quot; Topic: &quot;System&quot; Item: &quot;[Run(&quot;&quot;macro1.xlm!Message&quot;&quot;)]&quot;

The syntax for these two macros demonstrates that the correct syntax for the &quot;Item&quot; depends on the target application.

<div class="references_section">