Jack
Jack

Reputation: 5768

VSTO Get Excel Range and put in Textbox

I created a custom Task Pane with a few inputs on it. One of the inputs is a textbox.

What I want to do is click on the textbox, then drag select a range of cells and have the range be set to the textbox.

For example, the Conditional Formatting dialog window has this functionality.

What event do I use if I'm changing from my custom task pane to Excel?

enter image description here

Upvotes: 3

Views: 2188

Answers (1)

Luc Morin
Luc Morin

Reputation: 5380

In your modeless form, just add an event handler to the WorkSheet.SelectionChanged event.

Something like this:

public partial class Form1 : Form
{
    Microsoft.Office.Interop.Excel.Worksheet ws;

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        ws = Globals.ThisAddIn.Application.ActiveSheet;
        ws.SelectionChange += ws_SelectionChange;            

    }

    void ws_SelectionChange(Microsoft.Office.Interop.Excel.Range Target)
    {
        this.textBox1.Text = Target.Address; 
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        ws.SelectionChange -= ws_SelectionChange;
    }

}

Hope this helps

Upvotes: 4

Related Questions