JoeDev

I wanna to let the user to select the cells to copy manually.

For example when a button is clicked in the actions pane, let the user to select a workbook (file.xls) from the open dialog , and after the document is opened let the user to select a variable range of cells from the worksheet, and load this data in my workbook.

I'm using VSTO 2005 for a Excel 2003 workbook project.

Sorry my english, I'm still learning.

Thanks




Re: Visual Studio Tools for Office How to manually copy cells ranges from another workbook?

Ji Zhou ¨C MSFT

Hi Jose,

I write some sample codes for that. There are comments inline, if you still have any question, please feel free to let me know.

Code Snippet

private Excel.Workbook workbook = null;

private void ThisWorkbook_Startup(object sender, System.EventArgs e)

{

//Add a button on the Action Pane

Button btn = new Button();

this.ActionsPane.Controls.Add(btn);

btn.Click += new EventHandler(btn_Click);

}

void btn_Click(object sender, EventArgs e)

{

//Use OpenFileDialog to let use choose a .xls file

OpenFileDialog file = new OpenFileDialog();

if (file.ShowDialog() == DialogResult.OK)

{

//Open user selected workbook

workbook = this.Application.Workbooks.Open(

file.FileName, missing, missing, missing, missing,

missing, missing, missing, missing, missing, missing,

missing, missing, missing, missing);

//Register the event Selection Change. It will be raised

//when your selection is changed in the new opened workbook

((Excel.WorkbookEvents_Event)workbook).SheetSelectionChange += new Microsoft.Office.Interop.Excel.WorkbookEvents_SheetSelectionChangeEventHandler(ThisWorkbook_SheetSelectionChange);

}

}

void ThisWorkbook_SheetSelectionChange(object Sh, Microsoft.Office.Interop.Excel.Range Target)

{

//Popup a messagebox to let user confirm if copy these cells

if (MessageBox.Show("Are you sure to copy", "Confirm", MessageBoxButtons.OKCancel)

== DialogResult.OK)

{

//Copy cells to your workbook and close the source workbook

Target.Copy(Globals.Sheet1.Cells[1, 1]);

workbook.Close(missing, missing, missing);

}

}

Thanks

Ji