This project is read-only.

Working on a Excel 2010 with Visual Studio 2010

Oct 11, 2012 at 11:40 PM

This is an example for reading and writing text in cells from a Excel sheet.


In Excel sheet i just added  2 columns for test, the first is given by the textbox whe pressing "enter key" and the 2nd column is posted to the textbox when the value changes.

Server Get Cliente Post
60 600

 

using NDde.Client;
namespace DDETest
{
    public partial class Form1 : Form
    {
        //This class provides the infraestructure for DDE comunication
        NDde.Client.DdeClient ddeClient_TextBox1 = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //I initialize the DDEClient object. Application is Excel and Topic is Sheet1. I'm using the 
                //the TextBox as the syncronization object
                ddeClient_TextBox1 = new NDde.Client.DdeClient("EXCEL", "REUTERS.xls/Hoja2", textBox1);

                //Connect to the DDE Server
                ddeClient_TextBox1.Connect();
                //Start the Advise Loop
                ddeClient_TextBox1.StartAdvise("R2C2", 1, true, 60000);
                //Setup the Advise Method
                ddeClient_TextBox1.Advise += new EventHandler<NDde.Client.DdeAdviseEventArgs>(OnAdvise);
                //Setup a method to Poke the Server for TextBox cahnges
                textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            }
            catch (NDde.DdeException nddex)
            {
                switch (nddex.Code)
                {
                    case 16394:
                        MessageBox.Show("No hay una aplicacion abierta Excel con la Hoja1");
                        this.Close();
                        break;
                    default:
                        MessageBox.Show(nddex.Message);
                        this.Close();
                        break;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
        }

        const string DDE_postFix = "\r\n\0";
        void OnAdvise(object sender, NDde.Client.DdeAdviseEventArgs e)
        {
            //Advise only if needed
            if (e.Text.Length >= DDE_postFix.Length && textBox1.Text + DDE_postFix != e.Text)
                textBox1.Text = e.Text.Substring(0, e.Text.Length - 3);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == '\r')
            {
                if (ddeClient_TextBox1.IsConnected)
                    //Syncronous Poking the server
                    ddeClient_TextBox1.Poke("R2C1", textBox1.Text + "\0", 4000);
                else
                    MessageBox.Show("El cliente ya no esta conectado.");
            }
        }

        //void OnDisconnected(object sender, DdeDisconnectedEventArgs args)
        //{
        //    MessageBox.Show(
        //        "OnDisconnected: " +
        //        "IsServerInitiated=" + args.IsServerInitiated.ToString() + " " +
        //        "IsDisposed=" + args.IsDisposed.ToString());
        //}
    }
}

I Hope it helps you! =)

Feb 27, 2014 at 3:58 PM
Thanks, @antunio84. I've gist'd a slightly modified and console app version of your code in the interest of helping someone else, as you helped (at least) me.