Microsoft KB Archive/105447

= How To Call a Visual Basic Application from Excel =

Article ID: 105447

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q105447



SUMMARY
This article outlines the steps necessary to initiate dynamic data exchange (DDE) between an Excel Macro and Visual Basic application at run-time.

This article demonstrates How To


 * Prepare an Excel Macro that will perform DDE.
 * Initiate a manual DDE link.
 * Use Poke to send information from the Excel Worksheet to the Visual Basic application.



MORE INFORMATION
A destination application sends commands through DDE to the source application to establish a link. Through DDE, the source provides data to the destination at the request of the destination or accepts information at the request of the destination.

Example Showing How to Establish a DDE Conversation
The steps below give an example of how to establish a DDE conversation between an Excel Worksheet and a Visual Basic application.

Preparing Visual Basic for DDE
 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default. Add a text box to form1.  Change the following properties:   Control       Name         Caption       Properties --  Form1         Form1        Form1         LinkMode  : 1-Source LinkTopic : Form1 TextBox      Text1        Text1         LinkItem  : Text1  Save the project as DDE.MAK, and save the form as DDE.FRM. From the File menu, choose Make Exe File, and name the .EXE file DDE.EXE to enable the link. Close Visual Basic.</li></ol>

Preparing Excel for DDE
<ol> When you start Excel, Sheet1.XLS is created for you by default. In Cell C2, type the text you want to send to the Visual Basic application. Save the Sheet1 spreadsheet.</li> Create a new Macro Sheet in Excel by choosing New from the File Menu and then selecting Macro Sheet.</li>  Type the following macro: Record1 (a) chan=INITIATE("dde","Form1") =POKE(chan,"text1",'A:\SHEET1.XLS'!C2) =TERMINATE(chan) =RETURN(chan) The Initiate function starts the Visual Basic application if it is not already running and establishes the link with Form1. The Poke function puts text from cell C2 of the Sheet1 worksheet into the Text1 box on form1. The Terminate function terminates the link, and the Return function ends the macro.

Important Notes

<ul> 

</li> Save the Worksheet before attempting the Link.</li> Store the application in the path by making sure the application directory is in the path.</li> Not specify the path to the application in the Initiate function.</li></ul> </li> From the Macro menu, choose Run.</li> You will see a message box saying Run Macro. In the reference section, type Macro1.xlm!Record1 and click OK.</li> If DDE.EXE is not running, you will a message box saying "Remote Data not accessible. Start application 'DDE.EXE'." Click Yes.</li> If DDE.EXE was in the path, it will be run minimized. When you maximize it, you will see the data from Sheet1 in the Text1 box. If you don't see the data, save Sheet1 and try again.</li></ol>

Keywords: kbhowto kbinterop kbprogramming KB105447

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.