ACC: Sample DDE Macro Communicates with Microsoft Excel 4.0

Last reviewed: June 6, 1997
Article ID: Q97522
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0
  • Microsoft Excel for Windows, version 4.0

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.

MORE INFORMATION

Running a Microsoft Access Macro from Microsoft Excel

Perform the following three steps to demonstrate this behavior. These steps assume that a "MESSAGE" macro already exists in Microsoft Access.

  1. In Microsoft Excel, choose New from the File menu, select Macro Sheet and choose OK.

  2. Enter the following macro into the macro sheet. Substitute the appropriate location for files on your computer.

          =EXEC("c:\access\msaccess.exe c:\access\db4.mdb")
          Chan=INITIATE("MSACCESS","system")
          =APP.ACTIVATE("Microsoft Access")
          =EXECUTE(Chan,"MESSAGE")
          =TERMINATE(Chan)
          =RETURN()
    

  3. 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 "MESSAGE," as follows.

   Name Summary
   --------------------------------
   Application Name: "MSACCESS"
   Window Title: "Microsoft Access"
   Topic: "System"
   Item: "Message"

Running a Microsoft Excel Macro from Microsoft Access

Perform the following two steps to demonstrate this behavior. These steps assume that a macro named "Message" already exists in a Microsoft Excel macro sheet named "MACRO1.XLM."

  1. 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("c:\excel\excel.exe c:\excel\macro1.xlm", 1)
             Chan = DDEInitiate("Excel", "System")
             DDEExecute Chan, "[Run(""macro1.xlm!Message"")]"
             DDETerminate Chan
          End Function
    
    

  2. Create a new blank form and add a command button.

  3. Select the button.

  4. From the View menu, choose Properties.

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

  6. View the form in Form view.

  7. Choose the command button to start the function.

The Shell function loads Microsoft Excel and the "MACRO1.XLM" 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 "Message."

   Name Summary
   -------------------------------------
   Application Name: "Excel"
   Window Title: "Microsoft Excel"
   Topic: "System"
   Item: "[Run(""macro1.xlm!Message"")]"

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

REFERENCES

"Microsoft Excel Function Reference", version 4.0, pages 14 - 15


Keywords : IntpDde kbinterop McrHowto
Version : 1.0 1.1 2.0 7.0
Platform : WINDOWS
Hardware : X86
Issue type : kbinfo


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: June 6, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.